滚动Listview时,自定义Listview中的图像集未设置

时间:2015-07-11 12:47:27

标签: android android-listview

Listview有一个默认图像,当我选择图像时,我用另一个图像替换它(如下所示 - onClickListener)。问题是,当listView退出并返回到视图时,替换deault图像的图像将恢复为默认图像。

public class BookViewCustomList extends ArrayAdapter<String>{
private final Activity context;
private final ArrayList<String> chapter;
private final ArrayList<String> verse;
private final ArrayList<String> content;
private final Integer[] imageId;

public BookViewCustomList(Activity context, ArrayList<String> chapter, ArrayList<String> verse, Integer[] imageId, ArrayList<String> content) {
    super(context, R.layout.custom_book_view_layout, chapter);
    this.context = context;
    this.chapter = chapter;
    this.verse = verse;
    this.imageId = imageId;
    this.content = content;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.custom_book_view_layout, null, true);

    TextView txtChapter = (TextView) rowView.findViewById(R.id.textViewChapter);
    txtChapter.setText(chapter.get(position));

    TextView txtVerse = (TextView) rowView.findViewById(R.id.textViewVerse);
    txtVerse.setText(verse.get(position));

    //ImageView imageViewBookmark = (ImageView) rowView.findViewById(R.id.imageButtonBookmark);
    //imageViewBookmark.setImageResource(imageId[position]);

    TextView txtContent = (TextView) rowView.findViewById(R.id.textViewContent);
    txtContent.setText(content.get(position));


    final ImageView imgView = (ImageView) rowView.findViewById(R.id.imageViewBookmark);
    imgView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            imgView.setImageResource(R.drawable.bookmark_blue);
        }
    });

    return rowView;
}

}

那么如何保留已经做出的改变?谢谢!

2 个答案:

答案 0 :(得分:0)

您需要拥有自己的数据模型和选中的项目,然后您需要维护模型。例如,为适配器编写模型 -

public class Book{
 private String chapter;
 private String verse;
 private String content;
 private int imageID;
 private boolean isChecked;
}

同样设置getter和setter方法,并将上述类型的适配器修改为ArrayAdapter<Book>。 现在,您可以修改getView()的{​​{1}}方法,以检查或取消选中模型项,并相应地设置图像。

答案 1 :(得分:0)

似乎你还没有使用viewHolder模式。

试试这个

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = context.getLayoutInflater();
    final ViewHolder holder;
    if(view==null){
        holder = new ViewHolder();

    view = inflater.inflate(R.layout.custom_book_view_layout, viewGroup, false);

    holder.txtChapter = (TextView) rowView.findViewById(R.id.textViewChapter);
    holder.txtVerse = (TextView) rowView.findViewById(R.id.textViewVerse);
    holder.txtContent = (TextView) rowView.findViewById(R.id.textViewContent);
    //ImageView imageViewBookmark = (ImageView) rowView.findViewById(R.id.imageButtonBookmark);
    holder.imgView = (ImageView) rowView.findViewById(R.id.imageViewBookmark);
        view.setTag(holder);
    }else{
        holder = (ViewHolder)view.getTag();
    }
    holder.txtChapter.setText(chapter.get(position));
    holder.txtVerse.setText(verse.get(position));
    //imageViewBookmark.setImageResource(imageId[position]);
    holder.txtContent.setText(content.get(position));

    holder.imgView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.imgView.setImageResource(R.drawable.bookmark_blue);
        }
    });

    return view;
}
class ViewHolder {
    TextView txtChapter, txtVerse, txtContent;
    ImageView imgView;
}