How to use a custom Adapter in a Fragment - android

I have a simple question, I know you can use:
setListAdapter(new ArrayAdapter<String> (getActivity(),
R.layout.your_layout,
R.id.your_text,
stringArray));
inside a ListFragment on your onActivityCreated() method. This works well for me so far, but only covers one item. For instance, say if in the the layout, I had two TextViews: sms_text, and sms_timestamp; both to be filled by smsTextArray [] and smsTimeStampArray [] respectively. How would that call be? I tried:
setListAdapter(new ArrayAdapter<String> (getActivity(),
R.layout.tweet_list_item,
R.id.sms_text,
smsTextArray,
R.id.sms_timestamp,
smsTimeStampArray,
));
That did not work :(
I also tried calling setListAdapter() twice in the same onActivityCreated(), but although that worked, it only displayed the second call of setListAdapter() in the list items.
I also tried creating my own Adapter but I kind of got lost in that mess. Can you guys point me in the right direction? I do not need the exact code, just a link to a library I could read, or perhaps it is a simple as just a code call. Who knows, maybe I am just using the wrong parameters, any ideas?

You can't use a simple ArrayAdapter and display more than one piece of data. An adapter maps one element in the list to one element in the listView. To have data come from two different pieces you need to either create your own adapter (simpler than it sounds). Or alternatively you can use a list of Maps in a SimpleAdapter (see this for an example).
Personally, I'd recommend going for the custom Adapter. You're going to have to code one eventually, and there's no time like the present. Plus, you're code will be more readable and obvious.

Related

How to force my GridView adapter to reload?

I'm an Android beginner starting with fragments. My problem is the following.
I has a fragment that used to show a custom listview (it extended SherlockListFragment). I populated the listview with some data server. The action of retrieveing data of the server was executed in another thread and I had a handler to manage responses. In this handler (inside my fragment) I recovered data for my adapter and the I reloaded my adapter like this ((myAdapter) fragment.getListAdapter()).dataChange(); .
Well, now I wanted to convert the list in a custom grid cause I think it's much better for my app. So, the fragment doesn't extends SherlockListFragment, but BaseFragment. As I don't have a ListView anymore, I can't not use "getListAdapter.datachange()". Does anybody know the correct way to do this?
Thanks in advance.
Make an Adapter for the gridview that extends from BaseAdapter, or use an Adapter that extends from BaseAdapter.
then call notifyDataSetChanged on that adapter when the contents of your collection has changed.
Small example of adapter and grid
I would recommend making your changes on the Adapter, not the Grid (or list). Depending on the type of adapter you are using there are a couple different approaches.
ArrayAdapter
For an ArrayAdapter, using the standard modification methods (add, addAll, clear, remove) should update any views.
CursorAdapter
A CursorAdapter works in much the same way, except that you use either swapCursor or changeCursor to change the underlying data. That should notify all data set observers that the underlying data has changed.
All Adapters
If your observers don't get notified, usually because setNotifyOnChange was called, you can call mAdapter.notifyDataSetChanged() to manually update all listeners (UI views, services, etc.) that the underlying data has changed.
thanks a lot for your quick responses. I tried your ideas but finally I converted my old list in a grid by making a two columns' row. It was the best option to keep my header and footer on.

Append items to Simple Adapter dynamically

I have a very simple question. I'm working on an Android application where I make use of Simple Adapter for ListView. Now I need to append items to this ListView dynamically. I know it is possible to do so in case of Array Adapters.
It is done as follows:
adapter.notifyDataSetChanged();
Is there any equivalent for the same in case of Simple Adapters? I couldn't find much relevant links regarding this on the net.
Kindly help.
Thanks in advance!
I think not, but you always can create a new SimpleAdapter and call setAdapter again with the new adapter.
If you are going to add/remove elements you, probably, need some other adapter not a SimpleAdapter. Because they say in the docs that SimpleAdapter is 'An easy adapter to map static data to views defined in an XML file'

Is it okay to change a ListView's adapter dynamically?

Instead of creating multiple activities, I would like to change the ArrayAdapter of the ListView as needed. I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.
To be more specific, say I would like to start an activity that has a ListView. In this example, the ListView is initialized with a listView.setAdapter(this) from, say, a CategoryArrayAdapter.
Then a user selects a category. Without starting a new activity, the code will set a new adapter for the same ListView. The new adapter, say ItemArrayAdapter calls listView.setAdapter(this).
Does someone have experience having done this successfully or know of a specific reason why this shouldn't be done?
I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.
The simple answer is YES, and I have done similar sort of things before.
This is exactly the reason why Adapter is existed and provided in the API. The actual content (Model) and how it is rendered (View) for each list items is isolated and implemented inside android.widget.Adapter, instead of directly bound to android.widget.AdapterView. As long as your adapter is properly implemented, you can swap/change the actual underlying adapter that bound to the ListView, simply by calling the setAdapter() method.
Resetting the adapter is ok, but notice, that there might be a GUI glitch when doing so, as the view whose adapter is being changed has to be redrawn with the new data. Aside from this you should be fine.

Advice with ArrayAdpters

Can anyone tell me what exactly does an array adapter do? I've tried searching the net but all I get is code examples. Please explain me what it does, I've visited the android developers as well.
An ArrayAdapter can be used as a data source for a number of different Android Views, such as a ListView or a Spinner.
Basically, you pass some kind of array or list to the constructor of an ArrayAdapter. Then, the adapter can be hooked up to a ListView by calling setAdapter(). You can also use the add and remove methods of the adapter to modify the underlying list itself.
You can also use an ArrayAdapter to customize the appearance of items in a ListView for example (or other Views) by using the constructor and passing in the resource of a layout to use, or by overriding the getView() method and building it yourself.
Typically, an adapter is some kind of translator. It's the "man in the middle" who know how to dialog with both sides and convert what is said.
An arrayAdapter is a class which get datas from an array and format it for a listview or spinner to understand it. When a listview need the data 4, for example, it will ask the adapter who will return him the 4 element of the array.
Ok, the listview could directly use the array.But with the adapter you're allowed to use any kind of data source. An ArrayAdapter(subclass of adapter) uses an Array, but another adapter could use a database or a file or anything else.That way the listview is able to get datas directly from any source without knowing how to access it.That is the adapter's role.

simple_list_item_2 in Android

after creating some menus with simple_list_item_1 (which worked very fine) I tried to replace this by simple_list_item_2, but my system throws around with exceptions...
Now I'm wondering how to create such a two-different-sized-line-entry for my list...is there any trap for beginners? Can someone please help me fixing my (small!?) problem?
My code looks like this:
ListAdapter listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, fileNames);
setListAdapter(listAdapter);
My String[] fileNames stores all strings to display in ascending order (does this matter for the functionality?!)
After unsuccessfully searching in some forums I now hope that someone of you can give me an useful suggestion.
nice greetings,
poeschlorn
simple_list_item_2 is different, instead of just a TextView it contains a TwoLineListItem containing two TextViews. ArrayAdapter is not going to work here, because the constructor you're using expects only a TextView; just look at the constructors. Instead you'll either have to create a custom adapter or use one that supports it like SimpleCursorAdapter or (I think) SimpleAdapter. This guy has a somewhat hacky solution that might work for you.

Categories

Resources