Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kevin Haube
12,265 PointsRemoving item from ListView?
final String[] exampleData = new String[] {
"How old am I?",
"What's my middle name?",
"What is my major?",
"What's my girlfriend's name?",
"Is this an example?"
};
final ListView questionList = (ListView) rootView.findViewById(R.id.questionList);
final ArrayAdapter<String> questionListAdapter = new ArrayAdapter<String>(
getActivity(),
R.layout.item_question,
R.id.questionText,
exampleData
);
final DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
//Todo link to question at "which"
Intent edit = new Intent(getActivity(), EditQuestionActivity.class);
startActivity(edit);
break;
case 1:
//Todo: delete question from quiz object
questionListAdapter.remove(questionListAdapter.getItem(which));
questionListAdapter.notifyDataSetChanged();
break;
}
}
};
So, my object is to present an option list on LONG click, which is successful, and upon chosing the Delete option (at index 1), it should remove the item found at the long click index. But I recieve this error...
java.lang.UnsupportedOperationException
...pointint to this line...
questionListAdapter.remove(questionListAdapter.getItem(which));
Any suggestions?!
1 Answer
Ratik Sharma
32,885 PointsYou use the following:
questionListAdapter.remove(questionListAdapter.getItem(which));
but note that 'which' is the value of the item clicked within the Dialog (it takes values depending on the number of list items in the Dialog).
Potential fix: Try to get the position of the list item which was long clicked and remove the element of the ArrayAdapter with that. That should do it! Let me know how it goes. :)
Kevin Haube
12,265 PointsKevin Haube
12,265 PointsOh! I see. The
int whichvalue is telling the onClick method that I clicked either the Edit or Delete options (indexes 0 and 1 of the dialog). I'll append this comment with an updated version of the code that works, once I fix it! Thanks for the help!!!Kevin Haube
12,265 PointsKevin Haube
12,265 PointsUpdate:
So, this is my new OnItemLongClickListener with the AlertDialog's onClick method set as an inner-method. I still get the same error pointing to the same line of code, though I've changed
whichtopositionto get the item's position in the list.Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherSorry for the delay here! This is an interesting issue. You are using a static array (
exampleData) as the data behind your adapter. We aren't allowed toaddorremovefrom a static array like that. Even though theArrayAdapterclass has the methods to do so, they won't work on this type of array.You should be able to switch
exampleDatato a dynamic collection, like an ArrayList. Then you'll be able to add or remove using the adapter.Hope this helps! Let us know if you are still stuck.
Kevin Haube
12,265 PointsKevin Haube
12,265 PointsIt worked! Here's a snippit of the game's "QuestionListFragment" class...
Everything is working 100 perfect. :)