ListView displays data from last passes as well as current pas
-
02-07-2021 - |
Pregunta
I have a small "search" implementation I am working on. A listView displays the items if they match the query. This works fine, but whenever the menu is hidden and re-displayed and a new query is made, all the "searchables" continue to stack up. If i have 4 that should be displaying, first it would show 4, then 8, then 12, etc.
private class Adapter extends BaseAdapter {
private LayoutInflater inflater;
public Adapter(){
inflater = LayoutInflater.from(getContext());
}
@Override
public int getCount() {
return menuItems.size();
}
@Override
public Object getItem(int position) {
return menuItems.get(position).text;
}
@Override
public long getItemId(int position) {
return menuItems.get(position).id;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder = new ViewHolder();
if (!isSearching) {
convertView = inflater.inflate(R.layout.rbm_item, null);
} else {
convertView = inflater.inflate(R.layout.rbm_search_item, null);
}
holder.subMenuList = (LinearLayout) convertView.findViewById(R.id.sub_holder);
holder.text = (TextView) convertView.findViewById(R.id.rbm_item_text);
convertView.setTag(holder);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (holder.subMenuList.getVisibility() == View.VISIBLE) {
holder.subMenuList.setVisibility(View.GONE);
} else {
holder.subMenuList.setVisibility(View.VISIBLE);
}
}
});
if (isSearching) {
holder.image = (ImageView) convertView.findViewById(R.id.rbm_item_icon);
holder.image.setImageResource(menuItems.get(position).icon);
holder.chapterId = (TextView) convertView.findViewById(R.id.id_text);
holder.chapterId.setText("Ch"+Integer.toString(menuItems.get(position).id));
} else {
holder.chapterId = (TextView) convertView.findViewById(R.id.rbm_item_id);
holder.chapterId.setText(Integer.toString(menuItems.get(position).id));
}
holder.text.setText(menuItems.get(position).text);
if (!isSearching) {
for (int i=0;i<menuItems.get(position).subItems.size();i++) {
TextView tv = new TextView(ctx);
tv.setTextColor(0xFF893658);
tv.setText(menuItems.get(position).subItems.get(i).getTitle());
tv.setCompoundDrawablesWithIntrinsicBounds(menuItems.get(position).subItems.get(i).getIcon(), 0, 0, 0);
tv.setCompoundDrawablePadding(5);
if (i == 0) {
if (menuItems.get(position).subItems.size() == 1) {
tv.setPadding(20, 20, 0, 20);
} else{
tv.setPadding(20, 20, 0, 10);
}
} else if (i == menuItems.get(position).subItems.size()-1){
tv.setPadding(20, 10, 0, 20);
} else {
tv.setPadding(20, 10, 0, 10);
}
holder.subMenuList.addView(tv);
}
}
return convertView;
}
class ViewHolder {
TextView text;
ImageView image;
LinearLayout subMenuList;
TextView chapterId;
}
}
Solución
try to clear the listitems that are in the menuItems and then add the items back after the new query is made. Is the try to clear the listitems that are in the menuItems and then add the items back after the new query is made a arraylist or something?
Otros consejos
Hard to tell for sure from what you posted, but I'd guess that you're appending the new results to menuItems rather than replacing it with your new data.
I didn't understand your problem much. but by just glancing over your code i see one issue
if (!isSearching) {
convertView = inflater.inflate(R.layout.rbm_item, null);
} else {
convertView = inflater.inflate(R.layout.rbm_search_item, null);
}
This piece can be the cause of your problem. getView method of list adaptor gets called very frequently and so it should contain minimum and less heavy operation.
If you will going to check the default examples of list adapters. you will going to see the convertView is checked against null so that it need not to be inflated again and is reused again for faster preparation of view to be displayed in the list. And this is not happening in your code.
Google about Efficient Adapter Example Android.