I have a GridView with photos (photos are loaded in AsyncTask), there is a CheckBox near each photo, when the user ticks the CheckBox, it is displayed not only in the cell where he clicked, but also in the row that is in six lines below (this is seen when scrolling). I can not understand what the problem is and how to solve it.

https://www.youtube.com/watch?v=pNI14Zfj_N4&feature=youtu.be

Activity code:

public class ChooseActivity extends AppCompatActivity { private ArrayList listFile; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.choose_grid); Intent intent = getIntent(); listFile = (ArrayList) intent.getSerializableExtra("LIST_FILES"); GridChooseAdapter adapter = new GridChooseAdapter(this, listFile); GridView gridView = (GridView) findViewById(R.id.gridChooseView); gridView.setAdapter(adapter); } } 

Adapter Code:

 public class GridChooseAdapter extends BaseAdapter { LayoutInflater inflater; ArrayList items; public GridChooseAdapter(Context context, ArrayList items) { this.items = items; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.choose_item, null); viewHolder = new ViewHolder(); viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView); viewHolder.imageView.setImageBitmap(null); convertView.setTag(viewHolder); } else{ viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.imageView.setImageBitmap(null); File image = new File(String.valueOf(items.get(position))); BitmapFactory.Options bmOptions = new BitmapFactory.Options(); final String imageKey = String.valueOf(items.get(position)); ChooseDownloadTask task = new ChooseDownloadTask(viewHolder.imageView); task.execute(imageKey); return convertView; } static class ViewHolder { ImageView imageView; } } 

AsyncTask code:

 public class ChooseDownloadTask extends AsyncTask<String, Void, Bitmap> { private final WeakReference<ImageView> viewHolderWeakReference; private String data = null; private ImageView imageView; public ChooseDownloadTask(ImageView imageView) { viewHolderWeakReference = new WeakReference<ImageView>(imageView); } @Override protected Bitmap doInBackground(String... params) { data = String.valueOf(params[0]); final Bitmap bitmap = decodeSampledBitmapFromResource(data, 100, 100); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { imageView = viewHolderWeakReference.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); } } } 

xml gridview:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/GridLayout1" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#118b0a" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <GridView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/gridChooseView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/toolbar" android:columnWidth="150dp" android:gravity="center" android:numColumns="auto_fit" android:stretchMode="columnWidth"></GridView> </RelativeLayout> 

xml item in GridView:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="150dp" android:layout_height="150dp" xmlns:app="http://schemas.android.com/apk/res-auto" android:padding="5dp"> <ImageView android:id="@+id/imageView" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop"> </ImageView> <android.support.v7.widget.AppCompatCheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" app:buttonTint="@color/white" /> </RelativeLayout> 
  • one
    This is a long-known "problem". The answer is RecyclerView , but the widget type is not important here. A detailed answer to why this happens - pavlofff
  • @pavlofff Thank you! - Lucky_girl

0