android:阅读表给出了同一个对象的多个副本

时间:2017-08-24 03:23:47

标签: java android sqlite

在SecondActivity.java中,我尝试使用以下数据填充SQLite表,然后根据第3个参数(学校/购物/餐厅)读取行。当我调试应用程序时,我得到同一个对象的多个副本。

 DatabaseHelper db = new DatabaseHelper(this);

        // Inserting default todos
        Log.d("Insert: ", "Inserting ..");
        db.addtodo(new Todo(1,"Wallet", "SCHOOL"));
        db.addtodo(new Todo(2,"Laptop", "SCHOOL"));
        db.addtodo(new Todo(3,"Phone", "SCHOOL"));
        db.addtodo(new Todo(4,"Laptop Charger", "SCHOOL"));
        db.addtodo(new Todo(5,"Phone Charger", "SCHOOL"));
        db.addtodo(new Todo(6,"USB", "SCHOOL"));


        db.addtodo(new Todo(8,"Wallet", "SHOPPING"));
        db.addtodo(new Todo(9,"Phone", "SHOPPING"));
        db.addtodo(new Todo(10,"Coupons", "SHOPPING"));
        db.addtodo(new Todo(11,"Shopping List", "SHOPPING"));


        db.addtodo(new Todo(12,"Wallet", "RESTAURANT"));
        db.addtodo(new Todo(13,"Phone", "RESTAURANT"));
        db.addtodo(new Todo(14,"Gift Cards", "RESTAURANT"));

        Log.d("Reading: ", "Reading all contacts..");
        ArrayList<Todo> tags_school = db.getAllTodo("SCHOOL");
        ArrayList<Todo> tags_restaurant = db.getAllTodo("RESTAURANT");
        ArrayList<Todo> tags_shopping = db.getAllTodo("SHOPPING");

        // Construct the data source
        ArrayList<Todo> todolist = new ArrayList<Todo>();

        switch (message){
            case "SCHOOL":{
                todolist = tags_school;
                break;
            }
            case "SHOPPING":{
                todolist = tags_shopping;
                break;
            }
            case "RESTAURANT":{
                todolist = tags_restaurant;
                break;
            }
            case "CUSTOM":{

                break;}
        }


        // Create the adapter to convert the array to views
        TodoAdapter adapter = new TodoAdapter(this, todolist);
        // Attach the adapter to a ListView
        ListView listView = (ListView) findViewById(R.id.lvItems);
        listView.setAdapter(adapter)

} 这是我在DatabaseHelper.java中执行此操作的功能

public ArrayList<Todo> getAllTodo(String tag) {
        ArrayList<Todo> noteList = new ArrayList<Todo>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_NOTES + " WHERE " + KEY_TAG + "='"+tag+"';";

        SQLiteDatabase db = this.getWritableDatabase();
//I THINK THE QUERY IS GETTING MULTIPLE TIMES
    Cursor cursor = db.rawQuery(selectQuery, null);

        cursor.moveToFirst();
        // looping through all rows and adding to list
        while (!cursor.isAfterLast()) {

                Todo todo = new Todo();
                todo.set_id(Integer.parseInt(cursor.getString(0)));
                todo.setNote(cursor.getString(1));
                todo.setTag(cursor.getString(2));
                // Adding contact to list
                noteList.add(todo);
            cursor.moveToNext();

        }

        // return contact list
        return noteList;
    }

这些是tags_school的内容。 Here is the first pic Here is the 2nd pic 我应该只读取6个对象但是读取121.这个数字每次运行代码时都会有所不同。很混乱。我不知道哪里出错了。

1 个答案:

答案 0 :(得分:0)

由于我没有完整的代码,我建议您使用其他方法轻松解决问题。

就像Altaf所说的那样,从设备或模拟器上的应用管理器清理应用数据并首次运行应用以查看您获得的记录数。

如果问题仍然存在,请检查您的代码是否在onCreate / onResume中插入数据,就像它在onCreate / onResume中一样,然后每次活动加载时,都会插入数据而无需任何操作来插入数据。

在数据库表上添加主键,唯一键等约束,这样如果它在不知不觉中再次插入数据,那么代码将失败,并且您将获得在数据库中再次插入数据的罪魁祸首代码,而无需任何操作来插入数据

在数据库表TABLE_NOTES和ToDo POJO中添加字段timeCreated是一个好习惯,它可以是long或String(使用SimpleDateFormat格式化日期,添加条目时使用小时和分钟)。添加Log.d语句打印数据插入时间,当您反复运行程序时,它将是一个调试数据,可以帮助您识别数据是否被多次插入以及何时插入。