searchview for multicolum listview

时间:2017-12-15 05:38:54

标签: android listview searchview

我正在使用ListViewAdapter扩展ArrayAdapter以在ListView中显示结果。我在TextView中有3 ListView来显示输出。我想实现一个搜索视图,但当我在搜索栏中输入时,屏幕变为空白。我该如何实现它?我尝试使用addTextChangedListener使用EditText,但没有输出。这是Activity

 searchView = (SearchView)findViewById(R.id.searchView);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

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

}

private void loadDescriptionList() {
    final ProgressDialog progressDialog = new ProgressDialog(TestDetails.this);
    progressDialog.setMessage("Loading Data...");
    progressDialog.show();
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response)
                {
                    progressDialog.dismiss();

                    try {
                        JSONObject obj = new JSONObject(response);
                        JSONArray heroArray = obj.getJSONArray("list");

                        for (int i = 0; i < heroArray.length(); i++) {
                            JSONObject heroObject = heroArray.getJSONObject(i);
                            TDescription hero = new TDescription(heroObject.getString("test_code"), heroObject.getString("test_description"),heroObject.getString("test_price"));
                            heroList.add(hero);
                        }
                         adapter = new ListViewAdapter(heroList, getApplicationContext());
                        listView.setAdapter(adapter);
                    } catch (JSONException e) {
                        progressDialog.dismiss();
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

LISTVIEWADAPTER.JAVA

public class ListViewAdapter extends ArrayAdapter<TDescription> {
private List<TDescription> heroList;
private Context mCtx;
Typeface typeface;
TextView rup;
public ListViewAdapter(List<TDescription> heroList, Context mCtx) {
    super(mCtx, R.layout.list_items, heroList);
    this.heroList = heroList;
    this.mCtx = mCtx;
    typeface = Typeface.createFromAsset(mCtx.getAssets(), "fontawesome-webfont.ttf");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View listViewItem = inflater.inflate(R.layout.list_items, null, true);

    TextView code = (TextView)listViewItem.findViewById(R.id.code);
    TextView desc = (TextView)listViewItem.findViewById(R.id.desc);
    TextView price = (TextView)listViewItem.findViewById(R.id.price);
    rup = (TextView)listViewItem.findViewById(R.id.rupee);
    rup.setTypeface(typeface);
    TDescription hero = heroList.get(position);
    code.setText(hero.getCode());
    desc.setText(hero.getDesc());
    price.setText(hero.getPrice());
    return listViewItem;
}

} enter image [description] 1此处

1 个答案:

答案 0 :(得分:1)

您必须实现可过滤的界面才能获得结果,只需将listview适配器类更改为以下代码

public class ListViewAdapter extends ArrayAdapter<TDescription> implements Filterable {
private List<TDescription> heroList;
private List<TDescription> mFilterData;
private Context mCtx;
private ItemFilter mFilter = new ItemFilter();
private TextView rup;

public ListViewAdapter(List<TDescription> heroList, Context mCtx) {
    super(mCtx, R.layout.list_items, heroList);
    this.heroList = heroList;
    this.mFilterData = heroList;
    this.mCtx = mCtx;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View listViewItem = inflater.inflate(R.layout.list_items, null, true);

    TextView code = (TextView) listViewItem.findViewById(R.id.code);
    TextView desc = (TextView) listViewItem.findViewById(R.id.desc);
    TextView price = (TextView) listViewItem.findViewById(R.id.price);
    rup = (TextView) listViewItem.findViewById(R.id.rupee);
    TDescription hero = mFilterData.get(position);
    code.setText(hero.getCode());
    desc.setText(hero.getDesc());
    price.setText(hero.getPrice());
    return listViewItem;
}


public int getCount() {
    return mFilterData.size();
}

public TDescription getItem(int position) {
    return mFilterData.get(position);
}

public long getItemId(int position) {
    return position;
}

@NonNull
public Filter getFilter() {
    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String userString = constraint.toString().toLowerCase();

        FilterResults filterResults = new FilterResults();

        final List<TDescription> originalList = heroList;

        int count = originalList.size();
        final ArrayList<TDescription> resultList = new ArrayList<>(count);

        TDescription tDescription;

        for (int i = 0; i < count; i++) {
            tDescription = originalList.get(i);
            if (tDescription.getDesc().toLowerCase().contains(userString)) {
                resultList.add(tDescription);
            }
        }

        filterResults.values = resultList;
        filterResults.count = resultList.size();

        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mFilterData = (ArrayList<TDescription>) results.values;
        notifyDataSetChanged();
    }

}

}

它会根据您的描述过滤数据