S
S
Ser_blyat2018-05-05 14:24:43
Android
Ser_blyat, 2018-05-05 14:24:43

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();
    }

This is part of the custom adapter code (public class CustomCursorAdapter extends SimpleCursorAdapter
)
. how to update list dynamically after button click(buttons are handled in adapter)??
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

1 answer(s)
O
Oleg Gamega, 2018-05-05
@gadfi

If you are using listview, then I recommend looking towards ContentProvider

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question