2015-12-08 201 views
1

我已經創建了列表視圖,其中包含兩個項目的動態項目:CheckBox和TextView。當用戶點擊項目內的複選框時,它會更新textView的背景顏色。它工作正常,直到這個項目滾動出可見的列表的用戶空間,我得到錯誤:java.lang.NullPointerException在com.viewactivities.AddNewPosition.changeTextColorWhenCheckBoxIsClicked(AddNewPosition.java:183)。ListView項背景顏色更新

我也實現了一個負責改變顏色的方法的接口。我已經觀察到,「listTextColorWhenCheckBoxIsClicked」方法也被觸發,當listView中的項目不再可見時(這裏它導致錯誤)。誰能幫忙?

規範類,其中包含ListView和方法來改變顏色:

@Override 
public void changeTextColorWhenCheckBoxIsClicked(int position) { 
    // TODO Auto-generated method stub 

    listView.getChildAt(position).findViewById(R.id.listViewItemText).setBackgroundColor(Color.BLUE); 

} 

和代碼的ListView適配器:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
      ViewHolder viewHolder = null; 
      globalPosition = position; 

     if(convertView==null){ 

      LayoutInflater inflater = ((AddNewPosition) mContext).getLayoutInflater();  
      convertView = inflater.inflate(layoutResourceId, parent, false); 

      viewHolder = new ViewHolder(); 
       viewHolder.text = (TextView) convertView.findViewById(R.id.listViewItemText); 
       viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.listViewItemCheckBox); 

       viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 


         int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
         listOfObjects.get(getPosition).setCheckBoxEnabled(buttonView.isChecked()); // Set the value of checkbox to maintain its state. changeTextColorWhenCheckBoxIsClicked(getPosition); 
        } 
       }); 
       convertView.setTag(viewHolder); 
       convertView.setTag(R.id.listViewItemText, viewHolder.text); 
       convertView.setTag(R.id.listViewItemCheckBox, viewHolder.checkbox); 
       } else { 
       viewHolder = (ViewHolder) convertView.getTag(); 
      } 
      viewHolder.checkbox.setTag(position); // This line is important. 

      viewHolder.text.setText(listOfObjects.get(position).getTextFromListViewItemObject()); 
      viewHolder.checkbox.setChecked(listOfObjects.get(position).getIsCheckedBoxEnabled()); 
      //viewHolder.text.setBackgroundColor(Color.RED); 

     //ListViewItemObject tmpObject = listOfObjects.get(position); 

     //TextView listItemText = (TextView) convertView.findViewById(R.id.listViewItemText); 
     //CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.listViewItemCheckBox); 
     //checkBox.setChecked(false); 

     //listItemText.setText(tmpObject.getTextFromListViewItemObject()); 

     return convertView; 
    } 


    @Override 
    public void changeTextColorWhenCheckBoxIsClicked(int position) { 
     // TODO Auto-generated method stub 

     ((AddNewPosition) mContext).changeTextColorWhenCheckBoxIsClicked(position); 

    } 

回答

0

ListView的重用,從而代替使用setTag其觀點()使用列表保持這一地位。

viewHolder.checkbox.setTag(position); // This line is important. 

事情是這樣的 - Android Listview Scrolling challenge for row elements

+0

我讀過你的答案,但它仍然是困惑我。你能在我的代碼中顯示什麼應該改變? – DKM