【问题标题】:Search in Android ListView在 Android ListView 中搜索
【发布时间】:2017-09-06 03:01:01
【问题描述】:

我正在尝试在this 教程之后添加搜索ListView 功能。问题是当我在搜索EditText 中输入内容时,我无法显示任何内容。我希望能够显示用户正在搜索的内容,但事实并非如此。我需要对以下代码进行哪些更改才能实现?

代码:

private void loadList()
{
    Cursor res = tbl_business_desc.getRegisterData();
    int count = res.getCount();
    lvListItemsData = new android.projects.helper.mylist.ListViewItems[count];
    res.moveToFirst();
    for (int i = 0; i < count; i++) 
    {
        String ControlNum  = res.getString(res.getColumnIndex(tbl_business_desc.col_ControlNum));
        String BizName     = res.getString(res.getColumnIndex(tbl_business_desc.col_BizName));
        String Owner       = res.getString(res.getColumnIndex(tbl_business_desc.col_Owner));
        String Add         = res.getString(res.getColumnIndex(tbl_business_desc.col_Add));
        String Tel         = res.getString(res.getColumnIndex(tbl_business_desc.col_Tel));
        String Email       = res.getString(res.getColumnIndex(tbl_business_desc.col_Email));
        lvListItemsData[i] = new android.projects.helper.mylist.ListViewItems(ControlNum,BizName,Owner,Add,Tel,Email);
        res.moveToNext();
    }
    lvList = (ListView) findViewById(R.id.lvMylist);
    inputSearch  = (EditText) findViewById(R.id.inputSearch);

    // Set the adapter to our ListView
    lvListadapter = new android.projects.helper.list.ListViewItemsAdapter(this, R.layout.listview_item_row_masterlist_body, lvListItemsData);
    lvList.setAdapter(lvListadapter);

    /**
     * Enabling Search Filter
     * */
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            MyListActivity.this.lvListadapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

    lvList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            showStatus(position);
        }
    });
}

ListViewItemAdapter 代码

public class ListViewItemsAdapter extends ArrayAdapter<ListViewItems> {

    Context mContext;
    int layoutResourceId;
    ListViewItems data[] = null;

    public ListViewItemsAdapter(Context mContext, int layoutResourceId, ListViewItems[] data) {

        super(mContext, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listItem = convertView;
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        listItem = inflater.inflate(layoutResourceId, parent, false);

        // get the elements in the layout
        TextView textControlNum   = (TextView) listItem
                .findViewById(R.id.txtControlNum);

        TextView textBizName = (TextView) listItem
                .findViewById(R.id.txtBizName);

        TextView textOwner = (TextView) listItem
                .findViewById(R.id.txtOwner);

        TextView textAdd = (TextView) listItem
                .findViewById(R.id.txtAdd);

        TextView textTel = (TextView) listItem
                .findViewById(R.id.txtTel);

        TextView textEmail = (TextView) listItem
                .findViewById(R.id.txtEmail);


        ListViewItems listViewItems = data[position];

        try {
            textControlNum.setText(listViewItems.ControlNum);
            textBizName.setText(listViewItems.BizName);
            textOwner.setText(listViewItems.Owner);
            textAdd.setText(listViewItems.Add);
            textTel.setText(listViewItems.Tel);
            textEmail.setText(listViewItems.Email);

        }
        catch (Exception e){}

        if(position % 2 == 0){
            listItem.setBackgroundColor(Color.rgb(243,251,254));
        }else{
            listItem.setBackgroundColor(Color.rgb(255,255,255));
        }   
        return listItem;
    }


}

【问题讨论】:

标签: android listview


【解决方案1】:

您可能需要覆盖适配器类中的过滤器。请参阅this SO 答案。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    首先,在行添加断点

     MyListActivity.this.lvListadapter.getFilter().filter(cs);
    

    查看事件是否已被文本更改触发。

    其次,正如您在 ArrayFilter 的源代码中看到的那样,它通过代码过滤项目

    if (valueText.startsWith(prefixString)) {
                            newValues.add(value);
                        } else {
                            final String[] words = valueText.split(" ");
                            for (String word : words) {
                                if (word.startsWith(prefixString)) {
                                    newValues.add(value);
                                    break;
                                }
                            }
                        }
    

    因此,如果一无所有,可能就是这样,应该一无所有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 2015-07-24
      相关资源
      最近更新 更多