2016-02-21 63 views
0

我現在在這裏我有同一主題的一些問題,但是任何的,這是爲我工作: When scrolling custom ListView, the checkbox value changes Checkbox gets unchecked when i scroll my custom listview複選框值更改時滾動列表視圖

我可以申請任何這個答案我的代碼:

我的問題是,如果我檢查或取消選中某個複選框並滾動列表視圖,當我回到該項目時沒有他的正確狀態。

有人可以看看我的getView方法,並試圖看看錯誤在哪裏?

在此先感謝。

這是我getView

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

    Log.e("ConvertView", String.valueOf(position)); 
    final ViewHolder holder; 

    if (convertView == null) { 
     LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = vi.inflate(R.layout.newspaper_list_row, null); 

     holder = new ViewHolder(); 
     holder.newspaperName = (TextView) convertView.findViewById(R.id.newspaperName); 
     holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1); 

     Country country = countryList.get(position); 
     holder.checkbox.setTag(country.getName()); 

     convertView.setTag(holder); 

    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.checkbox.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      CheckBox cb = (CheckBox) v; 
      Country country = (Country) cb.getTag(); 


      if (((CheckBox) v).isChecked()) { 
       holder.checkbox.setChecked(true); 
       addPreferencesAndDataBase(country.getName(), country.getUrl()); 

      } else { 
       removePreferencesAndDataBase(country.getName()); 
       holder.checkbox.setChecked(false); 

      } 
     } 

    }); 

    Country country = countryList.get(position); 
    holder.newspaperName.setText(country.getName()); 
    holder.checkbox.setChecked(country.isSelected()); 
    holder.checkbox.setTag(country); 

    return convertView; 

} 

這是我的祖國類:

public class Country { 

    int id; 
    String name = null; 
    String url = null; 
    boolean selected = false; 

    public Country(String name, String url, boolean selected) { 
     super(); 
     this.name = name; 
     this.url = url; 
     this.selected = selected; 
    } 

    public Country(String name, String url) { 
     super(); 
     this.name = name; 
     this.url = url; 
    } 

    public long getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 


    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

    public String getUrl() { 
     return url; 
    } 
    public void setUrl(String url) { 
     this.url = url; 
    } 

    public boolean isSelected() { 
     return selected; 
    } 
    public void setSelected(boolean selected) { 
     this.selected = selected; 
    } 

} 
+0

我不明白mCheckBoxes用於什麼,以及爲什麼你在單個複選框的偵聽器中迭代它。 –

+0

mCheckBoxes是一個複選框數組。我用它來設置數據庫中的正確複選框 – Simpson

+0

好的,我可以刪除這段代碼,沒有必要......我只是測試它,但仍然有滾動時的複選框的問題。我只是編輯我的問題 – Simpson

回答

0

它看起來單擊時就像你保存複選框共享偏好的狀態,但你」不要更新Country對象來反映這種變化。如果更新的selected在每一次點擊你的國家類的新的價值,應該OK:

CheckBox cb = (CheckBox) v; 
Country country = (Country) cb.getTag(); 
country.setSelected(cb.isChecked()); // add this line 

你也不必呼籲複選框setChecked()在你的支票聽衆。選中該框將足以使複選框處於正確的狀態。

+0

不知道你告訴我。在國家班級,我有一個get/set /可以獲得州...我用我的國家班級更新了我的問題。你可以看一下嗎?對不起,但我是Android中的新東西。我沒有在數據庫中保存複選框的狀態,只在sharedPreferences中保存。不可能將狀態保存在只有當您滾動時纔會保留的局部變量中? – Simpson

+0

我更新了我的答案以反映新信息。您需要更新Country對象以反映對該複選框的更改。 –

相關問題