2013-10-22 133 views
2
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

      if (selectedCourses.contains(courseNames.get(position))){ 
       selectedCourses.remove(selectedCourses.lastIndexOf(courseNames.get(position))); 
       Toast.makeText(getApplicationContext(), 
         courseNames.get(position) + " removed", Toast.LENGTH_LONG) 
         .show(); 

       row = view; 
       row.setBackgroundResource(R.drawable.charback); 

       System.out.println(selectedCourses); 
      } 

      else{ 
       selectedCourses.add(courseNames.get(position)); 
       Toast.makeText(getApplicationContext(), 
         courseNames.get(position) +" added", Toast.LENGTH_LONG) 
          .show(); 

       row = view; 
       row.setBackgroundResource(R.drawable.white); 

       myarrayAdapter.notifyDataSetChanged(); 
       System.out.println(selectedCourses); 

      } 

所選項目變成白色,但是大約12個項目延遲另一個隨機項目變成白色,就像它進入視圖一樣?有人可以幫幫我嗎??ListView項目更改顏色問題。多個項目向下改變視圖

回答

0

試試這個:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

     if (selectedCourses.contains(courseNames.get(position))){ 
      selectedCourses.remove(selectedCourses.lastIndexOf(courseNames.get(position))); 
      Toast.makeText(getApplicationContext(), 
        courseNames.get(position) + " removed", Toast.LENGTH_LONG) 
        .show(); 

      row = view; 
      row.setBackgroundResource(R.drawable.charback); 

      System.out.println(selectedCourses); 
     } 

     else{ 
      selectedCourses.add(courseNames.get(position)); 
      Toast.makeText(getApplicationContext(), 
        courseNames.get(position) +" added", Toast.LENGTH_LONG) 
         .show(); 

      row = view; 
      row.setBackgroundResource(R.drawable.white); 

      System.out.println(selectedCourses); 

     } 

我刪除myarrayAdapter.notifyDataSetChanged();行....

0

會發生什麼情況是您的適配器中的getView方法不是回收/重新使用提供的convertView

如果convertView不爲空,表示它是一個正在被重用的視圖。這意味着您應該明確地將背景資源設置爲默認值,除非該項目被選中。您看到白色背景重用的原因是因爲您將而不是重置爲默認背景。

喜歡的東西:

if (convertView != null) { 
    if (selectedCourses.contains(courseNames.get(position))){ 
     convertView.setBackgroundResource(R.drawable.white); 
    } else { 
     convertView.setBackgroundResource(R.drawable.charback); 
    } 
} 
0

您可以使用Holder類此:

在您的適配器類創建包含您的組件另一個靜態類:請參見下面的代碼

static class MyHolder { 

     TextView mTextView; 
     RelativeLayout lv; 
     ImageView imgInvite; 
     RelativeLayout rel_bckground; 
    } 

後使用它在您的getView方法中:

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

      View row = convertView; 
      holder = null; 

      if (row == null) { 

     // initialize your components 
     holder = new MyHolder(); 
     holder.mImageView = (ImageView) row.findViewById(R.id.someID); 

     }else{ 

     holder = (MyHolder) row.getTag(); 
      } 
    }