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);
9
  • Possible duplicate of Filter ListView with arrayadapter Commented Feb 17, 2017 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 ? Commented Feb 17, 2017 at 18:49
  • this.simNo = (String[]) object[0]; this.mobileNo = (String[]) object[1]; this.data = (String[]) object[2]; Commented Feb 17, 2017 at 18:49
  • Create a java class to hold your trio of strings as one object, then follow the question linked above. Commented Feb 17, 2017 at 19:01
  • can you post some sample code ? Commented Feb 17, 2017 at 19:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi but I have a two Array , How can I assign that two values to result.values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.