Question

I found a lot of questions around this topic, but none could help me. So here it goes.

I'm building a ListView using a this BaseAdapter

public class ListAdapter extends BaseAdapter {

Context context;
int layout;
Person[] mpeople;

public PostListAdapter(Context context,int layout, Person[] people){
    this.context=context;
    this.layout=layout;
    this.mpeople=people;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v=convertView;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(layout, parent, false);
    }

    Person swig=mpeople[position];

    ImageView profilepic= (ImageView) v.findViewById(R.id.profilepic);
    profilepic.setImageResource(swig.getUser().getProfilepic());
    TextView username= (TextView) v.findViewById(R.id.user_name);
    username.setText(swig.getUser().getUsername());
    TextView timestamp= (TextView) v.findViewById(R.id.time);
    timestamp.setText(Integer.toString(swig.getTimestamp()));
    TextView privacy= (TextView) v.findViewById(R.id.privacy);
    privacy.setText(swig.getPrivacy());
    TextView hashtag= (TextView) v.findViewById(R.id.hashtagtext);
    hashtag.setText(swig.getHashtag());
    TextView n_likes= (TextView) v.findViewById(R.id.likenumber);
    n_likes.setText(Integer.toString(swig.getN_likes()));
    TextView n_comments= (TextView) v.findViewById(R.id.commentnumber);
    n_comments.setText(Integer.toString(swig.getN_comments()));

    ImageView cell1= (ImageView) v.findViewById(R.id.cell1);
    cell1.setImageResource(swig.getPics()[0]);
    ImageView cell2= (ImageView) v.findViewById(R.id.cell2);
    cell2.setImageResource(swig.getPics()[1]);
    ImageView cell3= (ImageView) v.findViewById(R.id.cell3);
    cell3.setImageResource(swig.getPics()[2]);
    ImageView cell4= (ImageView) v.findViewById(R.id.cell4);
    cell4.setImageResource(swig.getPics()[3]);
    ImageView cell5= (ImageView) v.findViewById(R.id.cell5);
    cell5.setImageResource(swig.getPics()[4]);
    ImageView cell6= (ImageView) v.findViewById(R.id.cell6);
    cell6.setImageResource(swig.getPics()[5]);
    ImageView cell7= (ImageView) v.findViewById(R.id.cell7);
    cell7.setImageResource(swig.getPics()[6]);
    ImageView cell8= (ImageView) v.findViewById(R.id.cell8);
    cell8.setImageResource(swig.getPics()[7]);
    ImageView cell9= (ImageView) v.findViewById(R.id.cell9);
    cell9.setImageResource(swig.getPics()[8]);

    return v; 
}

@Override
public int getCount() {
    return mpeople.length;
}

@Override
public Object getItem(int arg0) {
    return mpeople[arg0];
}

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

}

The Array People[] is created on a fragment and every entry is different [it is, I checked], but for some reason when I run the code some elements of the code aren't.

All the ImageViews cell1 to cell9 only show the images of the last entry.

If any more code or info is needed feel free to ask.

Thanks in advance.


SOLVED

The problem was in the creation of the array,something really minor and nothing to do with the adapter.

Was it helpful?

Solution

Your code seem could work like:

  if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(layout, parent, false);
 }
 /*
 ** you intitalize your findeviewbyid after when your view not getting null So its 
   recreate your inflatore view  this is not corect way
 *
  */

ImageView profilepic= (ImageView) v.findViewById(R.id.profilepic);

e.g from your code Your code should be intialize view id into convertview==null condtion is met

  Person swig=mpeople[position];
  if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(layout, parent, false);
    ImageView profilepic= (ImageView) v.findViewById(R.id.profilepic);
    ImageView cell2= (ImageView) v.findViewById(R.id.cell2);
}
 cell1.setImageResource(swig.getPics());
 cell2.setImageResource(swig.getPics()[1]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top