0

在我的应用程序中,我创建了一个包含 textview、imageview 和按钮的自定义列表视图,并且我能够成功加载该列表中的 json 数据。现在我想实现一个文本观察器,这样我就可以对列表进行排序,并且只能查看那些被编辑文本过滤的项目。

CustomList 活动的代码:

@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);
}

SearchAdapter 类 (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;
    }
}

请帮助我让我知道如何在这个 SearchAdapter 类上实现过滤器......

提前致谢...

4

1 回答 1

-1

您非常接近解决方案,请在您的 getView 方法中进行以下更改。

    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 ;
    }
于 2012-09-15T06:48:47.113 回答