我有一个商家列表,我正在应用过滤器来搜索地址或名称,过滤器工作正常。它给出了适当的过滤结果列表。但是,如果我再次清空编辑文本,我应该看到原始列表,但我看到的计数与原始列表不同。
示例,在列表中我有7个商家,我会对其进行过滤,并在列表中显示两个过滤的商家,现在如果我清空编辑文本,我只看到5个商家,无法看到7个商家的原始列表。
适配器:
public class SearchedMerchantsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
static final int LOAD_MERCHANTS = 0;
private Context context;
public ArrayList<Merchants> list;
public ArrayList<Merchants> baselist = new ArrayList<>();
public SearchedMerchantsAdapter(Context context, ArrayList<Merchants> list) {
this.context = context;
this.list = list;
this.baselist = list;
}
@Override
public int getItemViewType(int position) {
Object obj = list.get(position);
if (obj instanceof Merchants) return LOAD_MERCHANTS;
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = holder.getItemViewType();
switch (viewType) {
case LOAD_MERCHANTS:
LoadSearchedMerchants loadSearchedMerchants = (LoadSearchedMerchants) holder;
retriveAllMerchants(loadSearchedMerchants, position);
break;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (viewType) {
case LOAD_MERCHANTS:
View v_image_msg = inflater.inflate(R.layout.search_merchant_layout, parent, false);
viewHolder = new LoadSearchedMerchants(v_image_msg);
break;
}
return viewHolder;
}
@Override
public int getItemCount() {
return list.size();
}
public void retriveAllMerchants(final LoadSearchedMerchants holder, int position) {
final Merchants data = (Merchants) list.get(position);
holder.tv_kiarana_name.setText(data.getKirana_name());
holder.tv_address.setText(data.getMerchant_address());
holder.lay_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, MerchantProfileActivity.class);
i.putExtra("cont_name",data.getMerchant_name());
i.putExtra("kirana_name",data.getKirana_name());
i.putExtra("email_id",data.getEmail_id());
i.putExtra("phone_no",data.getPhone());
i.putExtra("address",data.getMerchant_address());
context.startActivity(i);
}
});
holder.btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final SessionData sessionData;
final String sessionUserId,access_token;
sessionData=new SessionData(context);
sessionUserId = sessionData.getString("user_id","-1");
access_token = sessionData.getString("api_key", "-1");
new SendRequestAsyncTask(context).execute(sessionUserId,access_token,data.getMerchant_id().toString());
}
});
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Merchants> results = new ArrayList<>();
if (constraint != null) {
if (baselist != null && baselist.size() > 0) {
for (final Merchants g : baselist) {
if (g.getKirana_name()
.contains(constraint.toString()) || g.getMerchant_address().contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
list = (ArrayList<Merchants>) results.values;
notifyDataSetChanged();
}
};
}
}
这可能是什么原因?谢谢..
答案 0 :(得分:0)
你能试试吗?
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Merchants> results = new ArrayList<>();
if (charSequence != null && charSequence.length() > 0) {
if (baselist != null && baselist.size() > 0) {
for (final Merchants g : baselist) {
if (g.getKirana_name()
.contains(constraint.toString()) || g.getMerchant_address().contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
oReturn.count = results.size();
}
else
{
oReturn.values = baselist;
oReturn.count = baselist.size();
}
return oReturn;
}
<强>更新强>
在适配器中创建一个方法..注释掉getFilter()
和界面Filterable
public void filterData(String compareString)
{
list.clear();
if (baselist != null && baselist.size() > 0) {
for (final Merchants g : baselist) {
if (g.getKirana_name()
.contains(compareString) || g.getMerchant_address().contains(compareString))
list.add(g);
}
}
notifyDataSetChanged();
}
最后通过edittext
更改侦听器
喜欢adapter.filterData("StringwhichisChangedFromEdittext")
希望得到这个帮助。
答案 1 :(得分:0)
在适配器中创建此方法:
public Item getItemAtPosition(int position)
{
return list.get(position);
}
现在,当发生点击操作时,只需调用此方法:
adapter.getItemAtPosition(position).getMerchant_name()
答案 2 :(得分:0)
如果用户没有输入任何内容,则返回整个列表
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Merchants> results = new ArrayList<>();
if(TextUtils.isEmpty(constraint)){
oReturn.values = baselist==null?new ArrayList() : baselist ;
return oReturn.values ;
}
if (constraint != null) {
if (baselist != null && baselist.size() > 0) {
for (final Merchants g : baselist) {
if (g.getKirana_name()
.contains(constraint.toString()) || g.getMerchant_address().contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
list = (ArrayList<Merchants>) results.values;
notifyDataSetChanged();
}
};
}