简体   繁体   English

将数据从sqlite加载到arraylist时,我只得到最后一行。 如何获取所有记录?

[英]I am getting only last row while loading data from sqlite to arraylist. How do I get all records?

I am trying to populate the sqlite data to recyclerview by adding the sqlite data to arraylist. 我正在尝试通过将sqlite数据添加到arraylist来填充sqlite数据到recyclerview。 I am not getting all records but only last row of the database table. 我没有得到所有记录,而只有数据库表的最后一行。

        cursor = sqLiteHelper.getData("SELECT * FROM news");

        while (cursor.moveToNext()) {

            String image = cursor.getString(cursor.getColumnIndex("image"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String category = cursor.getString(cursor.getColumnIndex("category"));

            mExampleList.add(new ExampleItem(image, title, category));


        }


        mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
        mRecyclerView.setAdapter(mExampleAdapter);

Did you try this? 你有尝试过吗?

// Move first to be sure
cursor.moveToFirst()
while(!cursor.isAfterLast()) { // Dont jump to the next before first iteration
    String image = cursor.getString(cursor.getColumnIndex("image"));
    String title = cursor.getString(cursor.getColumnIndex("title"));
    String category = cursor.getString(cursor.getColumnIndex("category"));

    mExampleList.add(new ExampleItem(image, title, category));

    // And then 
    cursor.moveToNext()
} 
cursor.close()

How many records are in your storage? 您的存储中有多少条记录? Because if there are 2 the first call to cursor.moveToNext() of the while might jump to the last. 因为如果有2,则while的第一次调用cursor.moveToNext()可能会跳到最后一个。

if (cursor.moveToFirst()) { 
    while (!cursor.isAfterLast()) { 
        String name = cursor.getString(cursor.getColumnIndex(countyname)); 
        list.add(name); 
        cursor.moveToNext(); 
    } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从查询中我会得到 2 行但是在返回 map 时我没有得到 2 行我只得到最后一行 - From query i wil get 2 rows but while returing the map i am not getting 2 rows i am getting only last row JSON解析为ArrayList。 如何在onPostExecute中为多个地图标记重复数据? - JSON parsed to ArrayList. How can I get it to repeat data in onPostExecute for multiple map markers? 仅将我要添加的最终数组列表克隆到2维ArrayList的所有元素。 - Only the final Array list I'm adding is cloned to all elements of the 2 Dimensional ArrayList. “弹出”数组列表中的元素。 我想念什么? - 'popping' off an element in an arraylist. what am I missing? 不断收到arraylist的get()错误。 为什么? - Keep getting an error for the get() of an arraylist. Why? 我只需要从数组中分离单词并将每个单词放入 ArrayList 中。 在这个时候,我不能使用 String tokenizer - I need separe only the words from the array and put each word into an ArrayList. In this time, I can't use String tokenizer 从 ArrayList 中获取字符串。 (带数字) - Get a String from an ArrayList. (with numbers) 如何在for循环中从arraylist获取所有数据总和? - How can I get all sum of data from arraylist in a for loop? 我如何从DAO数组列表中获取数据? - how do i get data from a DAO arraylist? 我的数组列表搜索方法有问题。 条件仅在第一个索引中为真 - I have a problem in my search method for the arraylist. The condition is true only in the first index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM