ListView在所有行中重复一些ImageView

时间:2014-04-17 17:20:32

标签: android listview android-fragments android-listview baseadapter

我发现了很多围绕这个主题的问题,但没有人可以帮助我。所以就这样了。

我正在使用此BaseAdapter构建ListView

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;
}

}

Array People []是在片段上创建的,每个条目都不同[我检查过],但由于某种原因,当我运行代码时,代码的某些元素不是。

所有ImageViews cell1到cell9仅显示最后一个条目的图像。

如果需要更多代码或信息,请随时提问。

提前致谢。


解决

问题出在数组的创建上,这个东西非常小,与适配器无关。

1 个答案:

答案 0 :(得分:0)

您的代码似乎可以像:

  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);

例如来自您的代码          您的代码应该是初始化视图ID到convertview == null condtion是否满足

  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]);