I’m trying to implement a ListView on my app, but i’m trying to understand and learn how to achieve it without using XML files, all with java code.
I’m stuck on the inflater part, the mInflater.inflate(); function needs a resource xml file, so, i didn’t understand how to continue without using XML files
I have a ArrayList of strings, and I simply need a ListView that shows a list with these strings of the Arraylist, and a delete Button on the right of the String. If the user press the Button, the item of the List get’s deleted.
Each item of the ListView has two things, a TextView with the String of the ArrayList and a Button to delete it.
If someone can give me code examples i will be grateful.
Thanks
I totally agree with CommonsWare. But the part that you are stuck is the part that you have to create a row “template” for the
ListViewrows. Theinflateris used so as to make a singleViewout of a completelayout.xmlfile. So the basic idea is that you create an xml that represents each row and then inflate it through that piece of code.In your situation, you need to do that through code. Perhaps add a
LinearLayoutas a parent with orientation=vertical add some width or height properties and then add 2TextViews so as to be a title and a subtitle with some additional properties. Then you should add them to theLinearLayoutand you there you go.Your
LinearLayoutis a pile ofViews that are dynamically created and have the same effect as inflating all the above code through an xml file.But I reaaaaally don’t see the point in creating such a fuss over a much faster, easier, straight forwarded, better implemented and not to mention best practice…
EDIT: Somewhere inside your adapter you have:
mInflater.inflate();with the resource that you mention. As I previously said the resource determines how the “template” for each row would be. So a normal xml file that will determine a list row would be something like this:This xml produces a 2 line list row for a
ListView. With the layout inflater the above xml file returns aViewobject that contains all the bundle.So if you want to create it from code, then the snippet would be:
Now instead of inflating the xml to get the contents into a single
Viewobject, you have thelayoutvariable in the code snippet that contains all the logic that was previously inflated through the xml.If you have created a custom
ListViewadapter before then you should be familiar with creating a custom list row and how it works.EDIT: sample code for the adapter of the
ListViewthis is the standart procedure of
getView()method of the adapter by inflating a single layout:dynamic implementation: