1

I want to filter my listview data where I am using custom list adapter and using ArrayList to store the data inside that. I am store data like users name, address. But I don't how I can filter the data. I want to filter data by name. Can anyone help me out? Custom list adapter code

public class ListAdapter extends BaseAdapter {
Context context;
ArrayList<String> name;
ArrayList<String> id ;
ArrayList<String> add1 ;
ArrayList<String> add2 ;
ArrayList<String> city ;


public ListAdapter(
        Context context2,
        ArrayList<String> id,
        ArrayList<String> name,
        ArrayList<String> add1,
        ArrayList<String> add2,
        ArrayList<String> city
)
{

    this.context = context2;
    this.id = id;
    this.name = name;
    this.add1 = add1;
    this.add2 = add2;
    this.city = city;
}

public int getCount() {
    return id.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

public View getView(int position, View child, ViewGroup parent) {

    Holder holder;

    LayoutInflater layoutInflater;

    if (child == null) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        child = layoutInflater.inflate(R.layout.listviewdatalayout, null);

        holder = new Holder();

        holder.account_master_name = (TextView) child.findViewById(R.id.account_master_name);
        holder.account_master_add = (TextView) child.findViewById(R.id.account_master_add);

        child.setTag(holder);

    } else {

        holder = (Holder) child.getTag();
    }
    holder.account_master_name.setText(id.get(position)+ "\t"+ name.get(position));
    holder.account_master_add.setText(add1.get(position)+""+add2.get(position)+city.get(position));

    return child;
}

public class Holder {
    TextView account_master_name;
    TextView account_master_add;
}

}

A.J
  • 133
  • 1
  • 1
  • 11

2 Answers2

0
List<ListAdapter> filterList = new ArrayList<>();
List<ListAdapter> allList = new ArrayList<>();

//Add Data to list

filterList.clear();
for (ListAdapter listItem : allList)
{

    if(listItem.name.contains("yourText")
    {
       filterList.add(listItem);
    {
}

Then you display filterList in your ListView

Janwilx72
  • 502
  • 4
  • 18
0

You could try and run a filter on the Adapter when like this:

Untested Code

EditText search = findViewById(R.id.search); search.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        listAdapter.getFilter().filter(s);
        listAdapter.notifyDataSetChanged();

    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    public void afterTextChanged(Editable s) {}
});
jampez77
  • 5,012
  • 7
  • 32
  • 52