0

I want to create a custom list in which items are added or removed dynamically (say when a button is tapped). The problem is I have very little knowledge of lists in android. I have gone through various tutorials on creating a custom list in android but none of them shows how to dynamically add contents to it

What I know so far:

1) I have to create a model class to store data.

2) I have to create an adapter class.

3) Pass the objects of the model class as an arraylist to the adapter.

3) Bind listview to the adapter

What's confusing me:

1) I know I have to create an apapter class, but what's really confusing me is what kind of adapter ? i.e. ArrayAdapter, BaseAdapter ??

2) What and How will I feed adapter? I will be fetching data from the Sql lite database and I want the results to be displayed in my custom made list.

3) How will I update my list when a new record is added to the database ? I know how to populate listview from a static array but its of no use in my project.

I need little guidance where should I start from ?

Rakesh
  • 1,133
  • 4
  • 13
  • 28
  • Hmmm...you need to calm down...just implement simple custom adapter form any of the tutorial...then add/remove items at runtime would not be an issue.... – umair.ali Mar 13 '14 at 11:27
  • Thanks for the quick response umair.ali. But that's the problem, I can implement a custom list that can fetch data from an array, Infact I have made one. But I don't know how to add a new item to it. – Rakesh Mar 13 '14 at 11:30
  • Its simple than...just put new item in your array...then make you adapter to refresh the list view like yourAdapter.notifyDatasetChanged(); – umair.ali Mar 13 '14 at 11:33
  • 1
    see http://stackoverflow.com/questions/4540754/add-dynamically-elements-to-a-listview-android – super-qua Mar 13 '14 at 11:33
  • Thanks, I will surely try it. – Rakesh Mar 13 '14 at 11:43
  • 1
    Does this answer your question? [How to add elements to a listView dynamically in Android](https://stackoverflow.com/questions/4540754/how-to-add-elements-to-a-listview-dynamically-in-android) – Josh Correia Aug 21 '20 at 21:48

1 Answers1

1

1) You can use ArrayAdapter. 2) After you create your own arraylist, you can pass it for first time, listview.setAdapter(... 3) After you refresh your data you can call this method, ((ArrayAdapter)listView.getAdapter).notifyDataSetChanged(). This will ensure your listview refresh.

The below link is a good example: https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

quick release
  • 288
  • 2
  • 9
  • Thanks, That really helped me a lot in understanding the concepts behind listView. – Rakesh Mar 13 '14 at 12:03
  • Don't mind but I am still confused between BaseAdapter and ArrayAdapter. The link you provided is using ArrayAdapter while this one is using BaseAdapter : http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92 – Rakesh Mar 13 '14 at 12:06