List <string []>仅返回表中的最后一行

时间:2017-07-19 17:26:59

标签: java android sqlite

我想将记录数据保存到字符串数组并将字符串数组保存到列表中

我的代码:

public List<String[]> getAllRecords(String table){
        try {
            List<String[]> list = new ArrayList<>();
            String query = "SELECT * FROM " + table;
            openDatabase();
            Cursor cursor = database.rawQuery(query,null);
            int count = cursor.getColumnCount();
            String[] record = new String[count];
            if(cursor.moveToFirst()){
                do {
                    for (int i = 0; i < count; i++) {
                        record[i] = cursor.getString(i);
                    }
                    list.add(record);
                }while (cursor.moveToNext());
            }
            //log for test list content
            int a = list.size();
            for (int i=0;i<a;i++){
                String[] s = list.get(i);
                for (int j = 0;j<s.length; j++){
                    Log.i(TAG, "getAllRecords: "+s[j]);
                }
            }
            return list;
        }catch (Exception e) {return null;}
    }

但最后一条记录已添加到列表中10次了!

07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.369 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: 12
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: name9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: biography9
07-19 17:13:23.373 10170-10170/com.package.app I/Tag:Database: getAllRecords: null

我如何解决此问题或如何获取所有记录并保存到列表?

2 个答案:

答案 0 :(得分:2)

您需要在循环内创建查询结果中记录的record数组:

if(cursor.moveToFirst()){
    do {
        String[] record = new String[count];
        for (int i = 0; i < count; i++) {
            record[i] = cursor.getString(i);
        }
        list.add(record);
    }while (cursor.moveToNext());
}

没有它,你使用一个record数组, 并为每一行覆盖其内容, 并重新添加到list

答案 1 :(得分:0)

list.add(record);移至循环中。像这样的代码。

if(cursor.moveToFirst()){
    do {
        for (int i = 0; i < count; i++) {
            record[i] = cursor.getString(i);
            list.add(record);
        }
    }while (cursor.moveToNext());
}

我希望能解决你的问题。