I want to update the listview in the activation every time I click the checkbox, but when I call the updateTodo () method from the checkbox listener, the application does not work.
MainActivity
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.getListView().setDividerHeight(2); dbHelper = new TodoDatabase(this); fillData(); registerForContextMenu(getListView()); mEditText = (EditText)findViewById(R.id.editTextNewTask); RowId = null; Bundle extras = getIntent().getExtras(); RowId = (savedInstanceState == null) ? null : (Long) savedInstanceState .getSerializable(TodoDatabase.COLUMN_ID); if (extras != null) { RowId = extras.getLong(TodoDatabase.COLUMN_ID); } startService(new Intent(this, TimeService.class)); } ......... ......... public void updateTodo(){ fillData(); } private void fillData() { cursor = dbHelper.getAllTodosListView(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { startManagingCursor(cursor); } String[] from = new String[] {TodoDatabase.COLUMN_ACHIEVED, TodoDatabase.COLUMN_SUMMARY, TodoDatabase.COLUMN_TIMESECONDS}; int[] to = new int[] {R.id.checkBoxLabel, R.id.textViewLabel }; // Π’Π΅ΠΏΠ΅ΡΡ ΡΠΎΠ·Π΄Π°Π΄ΠΈΠΌ Π°Π΄Π°ΠΏΡΠ΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π° ΠΈ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΠΌ Π΅Π³ΠΎ Π΄Π»Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π½Π°ΡΠΈΡ
// Π΄Π°Π½Π½ΡΡ
SimpleCursorAdapter notes = new MySimpleCursorAdapter(getApplicationContext(), R.layout.list_item, cursor, from, to, 0); notes.changeCursor(cursor); notes.notifyDataSetChanged(); setListAdapter(notes); } SimpleCursorAdapter
public class MySimpleCursorAdapter extends SimpleCursorAdapter { private Context context; private LayoutInflater mInflater; private CheckBox mCheckBox; private long xId; public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); mInflater = LayoutInflater.from(context); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final View view = mInflater.inflate(R.layout.list_item, parent, false); return view; } @Override public void bindView(View view, final Context context, final Cursor cursor) { final TodoDatabase mDbHelper = new TodoDatabase(context); final TextView textView = (TextView)view.findViewById(R.id.textViewLabel); mCheckBox = (CheckBox)view.findViewById(R.id.checkBoxLabel); xId =Long.parseLong(cursor.getString(cursor .getColumnIndex(TodoDatabase.COLUMN_ID))); textView.setText(cursor.getString(cursor.getColumnIndex(TodoDatabase.COLUMN_SUMMARY))); if (cursor.getString(cursor.getColumnIndex(TodoDatabase.COLUMN_TIMESECONDS)) != null) { if (cursor.getLong(cursor.getColumnIndex(TodoDatabase.COLUMN_TIMESECONDS)) > 0) { if (cursor.getLong(cursor.getColumnIndex(TodoDatabase.COLUMN_TIMESECONDS)) > System.currentTimeMillis()) { } else if (cursor.getInt(cursor.getColumnIndex(TodoDatabase.COLUMN_ACHIEVED)) != 1){ textView.setTextColor(Color.parseColor("#FF0000")); } } } if (cursor.getInt(cursor.getColumnIndex(TodoDatabase.COLUMN_ACHIEVED)) == 1) { mCheckBox.setChecked(true); textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); final Long mRowId = xId; mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Long mXRowId = mRowId; if (mCheckBox.isChecked()) { TodoDatabase mDbHelper = new TodoDatabase(context); mDbHelper.updateTodoAchieved(mXRowId, "1"); MainActivity ma = new MainActivity(); ma.updateTodo(); // ΠΡΠΈΠ±ΠΊΠ° } else if (!mCheckBox.isChecked()) { TodoDatabase mDbHelper = new TodoDatabase(context); mDbHelper.updateTodoAchieved(mXRowId, "0"); MainActivity ma = new MainActivity(); ma.updateTodo(); // ΠΡΠΈΠ±ΠΊΠ° } } }); } else { mCheckBox.setChecked(false); textView.setPaintFlags(textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG)); final Long mRowId = xId; mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { Long mXRowId = mRowId; @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (mCheckBox.isChecked()){ TodoDatabase mDbHelper = new TodoDatabase(context); mDbHelper.updateTodoAchieved(mXRowId, "1"); MainActivity ma = new MainActivity(); ma.updateTodo(); // ΠΡΠΈΠ±ΠΊΠ° } else if (!mCheckBox.isChecked()){ TodoDatabase mDbHelper = new TodoDatabase(context); mDbHelper.updateTodoAchieved(mXRowId, "0"); MainActivity ma = new MainActivity(); ma.updateTodo(); // ΠΡΠΈΠ±ΠΊΠ° } } }); } } }