ListView Changing Items During Scroll

后端 未结 5 438
野性不改
野性不改 2021-01-13 15:05

I am implementing a ListFragment using a custom ArrayAdapter to populate the list. Each row item has an ImageView and three TextViews. Data is being parsed via XML and the i

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 15:33

    In my case revert back was forgotten in if else condition:

    /*public class ContactAdapter extends BaseAdapter {
        public List _data;
        private ArrayList arraylist;
        Context _c;
        ViewHolder holder;
    
    public ContactAdapter(List contacts, Context context) {
        _data = contacts;
        _c = context;
        this.arraylist = new ArrayList();
        this.arraylist.addAll(_data);
    }*/
    @Override
    public View getView(final int i,  View convertView, final ViewGroup viewGroup) {
        View view = convertView;
        final Contact data = (Contact) _data.get(i);
        ViewHolder holder ;
    
        if (view == null) {
            Context context = viewGroup.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.contact_list, null,false);
            holder = new ViewHolder();
            holder.title = (TextView) view.findViewById(R.id.name);
            holder.imageView = (ImageView) view.findViewById(R.id.pic);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
    
        holder.title.setText(data.getName());
        // Set image if is null string
        if(!data.getThumb().equals("null")){
            try {
                 Bitmap myBitmap = BitmapFactory.decodeFile(ImageStorage.getImage(data.getThumb()).getAbsolutePath());
                 holder.imageView.setImageBitmap(myBitmap);
                }catch (Exception e){e.printStackTrace();
                 holder.imageView.setImageResource(R.mipmap.contactimage);
              }
    
        }else // I had forgotten this else 
            holder.imageView.setImageResource(R.mipmap.contactimage); 
    
    
        // same action with background color
        if(data.getId()==data.getFromContact() ){
             view.setBackgroundColor(Color.RED);
         } else{
             view.setBackgroundColor(Color.WHITE); //**revert back it**
         }
            return view;
    }
    

    ViewHolder

      private  static class ViewHolder {
        ImageView imageView;
        TextView title;
    
    }
    

提交回复
热议问题