I have a list (of messages) and I want to give the user the ability to remove these items from the list. I have extended an ArrayAdapter and give it an ArrayList of my messages and would like to simply remove an item from that list and then refresh the ListView instead of reloading the entire list of sent messages. The problem is, if there's only one message and I remove it using listAdapter.remove(messageObject), the adapter is still calling getView() and then throwing NullPointerExceptions all over the place. I'm not sure what the best way is to go about this.
Apparently things go a little haywire if you don't override the getCount() function in your custom ArrayAdapter. I set it to the size of my ArrayList and now everything seems to be working correctly.
Related
I have 2 recycleviews and both of them have a number of icons. I also have a list that contains a number, which is the number that represents which icon has a background that shows it has been clicked.
Now, the problem is that I only want one icon marked, with this I mean 1 item in total of those 2 recyclewviews. The way I've tried to do this is having a json that saves a number that determines which recycleview should have a marked item, if it doesn't have the number of the recycleview, then the background is the normal one, that way only one recycleview has a marked item.
The problem is that for that to work I need to call the method Notifydataserchanged() on the other adapter so the marked background dissappears, and the easiest way of doing that is putting the other adapter on the constructor. The problem with this is that only works for one, because since the other is declared before you can't construct it with the adapter that comes after it.
I've seen way of doing it with listeners/interfaces made in the activity and then
moved to the adapter itself so you can just call those methods, but it hasn't worked for me.
Any ideas? Thanks
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.
I want to design a list with options to sort it in ascending or descending orders. I am able to do that using different intents. Is there a way to do that without having a new intent?
The example that comes to my mind is Manage Applications in android. Sort by name and size happen on the same screen. How is that done?
Edit - I have a list of say 20 items. Right now, I am sorting the list items and displaying only the top 5 items. I want to add an option to display the bottom 5 items too. I have done that by creating a new class exactle the same as prev class with top array replaced by bottom array.
You can re-sort the underlying data structure before setting it in the adapter.
Usually a list is displayed using a ListView.
ListViews usually have an adapter associated with them. The Views are listed in the order they exist in the adapter. So to change the order of elements in the list , all you have to do is set a new adapter to it where the the elements are ordered as you wish (You could also try to change of the order of elements of the existing adapter, but I don't know if it can be done).
You can see the ListView Hello World Example to have a better understanding of ListViews.
Umang, I don't have eclipse on hand now and I just want to express my train of thought to solve your problem. Please write the codes yourself and test it.
There must be an adapter associated with your listview, inside it, you can set a variable to moniter the order mode, like: private int mode;
Inside the getView() method of the adapter, based on the mode, return the items as you want.
I think you have finished the steps above, since you can change the order already.
In the first Activity, you can set some event for the user to chage the order, i.e, via the Menu. So you override public boolean onCreateOptionsMenu (Menu menu).
In the corresponding public boolean onOptionsItemSelected (MenuItem item), you set the mode value based on the item index the user selected.
Call adapter.notifyDataSetChanged() to notify the system that the data for the listView has been changed.
call listView.invalidate() to show the new ListView.
The ArrayAdapter class has a built-in method for sorting via "sort (Comparator comparator)", provided you are willing to go to the trouble to write the Comparators that you need to do the sorting. I use this method to sort "in place", without having to re-load the adapter with an externally re-ordered list.
Check out http://developer.android.com/reference/android/widget/ArrayAdapter.html
and http://developer.android.com/reference/java/util/Comparator.html
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.
I want to have a Button that when clicked, removes all the checked items in the ListView. I already have all the xml items set up, I just don't know how to write the java code.
The ListView displays data that comes from an Adapter. In order to remove items from the view the item needs to be removed from the Adapter and the view notified. In android the Adapter notifies the view by calling notifyDataSetChanged().
How to remove an item from the adapter depends on your particular adapter. The SimpleCursorAdapter gets its data from an underlying Cursor. To remove an item, the item should be removed from the underlying Cursor. For example using a SQLiteCursor a row in the database needs to be deleted.
If you use the ArrayAdapter just call remove(T object) on the adapter. It will automagically call notifyDataSetChanged() for you.
update:
I saw the code at git hub. Here are some pointers as how to get your app working as soon as possible.
Try refactoring your code in to smaller graspable parts. Start with extracting some methods to give parts of the large method understandable names.
The problem is that there might by hundreds of rows in the database and only enough views to fill the screen. Nowhere is it remebered what rows are checked, hence its not possible to remove them. You probably need to extend BaseAdapter or SimpleCursorAdapter to hold the state (checked or not) of the rows. Read up on the excellent android documentation.
My point here is there is a distinction between the view, your CheckBox, and the model containing the data to display. So check out Model-View-Controller. You can ignore the concept of controller for now.