Search code examples
javaandroidlistviewandroid-adapterview

ListView displays data from last passes as well as current pas


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

Solution

  • 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?