In my application i have spinners,if there are 10 items in a spinner how can i delete some specific item (like 3rd or 4th) from that spinner i used below code but not succeeded.
for(int i = 0;i<3;i++) {
Object t= cropT.getItem(i);
((ArrayAdapter<String>) cropT).remove((String) t);
spinnerCropType.setAdapter(cropT);
}
You dont need to set adapter everytime your delete items from it. In fact, after removing items from your spinner, you need to call notifyDataSetChanged() method on your adapter to refresh the spinner
//for example
adapterSpinner.notifyDataSetChanged();
Related
I have problem woith my spinner.
I use it to show record retrieved from database. The problem is that after selection one of objects, it is not display as selected, and method spinner.getSelectedItem() return null.
I tried multiple combination, and I see, that the problem appears only when list is populated AFTER adapter is added to spinner.
initTestSpinner();
listother.add(new UserProfile("adam"));
listother.add(new UserProfile("maya"));
listother.add(new UserProfile("maria"));
private void initTestSpinner() {
testSpinner = findViewById(R.id.second_spinner);
adapter = new UserProfileListAdapter(this, listother);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
testSpinner.setAdapter(adapter);
}
If I put those values before initSpinner it works fine. But any change on array list causing same issue.
You should call adapter.notifyDataSetChanged(); after you update the list:
initTestSpinner();
listother.add(new UserProfile("adam"));
listother.add(new UserProfile("maya"));
listother.add(new UserProfile("maria"));
adapter.notifyDataSetChanged();
I have a list of items: List<SomeObject> items that is mapped to an adapter to show on a ListView. When I set a new field for a specific item in that list and call adapter.notifyDataSetChanged() then list doesn't update. For example, I want to show a date TextView in a specific row in the ListView by setting
item.get(position).showDate(true);
adapter.notifyDataSetChanged();
The view doesn't show until I scroll away from that row and back to it (I'm assuming because of recycling).
Doing list.setAdapter(adapter); works but the entire view flashes and there shouldn't be a reason to re-set the adapter.
How can I get the list to update without scrolling?
You could try to change the List that is referenced in the adapter. Add something like this to your adapter:
public void updateData(List<SomeObject> dataItems) {
this.mDataItems = dataItems;
notifyDataSetChanged();
}
And then:
items.get(position).showDate(true);
adapter.updateData(items);
you can use adapter.getitem(postion) to get SomeObject and refresh it.
SomeObject sobj = (SomeObject)adapter.getItem(position);
sobj .showDate(true);
adapter.notifyDataSetChanged();
I am using a collection of ArrayList to fill my Listview. My ListView contains two separate rows types.
Header and Footer.
I am trying to achieve the ExpandableListView Functionality on my Listview from which I am trying to remove some items on click of header till next header.
I am using this function to loop through items and removing items
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
items.remove(i);
}
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
QUESTION IS : I am not able to remove all items from the list in one shot, some stays there !
I have tried looping through adapter.count(); but no luck
My List :
SECTION 1
ITEM 1
ITEM 2
Item N
Section 2
But when I click on Section 1 not all ITEMS get deleted in one shot WHY!
I am not able to use Expandable Listview at this stage because activity contains many more complex functionality on List. Please help me where I am going wrong!
Create a new ArrayList<Collection> , Then add your item in it and then use removeAll(collection).
TRY THIS:
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
ArrayList<Collection> deleteItems= new ArrayList<Collection>();
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
deleteItems.add(items.get(i));
}
items.removeAll(deleteItems);
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
EDIT
Every time you are deleting an item, you are changing the index of the elements inside .
e.g : let suppose you are deleting list1 , then list[2] becomes list1 and hence your code will skip list1 next time because now your counter would be moved to 2.
Here are other ways by which you can achieve this also,
Removing item while iterating it
So what exactly I did now. Instead of looping through items again I did like this :
I created another list and parallely populate it with the main array.
items.add(user);
// after populating items did this
newItems.addAll(items); // same collection ArrayList
and finally I can play with the main array by using removeAll and addAll methods.
items.removeAll(newItems); // remove items
items.addAll(afterPosition,newItems); // add items after position
I have a list view with multiple items, where i need to select and deselect the list items, and also delete the selected items.
So i have looked into the example in the below link but its for android:minSdkVersion="11"
but i am working on minSdkVersion="10".
Link : http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
And yes we can do with checked text view, check box and radio button, but the requirement is like that i cannot use that.
Is there any other way that we can acheive this?
Make custom list adapter and get the click of that each view and maintain flag in adapter. If flag is true that means item selected otherwise item deselected, according to that you can change item view like disable that particular item or show some check box.
What I did was created an ArrayList that stores all the position of selected items, and toggle the background colors on clicks.
In my Adapter I define:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
With the following method :
public void toggleSelected(Integer position)
{
if(selectedIds.contains(position))
{
selectedIds.remove(position);
}
else
{
selectedIds.add(position);
}
}
which addes\removes items from the ArrayList
In my getView method :
if (selectedIds.contains(position)) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
else
{
convertView.setSelected(false);
convertView.setPressed(false);
convertView.setBackgroundColor(Color.parseColor("#000000"));
}
This checks if the position is storred in the ArrayList. if it does, paint it as selected. if not, the opposite.
all is left is the OnItemClick listener, i added :
((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));
When YourAdapter is the adapter of your ListView
Hope this helps anyone, as it's a generic answer :)
Thanks to eric.itzhak here : How to change background color of selected items in ListView?
In my application I have two spinner which one is using same adapter.
Spinner mSpinner1 = findSpinnerView(R.id.spinner1);
Spinner mSpinner2 = findSpinnerView(R.id.spinner2);
SpinnerCustomAdapter mAdapter = new SpinnerCustomAdapter(this,List<Food> foodList);
mSpinner1.setAdapter(mAdapter);
mSpinner2.setAdapter(mAdapter);
How could I remove or add items in adapter? More specifically when I select one of item that selected item should be remove when selecting another that items should be removed but the previous should be appear again.
I recommend that you do the deletion of spinner items this way:
foodList.remove(foodList.get(itempostoremove));
SpinnerCustomAdapter mAdapter = new SpinnerCustomAdapter(this, foodList);
mSpinner1.setAdapter(mAdapter);
you can change the foodList,add or delete
and use mAdapter.notifyDataSetChanged() to refresh view
OnItemSelected will be fired by this:
this.getAdapter().remove(currentWagon);
this.getAdapter().notifyDataSetChanged();
this.setAdapter(this.getAdapter());