gpt4 book ai didi

android - 来自 json getfilter 的 ListView 自定义

转载 作者:行者123 更新时间:2023-11-30 02:35:17 24 4
gpt4 key购买 nike

在我的应用程序中,我创建了一个自定义 ListView ,我想实现一个过滤器,以便可以根据在 EditText 中输入的文本来过滤列表。我将 BaseAdapter 用作单独的类。

这是我的适配器:

公共(public)类 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) {
}
});

然后在适配器类中实现“Filterable”

然后添加这段代码

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

}

关于android - 来自 json getfilter 的 ListView 自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26673166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com