0

我正在开发小型 android 应用程序,我想在列表视图中显示新添加的项目。我正在从存储的数据库中获取项目,ArrayList<HashMap<String,String>>()因为我使用了 SimpleAdapter。我正在将新添加的元素放入 ArrayList<HashMap<String,String>>()但不显示在列表视图中,即使我使用过notifyDataSetChanged()更新listview。请帮我解决这个问题。

do 
                {
                    cont = new String[3];
                    cont[0] = Integer.toString(c.getInt(ID));
                    cont[1] = c.getString(booktitl);
                    cont[2] = c.getString(audiotime);

                    audiobooks.add(cont);

                }
                while(c.moveToNext());

                c.close();

                db.close();//close db resources 
                for(int i=0;i<audiobooks.size();i++)
                {
                    //hashmap to add it to resultbookMarklist.
                    HashMap<String, String> item = new HashMap<String, String>();                       
                    resultbookMarklist=new ArrayList<HashMap<String,String>>();

                    String title[]= audiobooks.get(i);

                    item.put("Booktiltle",title[1]);
                    item.put("BookmarkTime",title[2]);
                    //intantiating string which stores filetitle
                    from= new String[] {"Booktiltle","BookmarkTime" };
                    //intantiating array of integer which gives detais and col id for document
                    to= new int[] { R.id.itemr1,R.id.itemr2};
                    Log.e("booktitle is", title[1]);
                    Log.e("BookmarkTime is", title[2]);
                    resultbookMarklist.add(item);

                }
                //resultbookMarklist=getBookmark();
                SimpleAdapter adapter = new SimpleAdapter(GetAudioBookmarks.this, resultbookMarklist, R.layout.row3, from, to);
                lv.setAdapter(adapter); 


            }
4

2 回答 2

1

如果您的 Arraylist<> 更新并且在更新之后您正在调用 adapter.notifyDataSetChanged() ,它应该更新列表。

于 2012-09-05T04:47:10.007 回答
1

代替 SimpleAdapter 使用 ArrayAdapter 因为 SimpleAdapter 被用作静态数组的适配器。如果您仍想使用 SimpleAdapter,则改为调用 notifyDatasetChanged,创建一个新的适配器实例并设置为 listview。

于 2012-09-05T04:48:15.370 回答