0

I have a custom adapter using ArrayAdapter but I don't know how to set filter for all values. This my custom adapter code

public class ListAdapter extends ArrayAdapter<String> {

String[] simNo, mobileNo, data;
Activity activity;

public ListAdapter(Activity activity, Object object[]) {
    super(activity, R.layout.search_data, (String[]) object[0]);
    this.simNo = (String[]) object[0];
    this.mobileNo = (String[]) object[1];
    this.data = (String[]) object[2];
    this.activity = activity;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {
    String DELIMITER = " : ";
    LayoutInflater inflater = activity.getLayoutInflater();
    view = inflater.inflate(R.layout.search_data, null, true);
    ((TextView) view.findViewById(R.id.sim_no_text)).setText("Sim Number"+DELIMITER+simNo[position]);
    ((TextView) view.findViewById(R.id.mob_no_text)).setText("Mobile Number"+DELIMITER+mobileNo[position]);
    ((TextView) view.findViewById(R.id.data)).setText(data[position]);
        return view;

}
}

Loading Data :

public Object[] formListViewDetails(JSONObject result) {

    int noOfSim;
    noOfSim = result.optInt("no_of_sims");
    String[] data, simNo, mobileNo;
    simNo = new String[noOfSim];
    mobileNo = new String[noOfSim];
    data = new String[noOfSim];
    try {
        JSONArray totalData = result.getJSONArray("sim_details");
        for (int i = 0; i < noOfSim; i++) {
            simNo[i] = totalData.optJSONObject(i).optString("sim_no");
            mobileNo[i] = totalData.optJSONObject(i).optString("mobile_no");
            data[i] = totalData.optJSONObject(i).toString();
        }
        return new Object[]{simNo, mobileNo, data};
    } catch (Exception e) {
        Log.d(TAG, "formListViewDetails: " + e.toString());
        return null;
    }

}

Calling Array adapter :

listAdapter = new ListAdapter(this, formListViewDetails(result));
listView.setAdapter(listAdapter);
Kalanidhi
  • 4,902
  • 27
  • 42
  • Possible duplicate of [Filter ListView with arrayadapter](http://stackoverflow.com/questions/10532194/filter-listview-with-arrayadapter) – petey Feb 17 '17 at 18:42
  • HI,I have a multiple Array , How can I search string in those array and I want to search content in both sim no and also mobile number ? – Kalanidhi Feb 17 '17 at 18:49
  • this.simNo = (String[]) object[0]; this.mobileNo = (String[]) object[1]; this.data = (String[]) object[2]; – Kalanidhi Feb 17 '17 at 18:49
  • Create a java class to hold your trio of strings as one object, then follow the question linked above. – petey Feb 17 '17 at 19:01
  • can you post some sample code ? – Kalanidhi Feb 17 '17 at 19:02
  • I always avoid this kind of coding style `String[] simNo, mobileNo, data;` Basically it is hard to maintain. I typically create an Object that represents the the value rather making a separate list of each details like. a `Contact` class or related. If you do that you can group them into a meaninful way and be able to create a clean array e.g. `List`, apart from that it is easier to apply filters. – Enzokie Feb 19 '17 at 12:42
  • @Enzokie Thanks for your response, I am loading the data in object and passing to array adapter, How can I convert to `List` ? verify my updated post – Kalanidhi Feb 19 '17 at 12:48
  • Normally you create class e.g. public class [Contact](https://hastebin.com/turomeqita.cs). Also I think its time to switch using RecyclerView because it is the modern version of ListView. – Enzokie Feb 19 '17 at 12:55
  • I understood, If i create class and set the value into that class use setter , my doubt when I set multiple value , that class value override or not ? – Kalanidhi Feb 19 '17 at 13:05

1 Answers1

1

use the class filter https://developer.android.com/reference/android/widget/Filter.html for example in your adapter implement interface Filterable

public class ListAdapter extends ArrayAdapter<String> implements
    Filterable {
.
.
.
    @Override
    public Filter getFilter() {
     if (mFilter == null)
        mFilter = new LocalFilter();

     return mFilter;
   }
    private class LocalFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        // We implement here the filter logic
        if (constraint == null || constraint.length() == 0) {
            // No filter implemented we return all the list
            results.values = arrayString;
            results.count = arrayString.size();
        } else {
            // We perform filtering operation
            List<Local> nArrayList = new ArrayList<Local>();
            for (String item : mStrings) {
                if (item.toUpperCase()
                        .contains(constraint.toString().toUpperCase()))
                    nArrayList.add(local);
            }

            results.values = nArrayList;
            results.count = nArrayList.size();
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        // Now we have to inform the adapter about the new list filtered
        mStrings = (ArrayList<String>) results.values;
        notifyDataSetChanged();
    }
}

I hope it helps

Oswaldo Leon
  • 261
  • 3
  • 12