0

In my application I have created a custom listview containing textview, imageview and button and i am able to sucessfully load the json data in that list. Now I want to implement a textwatcher so that I can sort my list and can only view those items that are filtered by the edit text..

Code for CustomList Activity:

@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<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

}

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
    srchadptr.getItem(start);
}

Code for SearchAdapter Class (BaseAdapter):

public class SearchAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;
    Button btn;
    View vw;
    ArrayList<String> list = new ArrayList<String>();

    public SearchAdapter(Context context,   LayoutInflater inflater, ArrayList<String> 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 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        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;
    }
}

Plase help me in letting me know that how can implement the filter on this SearchAdapter Class...

Thanks in advance...

Abhishek Dhiman
  • 1,631
  • 6
  • 25
  • 38

1 Answers1

-1

You are very near to solution,make following changes in your getView method.

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
       View view = super.getView(position, convertView, parent);

            LinearLayout vwParentRow = (LinearLayout) view;

            final EditText yourtextview= ((EditText) vwParentRow.getChildAt(2));
            //let say your edittext is 2nd in the  listview
            yourtextview.addTextChangedListener(yourtextchangelistener);
           ..........

        return view ;
    }
Zain Ali
  • 15,535
  • 14
  • 95
  • 108
  • How will I filter the list as there is no such adapter here.. please help... thanks.. and I am also getting a NullPointerException error after implementing your code.. and what this "2" represent in the getChildAt(2).. thanks – Abhishek Dhiman Sep 15 '12 at 07:24