Rakesh
Rakesh

Reputation: 1133

How to create custom dynamic list in android?

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 ?

Upvotes: 0

Views: 2022

Answers (1)

quick release
quick release

Reputation: 288

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

Upvotes: 1

Related Questions