java - 仅在 android 的自定义 ListView 中搜索特定值的结果?

标签 java android android-arrayadapter

我正在使用自定义 ListView ,我想搜索特定值。现在我认为我的代码搜索是针对与输入的文本匹配的任何内容。我只想搜索 doc_no。 以下是我的代码。

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

感谢任何建议或帮助。谢谢。

最佳答案

尝试像这样实现自定义过滤器:

 @Override
public Filter getFilter() {
    Filter filter=new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
           FilterResults results=new FilterResults();
            if(constraint!=null)
            {
              places=getPredictions(constraint.toString());
                if(places!=null)
                {
                    results.values=places;
                    results.count=places.size();
                }
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                // The API returned at least one result, update the data.
                notifyDataSetChanged();
            } else {
                // The API did not return any results, invalidate the data set.
                notifyDataSetInvalidated();
            }
        }
    };
    return filter;
}

此示例来自:- http://coderzpassion.com/android-working-google-places-api/但你可以尝试根据要求修改它。谢谢。希望对您有帮助

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

相关文章:

java - 循环遍历一个数组

java - CipherOutputStream 和 FileOutputStream(someFile, true) 在追加数据时产生垃圾

android - 使用 ArrayAdapter 在 ListView 中编辑文本

android - popBackStackImmediate 在当前 fragment 事务未添加到后台堆栈时不显示 fragment

android - Android/iOS 上的 Base64 编码图像

android - 在 ListView 上长按打开上下文菜单

android - 更改 ArrayAdapter 的值

java - 将用户的字符串输入与字符串数组进行比较

Java 正则表达式来匹配模式

java - AsyncTask 和 Android 线程 : how to get it right ?