我的数据库每次调用数据库时都会在listview中加载我的所有数据

时间:2016-02-02 13:49:49

标签: android sqlite listview

我非常确定我的sqlite语法是正确的,但我不知道为什么在我的listview中加载数据库结果时会显示我的所有数据。我认为问题出在我的光标片上。

如何解决仅加载table_one.id = table_two.day_id

行的问题
 @Override
    public List<Model> getAllEx() {
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Model> list = new ArrayList<>();
        String query = "SELECT * FROM " + Constants.TABLE_ONE + " INNER JOIN " + Constants.TABLE_TWO +" ON "+
                Constants.ID_ONE + " = " + Constants.ID_DAY;

        Cursor c = db.rawQuery(query, null);
        while (c.moveToNext()) {

            Model model = new Model();
            model.setSecond_id(c.getInt(c.getColumnIndex(Constants.ID_TWO)));
            model.setExercise(c.getString(c.getColumnIndex(Constants.EXERSICE)));
            model.setNumber(c.getString(c.getColumnIndex(Constants.NUMBER)));
            model.setReps(c.getString(c.getColumnIndex(Constants.REPS)));
            model.setId_day(c.getInt(c.getColumnIndex(Constants.ID_DAY)));

            list.add(model);
        }
        return list;
    }

1 个答案:

答案 0 :(得分:0)

我不确定你的常量中有什么值,但是你的查询是这样的

select * from table_one as one inner join table_two as two 
    on one.id = two.day_id 
    where one.category="cat_one" 
       and two.category="cat_two"

如果不需要,您可以删除第二个/第一个表的类别。