从sqLite数据库中删除整个表

时间:2014-10-21 08:22:10

标签: android sqlite

这是我的片段,其中包含listview。这里读取数据并在列表视图中输入 我在这里放了一个按钮,从数据库中删除了该特定表的全部数据。现在当我按下那个按钮时,我没有从列表视图中得到任何回复(即列表视图仍然存在) 但是当片段重新启动时(方向改变或重新启动整个应用程序),数据将被删除。

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

2 个答案:

答案 0 :(得分:0)

由于每个方法有很多行,但是当onClick发生时你似乎没有清空显示项的实际ArrayList,所以从你的代码中说出来有点难以理解?

为了使调试更容易,您应该将大块代码重构为具有明确名称的单独方法。

答案 1 :(得分:0)

您尚未清除todolItems。所以你需要在删除表后清除它。

adapter.notifyDataSetChanged()方法中onClick()之前添加以下行。

todolItems.clear();