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 ?

6
  • 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.... Commented Mar 13, 2014 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. Commented Mar 13, 2014 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(); Commented Mar 13, 2014 at 11:33
  • 1
    see stackoverflow.com/questions/4540754/… Commented Mar 13, 2014 at 11:33
  • 1
    Does this answer your question? How to add elements to a listView dynamically in Android Commented Aug 21, 2020 at 21:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, That really helped me a lot in understanding the concepts behind listView.
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 : androidexample.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.