0
嗨,大家好,我試圖設置在xml
文件表示ListView
(item_todo.xml)的單行聲明的CheckBox
的OnCheckedChangeListener
,在這裏複選框的代碼:如何設置OnCheckedChangeListener到小區的複選框,在ListView
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:text="" />
這裏的列表視圖的代碼的部分(fragment_bacheca.xml):
<ListView
android:id="@+id/listView1"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
然後我用一個自定義適配器:`
public class TaskListAdapter extends ArrayAdapter<TaskCell>{
public TaskListAdapter(Context context, int textViewResourceId, List<TaskCell> objects){
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_todo, null);
TextView task_subject = (TextView) convertView.findViewById(R.id.task_subject);
TextView task_type = (TextView) convertView.findViewById(R.id.task_type);
TextView task_data = (TextView) convertView.findViewById(R.id.task_data);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
TaskCell t = getItem(position);
task_subject.setText(t.getMateria());
task_type.setText(t.getTipo());
task_data.setText(t.getData());
if(t.getFatto().equals("1")) {
cb.setChecked(true);
task_subject.setPaintFlags(task_subject.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else{
cb.setChecked(false);
}
return convertView;
}
TaskCell
對於todo_item的數據的類。
在這裏,我呼籲聽衆片段的代碼:
taskView = getActivity().getLayoutInflater().inflate(R.layout.item_todo, null);
checkbox_task = (CheckBox) taskView.findViewById(R.id.checkBox1);
checkbox_task.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
adapter.notifyDataSetChanged();
}
});
list = new LinkedList<>();
adapter = new TaskListAdapter(getContext(), R.layout.item_todo, list);
lv.setAdapter(adapter);
的問題是,什麼都沒有發生,並與調試我看到onCheckedChanged
永遠達不到。
任何人都知道爲什麼?謝謝大家
它可能不是原因,而是你所提到的'todo_item.xml'作爲問題描述的文件名,但我在代碼中看到了'item_todo'。我在代碼中看不到'todo_item.xml'。 –
我在解釋中犯了一個錯誤,我打算item_todo.xml – AESolutions
'OnCheckedChangeListener()'應該在適配器內而不是片段內部實現。 –