【问题标题】:Searching a custom list view in android?在android中搜索自定义列表视图?
【发布时间】:2016-08-12 19:37:26
【问题描述】:

我正在使用自定义列表视图,但我在搜索时得到了意想不到的结果。理想情况下,我希望搜索列表中的特定列,例如,我想搜索发票号码或联系电话或按名称人。 以下是我的代码。

   public void showJson(String json) {
        final Context billsctx = getActivity();
        ParseBills pb = new ParseBills(json);
        pb.parseJSON();

        bl = new BillsCustomList((Activity) billsctx, ParseBills.doc_no, ParseBills.date, ParseBills.cust_name, ParseBills.cust_number, ParseBills.item_count, ParseBills.total_wt,
                ParseBills.total,ParseBills.balance, ParseBills.bill_type);


        all_listView.setAdapter(bl);




        inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


                    bl.getFilter().filter(s);

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.length()==0)
                {
                    refreshFragment();
                }

            }
        });

}

这是我的适配器代码。

public class BillsCustomList extends ArrayAdapter<String>
{
    private String[] doc_no;
    private String[]date;
    private String[] cust_name;
    private String[] cust_number;
    private String[] item_count;
    private String[]total_wt;
    private String[]total;
    private String[]balance;
    private String[]bill_type;

    private Activity context;

    public BillsCustomList(Activity context, String[] doc_no, String[] date, String[] cust_name,String[] cust_number,String[] item_count,String[] total_wt,String[] total,String[] balance,String[] bill_type)
    {
        super(context, R.layout.bills_list_view, doc_no);
        this.context =context;
        this.doc_no=doc_no;
        this.date = date;
        this.cust_name = cust_name;
        this.cust_number = cust_number;
        this.item_count= item_count;
        this.total_wt = total_wt;
        this.total = total;
        this.balance = balance;
        this.bill_type = bill_type;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.bills_list_view, null, true);
        TextView textViewDocno = (TextView) listViewItem.findViewById(R.id.textViewInvNo);
        TextView textViewDt = (TextView) listViewItem.findViewById(R.id.textViewDt);
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
        TextView textViewNumber = (TextView) listViewItem.findViewById(R.id.textViewNumber);
        TextView textViewCount = (TextView) listViewItem.findViewById(R.id.textViewCount);
        TextView textViewTotwt = (TextView) listViewItem.findViewById(R.id.textViewTotwt);
        TextView textViewTot = (TextView) listViewItem.findViewById(R.id.textViewTot);
        TextView textViewBal = (TextView) listViewItem.findViewById(R.id.textViewBalanace);
        TextView textViewBt = (TextView) listViewItem.findViewById(R.id.textViewBt);

        textViewDocno.setText(doc_no[position]);
        textViewDt.setText(date[position]);
        textViewName.setText(cust_name[position]);
        textViewNumber.setText(cust_number[position]);
        textViewCount.setText(item_count[position]);
        textViewTotwt.setText(total_wt[position]);
        textViewTot.setText(total[position]);
        textViewBal.setText(balance[position]);
        textViewBt.setText(bill_type[position]);

        return listViewItem;
    }
}

我怎样才能实现它?任何建议或帮助表示赞赏。谢谢。

【问题讨论】:

  • 扩展 BaseAdapter(不是 ArrayAdapter)并实现您自己的过滤器(覆盖 getFilter()),并在该过滤器中简单地针对您想要包含在搜索中的任何列的值测试查询字符串。跨度>
  • 本教程会帮助你androidbegin.com/tutorial/…
  • @Luksprog 你能给我一个代码示例吗?我是编程新手。谢谢。

标签: java android listview filtering


【解决方案1】:

在 ListView 类中:

SearchView mSearch;
mSearch.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                mAdapter.filter(newText);
                return false;
            }

        });

在适配器类中:

private List<ModelClass > mArrayList= new ArrayList<ModelClass >();
private ArrayList<ModelClass > arraylist;

    public void filter(String targetText) {

            targetText= targetText.toLowerCase(Locale.getDefault());
            mArrayList.clear();

            if (targetText.length() == 0) {
                mArrayList.addAll(arraylist);
            } else {
                for (ModelClass mModel: arraylist) {
                        if (mModel.getInvoiceNum()
                    .toLowerCase(Locale.getDefault()).startsWith(targetText)) {
                        mArrayList.add(mModel);
                    }
                }
            }
            notifyDataSetChanged();
        }

【讨论】:

  • 这里的ModelClass是什么?
  • 你能具体说明你想在这里做什么吗?
  • BillObject 是模型类
【解决方案2】:

首先,我认为你需要改变你的数据类型,可能你可以使用一个对象包含它们,并覆盖 toString() 方法,该方法是你的过滤器值,像这样:

public class BillObject {
    public String doc_no;
    public String date;
    public String cust_name;
    public String cust_number;
    public String item_count;
    public String total_wt;
    public String balance;
    public String bill_type;

    @Override
    public String toString() {
        // if you want to search by name
        return cust_name;
    }
}

然后,在您的适配器中,使用 List 作为数据源,并且不必分别创建 View,如下所示:

public class BillsCustomList extends ArrayAdapter<String>
{
    private List<BillObject> dataList;
    private Activity context;

    public BillsCustomList(Activity context, List<BillObject> dataList)
    {
        super(context, R.layout.bills_list_view, doc_no);
        this.context =context;
        this.dataList = dataList;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        if(convertView == null)
        {
            convertView= inflater.inflate(R.layout.bills_list_view, null, true);
        }
        // find view by id
        ...code

        // get data from dataList by position
        BillObject billObject = dataList.get(position);
        textViewDocno.setText(billObject.doc_no);
        textViewDt.setText(billObject.date);
        // like this do more

        return listViewItem;
    }

}

最后,在 afterTextChanged() 中使用适配器的过滤器。 如果想按规则搜索,可以循环dataList,通过searchKey获取数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2012-12-17
    • 2023-03-09
    相关资源
    最近更新 更多