Vilasmati Hanjagi
Vilasmati Hanjagi

Reputation: 20

How do I update listview dynamically(new items )?

I am working on small android application,I want to display newly added items on list view .I am fetching the items from database storing in ArrayList<HashMap<String,String>>() because I used SimpleAdapter .I am getting the newly added elements into ArrayList<HashMap<String,String>>() but not displaying on listview even though I used notifyDataSetChanged() to update listview.Please help me to solve this problem.

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); 


            }

Upvotes: 0

Views: 484

Answers (2)

jeet
jeet

Reputation: 29199

Instead of SimpleAdapter use ArrayAdapter cause SimpleAdapter is used as an adapter of Static Array. and if you still want to use SimpleAdapter then instead invoking notifyDatasetChanged, create a new adapter instance and set to listview.

Upvotes: 1

ankita gahoi
ankita gahoi

Reputation: 1562

If your Arraylist<> updates and after updating you are calling adapter.notifyDataSetChanged() ,it should update list.

Upvotes: 1

Related Questions