I have a ArrayAdapter. I want to item with mixed. For example;
ArrayAdapter<String> Adapter = new ArrayAdapter<String>();
Adapter.add("Table");
Adapter.add("Desk");
Adapter.add("Pen");
Adapter.add("Computer");
Adapter.add("Mouse");
Adapter.add("Book");
I am adding items my ArrayAdapter. But I want to after adding, I add other item on under "Pen" item. For example;
Adapter.add("New Item");
But this item is last. I want to this item is adding under "Pen" item. How can I do ?
Use insert(T object, int index) instead. Refer http://developer.android.com/reference/android/widget/ArrayAdapter.html
Related
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 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());
I have two listviews SAMPLE IMAGE HERE.i want to move items from one listview to another.
i have two buttons,"move to right" and "move to left".
Its a multi selection listview.after select items we need to move this items to another listview.
First listview data from database.code shown below
public void fillcategory() {
Cursor cursor = dataBase
.select("SELECT * FROM t_Category ORDER BY CategoryName");
lacategory = new list(this,
android.R.layout.simple_list_item_activated_1, cursor,
new String[] { "CategoryName" }, new int[] { android.R.id.text1 },1);
lvcategory.setAdapter(lacategory);
lvcategory.setOnItemClickListener(new listclick());
lvcategory.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
There are twio options you can do this, first by adding checkbox in each listitem and then populate the other listview with selected items. for this here is good tutorial Multiple Check
Second, you can set you listview property to multiple_select in adapter and then populated those selected item through position in other listview.
I hope atleast one will work.
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();