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.
Related
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.
I have created custom listview with baseadapter.
it's working fine.
but I am calculating total amout when adatper is filliup via getview method.
My question is when I can find that adapter has completed his process of filling up listview?
When adapter called getview() method for last the record after that I want to broadcast message. How can I do?
Thanks.
Adapter does not fill listview completely. It will only fill views that are on the screen ( it also depends on framework cos companies like Samsung, Sony do change the android framework a lot)
The best you should do is call Notifydatasetchanged on adapter and then handle call after that.
Views and whatever adapter needs to call should be done by then
Can you please explain more what you want to achieve. You probably need to use the adapter data and not think about touching its UI part?
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.
I am trying to get a button to show up on my list item (declared as android:visibility:"gone" in the XML) to show as visible when I perform some gestures on it. However, how can I actually notify the getView method correctly to display the button only on the listview item?
I tried using getChildAt(position) which ended up displaying several buttons at once.
I tried passing in the position for example I detected that the gesture was performed on from pointToPosition and passed it into the adapter for the getView method to display, but it had the same problem of displaying several at once.
do anyone know how can I solve this?
I think you may have a misunderstanding of how Adapter.getView() works its meant to create or reuse layouts when rendering the ListView it also needs to be fast so conditional manipulation in this method is discouraged. Although ListView.getChildAt() may work it does not effectively use the API. Your adapter will have a setViewBinder() unless you're using an ArrayAdapter (if so I suggest using SimpleAdapter because of the additional features). Use your ViewBinder implementation to switch the visibility of the button.
If you'd prefer to continue to use ArrayAdapter use ListView.getChildAt(int) to findViewById(R.id.your_button).setVisibility(). If this is what you already tried and its setting all the buttons visible then please post the related code.
i implemented a listview which implements SectionIndexer ...
everything fine so far.
Normally the items are sorted by Name, but i also offer the option to sort the list in a different way - by distance (from the user to the objects).
So, when the list is sorted the 2nd way, i want to hide the previously generated SectionIndex.
I'm just not able to do so.
I tried, re-writting most of the methods,
I tried it with a separation in the Constructor (clear why it doesnt work, it doesnt get called a second time)
I even tried it with implementing a second listadapter, and just using a different one? Even in this case the SEctionIndex is shown! I really don't understand this one.
So would be really great, if anyone knows whats going on :)
thanks a lot, mike
Your observations are correct. Let me tell you first why the constructor never gets called the second time. SectionIndexer are a special kind. They create the index only once for a particular set of data and re-use them on that adapter. The bigger issue which I had come across was when the underlying data changed for the adapter, the sectionIndexer still remained the same.
Check my Question and the answer there.
Coming back to your query here.
If you change the orientation after selecting the second option, you would observe that the constructor will get called and you will be able to re-populate the sectionIndex again. So basically you need to call onSizeChanged again and get the sectionIndex repopulated.
When you Short your List with different way ,you have a two option to load again .
after filled those new collection for adapter
1) you can make a notify this adapter .
2) you can fill set adapter again .
If by SectionIndexer which remains displayed you mean the section overlay you can achieve this by calling setFastScrollEnabled(false) before to switch to your other listadapter which does not implements SectionIndexer.