Answer the question
In order to leave comments, you need to log in
Android Studio how to update ListView after updating db in cursor on button click?
This is a piece of code from a fragment with a list, there is a btnStopAllTask button that changes the status of the task. Everything works fine and the list is updated (I know why, it's just that the adapter is recreated every time).
Button btnStopAllTask = (Button) view.findViewById(R.id.buttonStopAllTask);
btnStopAllTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
contentValues.put(DBHelper.STATUS, statusOff);
database.update(DBHelper.TABLE_TASKS, contentValues, DBHelper.STATUS
+ "=" + String.valueOf(statusStart), null);
onResume();
}
});
//final View viewTimer = getLayoutInflater().inflate(R.layout.timer, null);
return view;
}
public void onResume() {
super.onResume();
dbHelper = new DBHelper(getContext());
database = dbHelper.getReadableDatabase();
userCursor = database.rawQuery("select * from " + DBHelper.TABLE_TASKS + " where "
+ DBHelper.STATUS + "=?", new String[]{String.valueOf(statusStart)});
userCursor.moveToNext();
String[] headers = new String[]{DBHelper.NAME, DBHelper.DURATION_TASK};
adapter = new CustomCursorAdapter(getContext(), R.layout.timer, userCursor, headers,
new int[]{R.id.textViewNameTask, R.id.textViewWithTimer}, 0);
lvWithTaskIsRunningNow.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void onDestroy() {
super.onDestroy();
database.close();
userCursor.close();
}
public void bindView(View view, final Context _context, final Cursor _cursor) {
super.bindView(view,_context,_cursor);
TextView tvWithNameTask = (TextView) view.findViewById(R.id.textViewNameTask);
TextView tvWithTimer= (TextView) view.findViewById(R.id.textViewWithTimer);
ImageButton btnStop = (ImageButton) view.findViewById(R.id.imageButtonStop);
ImageButton btnStart= (ImageButton) view.findViewById(R.id.imageButtonStart);
ImageButton btnPause= (ImageButton) view.findViewById(R.id.imageButtonPause);
contentValues = new ContentValues();
dbHelper = new DBHelper(_context);
database = dbHelper.getWritableDatabase();
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int keyId = _cursor.getInt(_cursor.getColumnIndex(DBHelper.KEY_ID));
contentValues.put(DBHelper.STATUS, IsRunningNow.statusOff);
database.update(DBHelper.TABLE_TASKS, contentValues, DBHelper.KEY_ID
+ "=" + String.valueOf(keyId), null);
//swapCursor(_cursor);
/* Intent intent = new Intent(_context, MainActivity.class);
_context.startActivity(intent);*/
}
});
Answer the question
In order to leave comments, you need to log in
If you are using listview, then I recommend looking towards ContentProvider
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question