Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to filter ListView using getFilter() in BaseAdapter
    primarykey
    data
    text
    <p>In my application I have create a custom list view and I want to implement a filter so that the list can be filtered according to the text entered in the EditText. I am using a BaseAdapter as a separate class and I am calling that class in my Main Activity. I have also implemented the addTextChangedListener() in my main activity and I have also implemented the getFilter() in my BaseAdapter class. But I don't know how can I use getFilter() and can filter my list accordingly. In the list I am adding the values from a JSON URL. Please help me in letting me know that how can I use the getFilter() in order to filter my List.</p> <p>The code of Activity class:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView)findViewById(R.id.listView1); et1 = (EditText)findViewById(R.id.editText1); inflator = getLayoutInflater(); et1.addTextChangedListener(this); JsonParser jParser = new JsonParser(); JSONObject json = jParser.getJSONfromUrl(url); try { JSONArray explore = json.getJSONArray("explore"); for(int i=0; i&lt;explore.length(); i++) { JSONObject exp = explore.getJSONObject(i); list.add(exp.getString("username")); } } catch(JSONException e) { e.printStackTrace(); } srchadptr = new SearchAdapter(this, inflator, list); lv.setAdapter(srchadptr); } public void afterTextChanged(Editable s) { // TODO Auto-generated method stub srchadptr.getFilter().filter(s); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } </code></pre> <p>The code of BaseAdapter class:</p> <pre><code>public class SearchAdapter extends BaseAdapter implements Filterable { Context context; LayoutInflater inflater; Button btn; View vw; ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); public SearchAdapter(Context context, LayoutInflater inflater, ArrayList&lt;String&gt; list) { // TODO Auto-generated constructor stub this.context = context; this.inflater = inflater; this.list = list; } /*public CharSequence filter(CharSequence cs) { return cs; }*/ public int getCount() { // TODO Auto-generated method stub return list.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LinearLayout ll = (LinearLayout) vw; final EditText edt = ((EditText)ll.getChildAt(0)); vw = inflater.inflate(R.layout.list_items, null); ImageView img = (ImageView)vw.findViewById(R.id.imageView1); TextView tv = (TextView)vw.findViewById(R.id.textView1); btn = (Button)vw.findViewById(R.id.button1); tv.setText(String.valueOf(list.get(position))); btn.setText(String.valueOf(list.get(position))); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(context, list.get(position), Toast.LENGTH_LONG).show(); } }); return vw; } public android.widget.Filter getFilter() { // TODO Auto-generated method stub return new android.widget.Filter() { @Override protected void publishResults(CharSequence constraint, FilterResults results) { // TODO Auto-generated method stub } @Override protected FilterResults performFiltering(CharSequence constraint) { // TODO Auto-generated method stub return null; } }; } } </code></pre> <p>Thanks in advance...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload