2015-07-11 96 views
0

Listview有一個默認圖像,我將其替換爲另一圖像,當圖像被選中時(如下所示 - onClickListener)。問題是當listView熄滅並且返回到View中時,替換deault圖像的圖像會恢復爲默認圖像。當Listview滾動時,自定義Listview中的圖像正在被取消設置

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

}

那麼如何留住已經作出了改變?謝謝!

+0

使用視圖架模式http://codehenge.net/blog/2011/06/android-development-tutorial-asynchronous-lazy-loading-and-caching-of-listview-圖片/ –

回答

0

你需要有自己的數據模型與檢查或所選的項目,那麼你就需要保持model.For示例編寫模型適配器原樣

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

建立getter和setter方法也適用,並修改上述類型的適配器,如ArrayAdapter<Book>。 現在您可以修改您的getView()方法onClick()以使您的模型項目選中或取消選中,並相應地設置圖像。

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

它崩潰。我得到NPE上運行上面的代碼 – Jerry

+0

好吧,我gotch雅 從getview方法去除LaoutInflater並嘗試在初始化吹氣適配器構造函數 吹氣= LayoutIflater.from(背景); –

相關問題