[英]listview custom from json getfilter
在我的應用程序中,我創建了一個自定義列表視圖,並且想要實現一個過濾器,以便可以根據在EditText中輸入的文本來過濾列表。 我使用BaseAdapter作為單獨的類。
這是我的適配器:
公共類MyAdapter擴展BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
public HashMap<String, String> resultp;
public AllProductAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView namaBarang;
ImageView image;
TextView harga;
ImageButton cart;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_viewitem, parent,
false);
// Get the position from the results
resultp = new HashMap<String, String>();
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
namaBarang = (TextView) itemView.findViewById(R.id.judul);
cart = (ImageButton) itemView.findViewById(R.id.cart);
harga = (TextView) itemView.findViewById(R.id.posterFile);
image = (ImageView) itemView.findViewById(R.id.poster);
// Capture position and set results to the TextViews
namaBarang.setText(resultp.get(AllProductAgent.TITLE));
harga.setText(resultp.get(AllProductAgent.HARGA),
TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) harga.getText();
spannable.setSpan(new StrikethroughSpan(), 0,
resultp.get(AllProductAgent.HARGA).length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class to download and cache
// images
imageLoader
.DisplayImageList(resultp.get(AllProductAgent.GAMBAR), image);
// Link to Register Screen
itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
// Add ArrayList
cart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Get the position from the results
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = data.get(position);
// show toast if item was been added to cart
Toast.makeText(
context,
resultp.get(AllProductAgent.TITLE)
+ " has been added to Cart", Toast.LENGTH_LONG)
.show();
HashMap<String, String> map = new HashMap<String, String>();
map.put("nama", resultp.get(AllProductAgent.TITLE));
map.put("harga", resultp.get(AllProductAgent.HARGA));
map.put("link_gambar", resultp.get(AllProductAgent.GAMBAR));
map.put("website", resultp.get(AllProductAgent.WEBSITE));
BaseActivity.cartList.add(map);
}
});
itemView.setBackgroundColor(position % 2 == 0 ? Color
.parseColor("#c7eaf8") : Color.parseColor("#FFFFFF"));
return itemView;
}
我該如何過濾我的應用程序? 我正在嘗試太多代碼,但仍然無法獲取它。
謝謝
在Searchedittext中設置TextChangedListener
Searchtxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
ListAdapterview.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
然后在適配器類中實現“可過濾”
然后添加此代碼
@SuppressLint("DefaultLocale")
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final ArrayList<HashMap<String, String>> list = data;
int count = list.size();
final ArrayList<HashMap<String, String>> filtereddata = new ArrayList<HashMap<String, String>>();
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = list.get(i).get("ur item name");
if (filterableString.toString().toLowerCase().contains(filterString)) {
filtereddata.add(list.get(i));
}
}
results.values = filtereddat;
results.count = filtereddat.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null) {
if (results.count > 0) {
data = new ArrayList<HashMap<String,String>>((ArrayList<HashMap<String, String>>) results.values) ;
}
}
notifyDataSetChanged();
}
}
假設您有一個id為“ field1”的EditText
Field1 = (EditText)findViewById(R.id.field1);
Field1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
/* WORK HERE TO FILTER */
}
});
現在,在onTextChanged方法中,您可以以已知的最佳方式(可以使用自定義函數或實現Filter)過濾數據(數據必須是適配器使用的ArrayList實例),然后將適配器刷新為刷新並過濾列表。
public void onTextChanged(CharSequence s, int start, int before, int count) {
/* WORK HERE TO FILTER */
doFilter(data);
MyAdapterInstance.notifyDataSetChanged();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.