1

I am trying to load images dynamically in a list view in android. The code below loads the image according to the viewed position and loads the image in that array position. However, as it gets the data in background, sometimes the image in the first array is loaded in the second imageview position, second in third etc. I guess when the getDataInBackground part of the first item in the array is already finished the sytem is trying to create the second cell at that time and loads it to the second cell. How can I handle this, I am using android studio AVD with version kitkat and nexus 5 emulator.

private class MyListAdapter extends ArrayAdapter<String>  {
    public MyListAdapter() {
        super(getActivity().getApplicationContext(), R.layout.fragment_users_cell, myItemList);
    }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View cellView = convertView;

            if (cellView == null){
                cellView = getActivity().getLayoutInflater().inflate(R.layout.fragment_users_cell, parent, false);
            }

            cellProfileImage = (ImageView) cellView.findViewById(R.id.fragment_users_cell_profileImg);

            System.out.println("The current position" + position);

            if (resultsImageFiles.get(position)==null) {

                Bitmap image = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ph2);
                cellProfileImage.setImageBitmap(image);

            } else {
                ParseFile file = resultsImageFiles.get(position);
                file.getDataInBackground(new GetDataCallback() {
                    @Override
                    public void done(byte[] data, ParseException e) {

                        if (e == null) {

                            Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
                            cellProfileImage.setImageBitmap(image);
                        }

                    }
                });
            }


            return cellView;

        }

    }
saner
  • 821
  • 2
  • 10
  • 32

2 Answers2

0

Listview reuse convertview --> Some itemView has same value for view. In this case, you have to remove cellProfileImage before Bitmap loaded.

Brian Hoang
  • 1,111
  • 9
  • 18
  • Hello Hoang i could not understand your aswer very well, may you add an example? – saner May 06 '16 at 15:20
  • // clear image bitmap cellProfileImage.setImageBitmap(null); ParseFile file = resultsImageFiles.get(position); – Brian Hoang May 06 '16 at 15:57
  • Hello Hoang this does not solve the problem. It makes some imageViews invisible (which could not be loaded yet) and some visible. – saner May 06 '16 at 16:14
0

try to remove all cellProfileImage.setImageBitmap(image); and move it above return cellView; don't forget to declare Bitmap image after View cellView = convertView; but you can also create a class viewHolder that contain your imageview like this link:http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

user192417
  • 56
  • 1
  • 7