-2

This is my fragment which have a listview. here data is read and entered in the listview.
I put a button here which deleted entire data of that particular table from the database. Now when I press that button, I get no response from the listview (i.e. the listview itmes are still there).
but when the fragment is restarted (either orientation changed or restarting the entire app), the data is deleted.

public class tasksListFrag extends Fragment implements View.OnClickListener {

ListView taskslist;
private ArrayList<singleRow> todoItems = new ArrayList<>();
String taskentered, dateentered, timeentered;
taskListRecycler adapter;
Button delete;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tasks_list_frag, container, false);
    Log.d("HirakDebug", "tasksListFrag View Created");
    taskslist = (ListView) view.findViewById(R.id.tasks_list);
    adapter = new taskListRecycler(getActivity(), todoItems);
    taskslist.setAdapter(adapter);
    Log.d("HirakDebug", "tasksListFrag Adapter set");
    delete = (Button) view.findViewById(R.id.deleteAll);
    delete.setOnClickListener(this);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    Log.d("HirakDebug", "taskListFrag resumed");
    tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
    String[] columns = {tasksDatabaseOperations.ASSIS_TASK, tasksDatabaseOperations.ASSIS_DATE, tasksDatabaseOperations.ASSIS_TIME};
    SQLiteDatabase sqLiteDatabase = tasksDatabaseOperations.getWritableDatabase();
    Cursor cursor = sqLiteDatabase.query(tasksDatabaseOperations.ASSIS_TASK_TABLE_NAME, columns, null, null, null, null, null);

    while (cursor.moveToNext()) {

        Log.d("HirakDebug", "Cursor Moved To Next");
        int index0 = cursor.getColumnIndex(tasks_Database_Operations.ASSIS_TASK);
        int index1 = cursor.getColumnIndex(tasks_Database_Operations.ASSIS_DATE);
        int index2 = cursor.getColumnIndex(tasks_Database_Operations.ASSIS_TIME);
        String task = cursor.getString(index0);
        Log.d("HirakDebug", "tasks_Database_Operations got Task from table");
        String date = cursor.getString(index1);
        Log.d("HirakDebug", "tasks_Database_Operations got Date from table");
        String time = cursor.getString(index2);
        Log.d("HirakDebug", "tasks_Database_Operations got Time from table");
        singleRow ld = new singleRow();
        ld.setTask(task);
        Log.d("HirakDebug", "tasks_Database_Operations setTask called");
        ld.setDate(date);
        Log.d("HirakDebug", "tasks_Database_Operations setDate called");
        ld.setTime(time);
        Log.d("HirakDebug", "tasks_Database_Operations setTime called");
        todoItems.add(ld);
        Log.d("HirakDebug", "tasksListFrag Data Chenged Notified");
        adapter.notifyDataSetChanged();
        sqLiteDatabase.close();
        Log.d("HirakDebug", "tasksListFrag Database Closed");

/*    tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
    Cursor cursor = tasksDatabaseOperations.readData();
    String[] from = new String[]{tasksDatabaseOperations.ASSIS_TASK,
                                 tasksDatabaseOperations.ASSIS_DATE,
                                 tasksDatabaseOperations.ASSIS_TIME};
    int[] to = new int[]{R.id.task_added, R.id.date_added, R.id.time_added};

    SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.single_row,
            cursor,from, to);
    simpleCursorAdapter.notifyDataSetChanged();
    taskslist.setAdapter(simpleCursorAdapter);*/

    }
}

@Override
public void onPause() {
    super.onPause();
    Log.d("LifeCycle", "tLF Pause");
}

@Override
public void onStop() {
    super.onStop();
    Log.d("LifeCycle", "tLF Stop");
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    Log.d("LifeCycle", "tLF DestroyView");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d("LifeCycle", "tLF Destroy");
}

@Override
public void onDetach() {
    super.onDetach();
    Log.d("LifeCycle", "tLF Detach");
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d("LifeCycle", "tLF Attach");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("LifeCycle", "tLF Create");
}

@Override
public void onStart() {
    super.onStart();
    Log.d("LifeCycle", "tLF Start");
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("list", (Serializable) todoItems);
    Log.d("LifeCycle", "tLF saveInstance State");
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        //probably orientation change
        todoItems = (ArrayList<singleRow>) savedInstanceState.getSerializable("list");
    } else {
        if (todoItems != null) {
            //returning from backstack, data is fine, do nothing
        }
    }
}

@Override
public void onClick(View v) {
    tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
    SQLiteDatabase sqLiteDatabase = tasksDatabaseOperations.getWritableDatabase();
    sqLiteDatabase.delete(tasksDatabaseOperations.ASSIS_TASK_TABLE_NAME, null, null);
    Log.d("HirakDebug", "All Data Deleted");
    adapter.notifyDataSetChanged();
    sqLiteDatabase.close();
}
}
Hirak Chhatbar
  • 3,159
  • 1
  • 27
  • 36
  • Try [this](http://stackoverflow.com/a/19655916/1777090) – MysticMagicϡ Oct 21 '14 at 08:23
  • You need to clear your todoItems if the delete is succesful. What you are doing there is only deleting information from your database , but your arrayList of todoItems remains unchanged ;) – Daniel M. Oct 21 '14 at 08:30
  • you should clear the items of your adapter before calling `notifyDataSetChanged()` – Houcine Oct 21 '14 at 09:16

2 Answers2

0

It's a bit hard to tell from your code since you have a lot of lines per method but you don't seem to be emptying the actual ArrayList of display items when the onClick happens?

To make debugging easier you should really refactor large blocks of code into separate methods with clear names.

G_V
  • 2,396
  • 29
  • 44
0

You are not clearing todolItems. So you need to clear it after deleting your table.

Add the below line just before adapter.notifyDataSetChanged() inside your onClick() method.

todolItems.clear();
Vijeesh
  • 181
  • 10