searchview for multicolum listview



我正在使用一个ListViewAdapter,它扩展ArrayAdapter以显示ListView的结果。我在ListView中有 3 个TextView来显示输出。我想实现搜索视图,但是当我在搜索栏中输入时,屏幕变为空白。我该如何实现它?我也尝试使用EditTextaddTextChangedListener但没有输出。这是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);
}

列表视图适配器。.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;
}

} 在此处输入图像 [描述]1

您必须实现可过滤的接口才能获得结果,只需将列表视图适配器类更改为以下代码即可

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

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

相关内容

  • 没有找到相关文章

最新更新