java - 在 android 中搜索自定义 ListView ?

标签 java android listview filtering

我正在使用自定义 ListView ,并且在搜索时得到意外的结果。我理想情况下希望搜索列表中的特定列,例如我想搜索发票号码或联系号码或按姓名进行搜索人。 以下是我的代码。

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

我怎样才能实现它?如有任何建议或帮助,我们将不胜感激。谢谢。

最佳答案

在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();
        }

关于java - 在 android 中搜索自定义 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738478/

相关文章:

java - 正则表达式返回最后一个/和之间的数字?

android - 如何使用 MediaRecorder 在 Android 上捕获视频?

java - Main.this 修复此问题时遇到问题

Android SKIA 图像解码

java - 为什么我的自定义 ListView 不显示任何内容?

listview - TListItem.Checked在Assign()之后将自身切换为True

java - 从另一个类更改 JTextArea 值

java - 在 JUnit 测试中获取 JAR 列表

java - Docx4j - 更改 docDefaultStyle 的语言设置/将自定义(或更改的)库存样式正确应用到 docx 中的任何文本

android - 隐藏 ListView 行而没有间距