【问题标题】:How to implement AddTextChangedListener on a Custom ListView (BaseAdapter)如何在自定义 ListView (BaseAdapter) 上实现 AddTextChangedListener
【发布时间】:2012-09-15 06:17:51
【问题描述】:

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

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 类上实现过滤器...

提前谢谢...

【问题讨论】:

    标签: android filter android-listview baseadapter custom-lists


    【解决方案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 ;
        }
    

    【讨论】:

    • 我将如何过滤列表,因为这里没有这样的适配器..请帮助...谢谢..在实现您的代码后我也收到 NullPointerException 错误..以及这个“2 " 代表 getChildAt(2).. 谢谢
    猜你喜欢
    • 2012-01-24
    • 2023-03-12
    • 1970-01-01
    • 2019-02-13
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    相关资源
    最近更新 更多