ListView android



  • The task is to search listViev, but the code is adequately searched only by the first symbols, if the symbols in the words are in the middle or at the end, it fails at all. If you need to get an extra code, I'll add it.

     mInputSearch.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            ListFragment.this.mAdapter.getFilter().filter(cs);
        }
    
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });
    

    ListFragment.java

    public class ListFragment extends Fragment {

    private ListView mListView;
    private EditText mInputSearch;

    private ArrayList<String> mList;
    private ArrayAdapter<String> mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mList = new ArrayList&lt;String&gt;();
    fillArray();
    
    mAdapter = new ArrayAdapter&lt;String&gt;(getActivity().getApplicationContext(),R.layout.item_list, mList);
    

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_list, container, false);

    mInputSearch = (EditText) v.findViewById(R.id.inputSearch);
    
    mListView = (ListView) v.findViewById(R.id.listView);
    mListView.setAdapter(mAdapter);
    
    return v; }
    

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mInputSearch.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            ListFragment.this.mAdapter.getFilter().filter(cs);
    
    
        }
    
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });
    

    }

    private void fillArray() {

    mList.add("Lorem");
    mList.add("Ipsum");
    mList.add("Dolor");
    mList.add("Sit");
    mList.add("Amet");
    mList.add("Consectetur");
    mList.add("Adipiscing");
    mList.add("Elit");
    mList.add("Fusce");
    mList.add("Pharetra");
    mList.add("Luctus");
    mList.add("Sodales");
    
    Collections.sort(mList);
    

    }
    }



  • In order to build a filter, a sequence must be determined:

    1. Create your custom adapter.
    2. Link Filterable interface
    3. Build a method getFilter.

    I've done a working example to test:

    Castom Adapter:

    public class myAdapter extends ArrayAdapter<String> implements Filterable {
    

    private List<String> allModelItemsArray;
    private List<String> filteredModelItemsArray;
    private Activity context;
    private ModelFilter filter;
    private LayoutInflater inflater;

    public myAdapter(Activity context, ArrayList<String> list) {
    super(context, R.layout.list_item, list);
    this.context = context;
    this.allModelItemsArray = new ArrayList<String>();
    allModelItemsArray.addAll(list);
    this.filteredModelItemsArray = new ArrayList<String>();
    filteredModelItemsArray.addAll(allModelItemsArray);
    inflater = context.getLayoutInflater();
    getFilter();
    }
    @Override
    public Filter getFilter() {
    if (filter == null){
    filter = new ModelFilter();
    }
    return filter;
    }
    static class ViewHolder {
    protected TextView text;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    String m = filteredModelItemsArray.get(position);
    ViewHolder viewHolder = null;
    if (convertView == null) {

        view = inflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.tvItem);
        view.setTag(viewHolder);
    } else {
        view = convertView;
        viewHolder = ((ViewHolder) view.getTag());
    }
    viewHolder.text.setText(m);
    return view;
    

    }

    private class ModelFilter extends Filter
    {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
    
        constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if(constraint.toString().length() &gt; 0)
        {
            ArrayList&lt;String&gt; filteredItems = new ArrayList&lt;String&gt;();
    
            for(int i = 0, l = allModelItemsArray.size(); i &lt; l; i++)
            {
                String m = allModelItemsArray.get(i);
                if(m.toLowerCase().contains(constraint))
                    filteredItems.add(m);
            }
            result.count = filteredItems.size();
            result.values = filteredItems;
        }
        else
        {
            synchronized(this)
            {
                result.values = allModelItemsArray;
                result.count = allModelItemsArray.size();
            }
        }
        return result;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
    
        filteredModelItemsArray = (ArrayList&lt;String&gt;)results.values;
        notifyDataSetChanged();
        clear();
        for(int i = 0, l = filteredModelItemsArray.size(); i &lt; l; i++)
            add(filteredModelItemsArray.get(i));
        notifyDataSetInvalidated();
    }
    

    }
    }

    MainActivity.class

    public class MainActivity extends AppCompatActivity
    {
    ListView lv;
    EditText editText;
    myAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.lv);
    editText = (EditText) findViewById(R.id.editText);
    
    ArrayList&lt;String&gt; values = new ArrayList&lt;&gt;();
    for (int i = 0; i &lt; 25; i++) {
        values.add("Item:" + i);
    }
    adapter = new myAdapter(this, values);
    lv.setAdapter(adapter);
    
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            adapter.getFilter().filter(s.toString());
        }
    });
    

    }
    }

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

    <ListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true" />
    </RelativeLayout>

    list_item.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
    android:id="@+id/tvItem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:paddingRight="10dp"
    android:textSize="12sp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" />
    </LinearLayout>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2