Search code examples
javaandroidlistviewsearchviewcustom-adapter

SearchView shows only one & same record from listView with CustomAdapter but searchView works fine with ArrayAdapter. Here's my adapter


import com.example.mis_internee.atendence_app_android.R;


    public class EMP_LEAVE_ADAPTER extends ArrayAdapter<String> {

        private final Activity context;
        private final String[] itemname;

        public EMP_LEAVE_ADAPTER(Activity context, String[] itemname) {
            super(context, R.layout.mylist, itemname);
            // TODO Auto-generated constructor stub

            this.context = context;
            this.itemname = itemname;


        }
        public View getView(int position, View view, ViewGroup parent) {
            LayoutInflater inflater=context.getLayoutInflater();
            View rowView=inflater.inflate(R.layout.emp_leave_adpater_layout, null,true);

            TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
            ImageView image=(ImageView)rowView.findViewById(R.id.icon);

            txtTitle.setText(itemname[position]);
            return rowView;

        };
    }

Solution

  • You need to return different view and also you need to recycle the view manually as follows:

     public View getView(int position, View view, ViewGroup parent) {
     ViewHolder viewHolder;
     if(view == null) {
       viewHolder = new ViewHolder();
       LayoutInflater inflater=context.getLayoutInflater();
       View rowView=inflater.inflate(R.layout.emp_leave_adpater_layout, null,true);
    
      viewHolder.txtTitle = (TextView) rowView.findViewById(R.id.item);
      viewHolder.image=(ImageView)rowView.findViewById(R.id.icon);
      view.setTag(viewHolder);
        } else {
          viewHolder = (ViewHolder) view.getTag();
        }   
      viewHolder.txtTitle.setText(itemname[position]);
    
      return viewHolder;
    };
    
    
    public class viewholder {
    
     public TextView txtTitle;
     public ImageView image;
    }