UnsupportedOperationException with ArrayAdapter.remove [duplicate] - java

This question already has an answer here:
Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
(1 answer)
Closed 10 years ago.
In my code I have a ListActivity. One of the context menu options for a list item is "delete", which opens a dialog confirming the action. I intended to implement this functionality by first deleting the item's data in the database and then removing it from the ArrayAdapter. It is in removing it from the ArrayAdapter that I get an UnsupportedOperationException...
public void onClick(DialogInterface dialog, int id)
{
asynchronousDeleteEntry(CONTEXT_SELECTED_ID);
dialog.dismiss();
//I -know- that the adapter will always be an object
//of ArrayAdapter<JournalEntry> because this is the only type
//I ever call setListAdapter with. Debugging confirms this
#SuppressWarnings("unchecked")
final ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>)
journalViewerListActivity.this.getListAdapter();
//EXCEPTION OCCURS HERE
adapter.remove(adapter.getItem(CONTEXT_SELECTED_POSITION));
//refreshes the ListView to show the new items
adapter.notifyDataSetChanged();
Any help appreciated.
Thanks!

It seems that this problem crops up when you initialize your ArrayAdapter with an array. Try initializing it with a List<JournalEntry>. Reference: Why can't one add/remove items from an ArrayAdapter?

You're trying to modify list which is declared as final. Compiler tried to warn you, but you've suppressed warning by #SuppressWarnings("unchecked")

Related

How do I make Recycler view replace existing items instead of adding to it?

I've figured out how to get the sorted data with a query and pass that to the Recycler view, but it only adds to the RecyclerView instead of replacing the already existing items. Uploaded the code to Pastebin since code irrelevant to the problem is in the class.
MyDatabaseHelper class = https://pastebin.com/fXHw5ccb
CustomAdapter class = https://pastebin.com/4epLqWzJ
MainActivity class = https://pastebin.com/h5mDQunv
Including
customAdapter.notifyDataSetChanged();
recyclerView.setAdapter(customAdapter);
or removing it doesn't solve the problem.
How it looks: https://i.imgur.com/C60ur8b.png
How I want it to look after clicking the menu sort item: https://i.imgur.com/wjZP9yX.png
Solved: Had to pass Custom Adapter to a custom object and read the ArrayList from the object, which let me sort based on SQL queries.
It looks like you call storeFoodDataInArrayList() multiple times which populates the food_ID array, but you never clear that array.
Clear the food_ID array before populating it.

Who calls the getView() method of custom ArrayAdapter class

While using custom ArrayAdapter in android, the getView() of the custom ArrayAdapter is called by whom? I have searched this question a number of times... found it in StackOverflow but the answer which has been marked as satisfactory by the user is:
for the developer it is not necessary to know that who calls the
getView() of the ArrayAdapter class".
Please, I humbly request that a more meaningful answer will be appreciated!
getView() is called by a subclass of AdapterView. So, if you attach the Adapter to a ListView, ListView inherits from AbsListView, and AbsListView will be calling getView() to get the rows to display in the list.

Populating a custom listview from List, not an ArrayList

I am trying to populate a custom Android ListView from a List. I am using sugar ORM to save and get data from a database. In my Activity I use the following 2 lines to get the data, and put it in a simple listview.
List<Fish> fishList = Select.from(Fish.class).orderBy("species").list();
setListAdapter(new ArrayAdapter<Fish>(this,android.R.layout.simple_list_item_1, fishList));
This works as expected and displays the getString() method from the Fish class.
Every method I have found for populating a custom listview uses ArrayList but fishList is just a List. I have tried probably 4 or 5 tutorials and they all come up short. Most just describe populating a list manually, but not how to do it if I already have the data in a database.
I have the ArrayAdapter classes set up and at one point had it working where it would just load the most recent object into the list. I suspected some kind of loop was needed but couldn't get that working after hours of trying.
Is there a way to populate a custom listview using my List, or do I need to convert it to an ArrayList somehow, and if so, how do I do that for all items in the List?
Can I replace the first line so it gets the data from the database and puts it in an ArrayList right away?
I apologize if this is super simple (I'm sure it is) but I have spent 4 days trying to find a solution before resorting to asking a question here. Thank you so much for any help you can provide.
Found Exactly what I needed here:ListView Populating using Custom Class
In my My onCreate()
adapter = new ListAdapter(this);
listView = getListView();
List<Fish> fishList = Select.from(Fish.class).orderBy("species").list(); //get data from database (SugarORM)
adapter.setData(fishList);
listView.setAdapter(adapter);
Use a BaseAdapter:
http://developer.android.com/reference/android/widget/BaseAdapter.html
Refer to this tutorial:
http://www.vogella.com/tutorials/AndroidListView/article.html

javafx listview item order [duplicate]

This question already has an answer here:
How to create a reorder-able TableView in JavaFx
(1 answer)
Closed 7 years ago.
I'm having trouble with JavaFXs ListView items. The thing is that I can not find a way to reorder them at runtime. Just with dragging item with mouse. Also I want to change items order in underlaying ObservableList.
How could this be done?
You can change the order of the ObservableList that forms the underlying model of the ListView at any time to change the order of your list view items...
... for example: to use a Comparator to re-order the contents of your list view you could use something like:
javafx.scene.control.ListView<TYPE> listView; // Your ListView, defined somewhere else.
java.util.Collections.sort(listView.getItems(), new java.util.Comparator<TYPE>() {
#Override
public int compare(TYPE o1, TYPE o2) {
// Implement your comparator here.
}
});
... whereas "TYPE" matches the type of your ListView instance.
See http://docs.oracle.com/javafx/2/api/javafx/scene/control/ListView.html#itemsProperty for further details of the list view items definition.
You can add, remove and shuffle the items around as you like and the ListView will automatically adjust it's appearance...

Checkbox in ListView with odd checking behaviour

I am new to Java and Andriod development.
I have a view within my app which contains a ListView
I then have a custom Adapter which inherits from an ArrayAdapter
within the view in the getView method I return a row which contains some text and a checkbox
now so far this all works great my list is populated and I can check my items, and all the events seem to fire as expected.
My problem that I am having is that I check the first 3 items and then I notice that the 11th, 12th and 13th items are checked and as I scroll I see that at regular intervals other checkboxes also seem to be checked in the same pattern.
If I check about 10 checkboxes then it will end up checking all the items in the list of about 80...
can anyone explain what I have done wrong?
I dont think any code will help explain this as I do not set the checkstate of the checkbox anywhere, this is all handled itself, so the fact items are being checked is puzzling me.
Thanks in advance
This is happening because Android recycles list items for performance purposes, here you have the solution of this problem:
ListView reusing views when ... I don't want it to
Hope to help :)
This is expected behavior. ListView reuses views in the adapter.
So what you want is to keep a Set of "selected items" and add an item or remove it when the CheckBox is clicked and then in your getView add the following code:
if(mSelected.contains(getItem(position)) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
This way when the row is reused the CheckBox will be the proper status.

Categories