0

i am facing a problem like , i am developing an android application where all the records coming from database using REST call and displayed using ListView. now on the same screen one button is there to post the new record.

when user click in the button to post the new recored it will come as popup(DialogFragment). if user post any record from there, the record should prepend to the ListView without any rest call.

the user should feel that recored add to the db and immediately displayed on the screen.

Thanks in advance and any help is appreciated.

Nirmal Dhara
  • 2,121
  • 18
  • 27

1 Answers1

0

So, I guess you are using a list adapter which extends from BaseAdapter or ArrayAdapter. After the user added a new record, you should add this into the list of records and than call the method notifyDataSetChanged() of the adapter. This way, the new record will be immediately displayed on the screen and you can later on add it into your database (before the activity be destroyed). Here you can see a example with notifyDataSetChanged()

How to correctly implement BaseAdapter.notifyDataSetChanged() in Android

Andy
  • 462
  • 3
  • 8
  • HI Thanks for the answer. yes that can be the approach but the problem is the arrayList(for ArrayAdapter) is in different class not in the same popup class. the ArrayAdapter and the popup are two different class. – Nirmal Dhara Mar 17 '15 at 09:34
  • How about creating a public method where the Arraylist is to add elements on it? – Andy Mar 17 '15 at 12:01
  • No i did not get it , it may loose the old value of ArrayList – Nirmal Dhara Mar 17 '15 at 13:11
  • Where do you call the popup class? Does it extend from DialogFragment? Or something similar? From the beggining I'am guessing things about your implementation, since I didn't take a look on it... for example, I thouth you have a class that controls the listView (with the arrayList and its adapter) and there you call the dialog (popup)... Could you tell me more about it? – Andy Mar 17 '15 at 15:55
  • I have an activity A(ListView is in Activity A) from where i call the DialogFragment. If user click on ok button of the DialogFragment then popup will close and send the data to database. at the same time i want the data in my ListView which is in activity A. – Nirmal Dhara Mar 17 '15 at 16:26
  • I suggest you to take a look in how to return data from a DialogFragment. I found this answer here: http://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment – Andy Mar 17 '15 at 16:45
  • I didn't test it, but it seems to work. I'm sorry I took so long to get what you need – Andy Mar 17 '15 at 16:46