2016-02-21 62 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对象以反映对该复选框的更改。 –

相关问题