How to use setAdapter? - java

I have a doubt related with setAdapter. Somebody could detail or explain the functions and how to understand a code like this.
mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, mServices));
detail for example:
new ArrayAdapter ; this.

A list adapter is an object that adapts a collection objects for display in a ListView. ArrayAdapter is one simple implementation that maps an array of objects.
mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, mServices));
This line is mapping an array of strings (mServices) for display in a ListView (mDrawerList). The second argument to the adapter's constructor is the layout that will be used to render each list item.
List adapters, and adapters in general are a long and complex topic that isn't going to be explained in an SO answer. Try these links,
http://developer.android.com/guide/topics/ui/layout/listview.html
http://developer.android.com/reference/android/widget/ListAdapter.html

Related

More detailed list view on android?

I'm trying to design a page for my app that will be a list of places. I need each list entry to have a picture, text below, short description, and some other info, address
The list view fragment examples that I've seen simply have one or two lines which won't really do what I need it to... where do go from here? I'm not sure what to search for, nothing useful has come up so far..
You are going to need to make a custom ArrayAdapter.
Check out some of these examples:
Android Custom Listview with Image and Text using ArrayAdapter
Android Custom ListView with Images and Text – Example
Customizing Android ListView Items with Custom ArrayAdapter

can you put an expandable list within an expandable list

is it possible to make an expandable list within an expandable list
i am making a recipe application and i've three expandable lists of the same type - vitamins, minerals and macro-nutrients created with expandablelistview with android studio
is it possible to put these three expandable lists under an expandable list called nutrition and if so could you briefly outline how please. thanks in advance
Short answer, yeah you can but you really really shouldn't.
I've tried this before and it didn't end well, I can't remember the exact reasons but conflicts arise between touch listeners and the measure passes when trying to layout the views inside the different adapters don't calculate correctly.
You can find a number of custom ExpandableLists trying to do what you want around the web but I found they also had similar issues.
What I ended up doing was building my own expandable listview. If you check the source code for Android's ExpandableListView and ExpandableListAdapter you'll find that it's basically
just a regular listview
a list of items that are visible
an Async filter for the data source
some meta data to tell if its a child or group, expanded or collapsed, etc
So if all your item are collapsed the visible list only contains the Group Items.
Expand a Group Item and the async filter gets triggered and populates the visible list with the Group Items and the Children of the expanded Group Item.
Depending on the meta data it will call either getGroupView or getChildView.
So if you want you can take a shot at writing your own.
I wrote a blog post for an ExpandableList that I wrote that does, in theory, infinite levels of expansion. NLevelExpandableListView

Android: 1 Adapter to multiple Array Lists

I have a base adapter and 5 array lists which contain different data.
Based on a selection, the contents of the array list are populated on the list view.
At the current moment this functions correctly, however each time, a new selection is made, an entire new adapter is being made and set. Would like to know if there is an alternative way of changing the data displayed without having to set a new adapter.,
I am fully aware of notifyDataSetChanged()
If you are using an ArrayAdapter (just a guess), then there are functions to change the used list (http://developer.android.com/reference/android/widget/ArrayAdapter.html):
clear()
addAll(Collection<t> c)

Android: How to clear a list view without removing data from the array list?

I am attempting to create a list view which displays different data based on a selection from an a spinner.
My list view is implemented via a base adapter which works fine.
My data is stored in an array list of objects which each contains fields that store individual data and based on the selection from the spinner filters the data stored in the array list and displays it on the list view.
But my question is how can I clear a list view content without clearing its underlying data (stored in the array list) and calling notifyDataSetChanged? I want to be able to preserve the data stored so if the user goes back to a previous selection, the data is still present.
Thank you for your time in reading this.
As your data is stored into an ArrayList, you can simply use:
listView.setAdapter(null);
It will remove the adapter of your ListView, and so, remove all the items displayed.
The data will still be in your ArrayList so if you want to re-display this ListView, you just have to recreate your Adapter and set it again. You can even create a global instance of your BaseAdapter so you only have to create it once.
You could just pass another ArrayList to the adapter with the new data to show inside the ListView.
Or, you could create two ArrayList inside the Adapter, one named original which contains the original elements of the ListView and another currentElements which will be the real ArrayList to show changed as you want. With some custom methods, you can choose to switch between the two versions of the ArrayList.
It's could be the same concept of an Adapter which implement a Filter.
Anyway if i understand your problem, you want to change the content of a ListView based on the selection. Well, in this case i would send another ArrayList to show.
Create a list of adapters, one adapter for each selection. Then simply switch to a different adapter when the selection is changed with listView.setAdapter(adapter)

Printing in TwoLineListView in android

I wonder if it is possible to print in TwoLineListView like in TextEdit? For example if I want to print some word in line1 and translation in line2? Is there anything like twolinedtextview.add("word,"translation"); ? I need my text to appear in listview when i press the button.
1. If you are using default ArrayAdapter with the ListView, then you can use ONLY ONE TextView.
2. But you can very well do this with the help of Custom adapter by using BaseAdapter or
ArrayAdapter..
See this link for an easy tutorial:
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
you can subclass ArrayAdapter class, override getView(), and construct your rows yourself. The getView() method is responsible for returning a View, representing the row for the supplied position in the adapter data.
If you don't want to extend ArrayAdapter (like the others suggested), you can always use a SimpleAdapter
Here's a short tutorial about using it for 2 line list.

Categories

Resources