user1683272
user1683272

Reputation: 33

Android ListView Base Adapter Getting the wrong position

I have a ListView with Custom ListView items in it!

Picture --> http://imageshack.us/photo/my-images/155/08pictureslistcopy.png/

To display the right Picture in the Listview i have a ArrayList with the picture_ID in the right order for the list view.

I also have created a class called AttachmentAdapter which extends BaseAdapter. In the getView method i get the right picture_Id out from the ArrayList and display the item.

I have debugged the process from getting the picture_ID from the ArrayList, this workes right :-)

My Problem is after I have 6 or more entrys in my listview the getView method starts to give me the wrong position of the items. So i get the wrong picture displayed at the wrong position in my list.

Can anyone tell me why the BaseAdapters getView method gets the false position passed?

Here is my Code of the BaseAdapter:

 private class AttachmentAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        private ArrayList<Data_Attachment> mData = new ArrayList<Data_Attachment>();

        public AttachmentAdapter(Context fragment) {
         //   super(fragment, textViewResourceId, items);
            mInflater = LayoutInflater.from(fragment);
        }

        public void setData(ArrayList<Data_Attachment> data)
        {
            mData = data;
        }

        private Data_Attachment getAttachmentfromPosition(int pos)
        {
            return DataResources.getAttachment(position_list.get(pos));
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            Data_Attachment data = getAttachmentfromPosition(position);      


            ViewHolder holder = null;

         Log.e("TEST","getView " + position + ", Attachment: #" + data.getID() + " --> " + data.getBriefDescr());

            if (convertView == null) {
                 convertView = mInflater.inflate(R.layout.listpictures_item, null);

                holder = new ViewHolder();
                holder.name= (TextView) convertView.findViewById(R.id.idPictureName);
                holder.date= (TextView) convertView.findViewById(R.id.idPictureDate);
                holder.uploader= (TextView) convertView.findViewById(R.id.idPictureUploader);

                holder.icon = (ImageView) convertView.findViewById(R.id.idListPicture_Preview);

                try{
                        Bitmap thumbnail = data.getThumbnail(getApplicationContext());

                        if(thumbnail != null)
                        {
                            holder.icon.setImageBitmap(thumbnail);
                        }
                        else
                        {
                              holder.icon.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
                        }      
                }
                catch(Exception e)
                {
                    Log.e("TEST","PicturesIncident_Fragment.AttachmentData.getView, Error while showing picture: " + e.toString());
                      holder.icon.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
                }

                String name = data.getBriefDescr();

                if(name.length() == 0)
                {
                    holder.name.setVisibility(View.GONE);
                    holder.uploader.setTextSize(14);
                    holder.uploader.setTypeface(Typeface.DEFAULT_BOLD);
                }
                else
                {
                    //Kürzen nach 40 Strings
                    if(name .length() >= _Settings.MAX_ATTACHMENT_NAME_SIZE)
                    {
                        char[] buf = new char[_Settings.MAX_ATTACHMENT_NAME_SIZE];
                        name.getChars(0, _Settings.MAX_ATTACHMENT_NAME_SIZE-1, buf, 0);
                        name = String.valueOf(buf) +"...";
                    }
                    holder.name.setText(name);
                }

                //Set uploader and Date
                Data_Worker data_worker = DataResources.getWorkerData_fromID(data.getuploaderID()); 
                String uploader = data_worker.getName();

                String date = data.getModified_str();

                holder.uploader.setText(uploader);

                //TODO change to view the date
                // holder.date.setText(date);
                holder.date.setText("pos: "+ position + " , #" + data.getID());

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder)convertView.getTag();
            }               

            return convertView;
        }

        class ViewHolder {
            TextView name;
            TextView uploader;
            TextView date;

            ImageView icon;
        }


        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Data_Attachment getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

For testing purpose I displayed, as you can see in the code and in the screenshot below, the current position of this item in the listview.

picture --> http://imageshack.us/photo/my-images/805/device20121010155051.png/

Here you can see that the position 0 is not on the top of the list.

Futhermore, after I am scrolling a bit up and down the positions will change, I can´t figure out any logic behind the position changes.

PS: Tested only on device yet (Samsung Galaxy S2 - Android 4.0.4)

Upvotes: 0

Views: 3566

Answers (1)

toadzky
toadzky

Reputation: 3846

It looks like you are only updating the content of a row if convertView is null. You pull the tag out but you never do anything with it. If the passed in view is null, create a new view, setup the holder, and end the if. All of your content assignment should be AFTER that if-else statement.

So do it like this:

ViewHolder holder = null;
if (convertView == null) {
    // create new view
    // setup holder
}
else {
    holder = (ViewHolder) convertView.getTag();
}
try {
    // set values on the holder
}
catch (Exception e) { }
return convertView;

Upvotes: 1

Related Questions