【问题标题】:displaying an ArrayList in ListView Android在 ListView Android 中显示 ArrayList
【发布时间】:2017-08-15 07:12:09
【问题描述】:

我很难将保存在 SqlLite 数据库中的按钮单击存储的数据显示到 ListView 中。当我单击它以将数据添加到数据库时,它不会在按钮下方显示我想要的数据。

我的代码:

boolean hasMoreData = cursor.moveToFirst();
    while (hasMoreData) {
        // get the value out of each column
        long key = cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry._ID));
        String studentID = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
        String studentGrade = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));
        //For now, just print out the record to the log.      

        ArrayList<String> myList = new ArrayList<>();
        myList.add( " student id: "+ studentID);
        myList.add(" student grade : " +studentGrade);
        ArrayAdapter<String> myAdapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,myList);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(myAdapter);


        System.out.println("RECORD KEY: " + key + " student id: " + studentID + " student grade : " + studentGrade);




        //move on to the next row!
        hasMoreData = cursor.moveToNext();
    }

这在模拟器中的样子:

current display

我想要什么:

what i want it to look like

【问题讨论】:

    标签: android sqlite listview arraylist android-sqlite


    【解决方案1】:

    在循环外放置一个 ArrayAdapterListView

    ArrayList<String> myList = new ArrayList<>();
    
    boolean hasMoreData = cursor.moveToFirst();
    while (hasMoreData) {
        long key = cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry._ID));
        String studentID = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
        String studentGrade = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));
    
        myList.add( " student id: "+ studentID);
        myList.add(" student grade : " +studentGrade);
    
        hasMoreData = cursor.moveToNext();
    }   
    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, myList);
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(myAdapter);
    

    【讨论】:

    • 放入 ArrayList myList;上面的 while 循环给了我在新的 ArrayAdapter 调用中的错误,因为 myList 没有被初始化
    • nvm 我通过调用 new 来修复它
    • 这仅显示添加的最后一个成绩,但似乎出于某种原因
    • 将 ArrayList 放在循环的顶部。
    【解决方案2】:

    每次您拨打ArrayList&lt;String&gt; myList = new ArrayList&lt;&gt;();ArrayAdapter&lt;String&gt; myAdapter = new ArrayAdapter&lt;String&gt;(); 时,您都会重置列表。

    删除ArrayList&lt;String&gt; myList = new ArrayList&lt;&gt;();

    然后移动 ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1); ListView listView = (ListView) findViewById(R.id.listView); listView.setAdapter(myAdapter);

    在你的循环之上,你会解决这个问题

    在循环内调用'myAdapter.add("whatever you want");'然后myAdapter.notifyDataSetChanged()更新ui

    【讨论】:

    • 在我的循环之外究竟在哪里?在 while 的入口上方还是下方?
    • while 循环的正上方
    • 好的,这适用于获取所有数据。现在我的问题是,如何显示当前通过按钮单击添加的数据?
    • 更新了我的答案。但 EYN 的回答要干净得多;)
    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    相关资源
    最近更新 更多