2016-10-25 29 views
0

我已經定義了一個任務,它只是一個複選框和一個文本。複選框在列表更新時取消選中 - Android

我也有一個顯示正確的任務ListView。

問題是,當我滾動列表或當我嘗試向我的列表中添加新元素時,所有複選框均取消選中,但文本保持不變。

下面是我在TaskAdapter有getView:

public View getView(int position, View convertView, ViewGroup parent) { 
    Task task = getItem(position); 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(layoutID, parent, false); 
    TextView textView = (TextView) convertView.findViewById(R.id.description); 
    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.box); 
    textView.setText(task.getDescription()); 
    checkBox.setActivated(task.getBox()); 
    return convertView; 
} 

在此先感謝您的幫助:d

+0

使用checkBox.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){ @Override 公共無效onCheckedChanged(CompoundButton buttonView,布爾器isChecked){ task.setBox(器isChecked); } }); – asim

+0

@asim。你忘了告訴OP如何確定所點擊項目的使用「任務」。因爲他需要一個「位置」。 – greenapps

+0

@asim非常感謝,當我用它來改變列表中的值時,它很有用 – streiter

回答

2

task.getBox()函數返回false。那就是問題所在。請檢查該區域。

+0

即使我編寫了setActivated(true),我的框仍然未被選中 – streiter

0

checkBox.setActivated(task.getBox());用於激活/取消激活複選框。使用checkBox.setChecked(task.getBox());設置選中/未選中。

相關問題