I am offering products in the menuItemArrayList and as the user places products of their choice in the shopping cart, they will be added to the selectionItemArrayList. Here is my code that checks to see if the product already exists in the shopping cart. If so then only the quantity gets updated.
I have twiddling around with the code but I keep getting this error
IndexOutOfBoundsException: Index: 0, Size: 0
The code is just an extract from an onClickListener from the recyclerview's onBindHolder:
int position = getAdapterPosition();
for (int j = 0; j <= selectionItemArrayList.size(); j++) {
if (menuItemArrayList.get(position).getMenuItemName().equals(selectionItemArrayList.get(j).selectionName)) { // Loop through selection array to see if item exists in array
selectionItemArrayList.get(j).selectionQuantity += 1; // if it does exist then only update the quantity by 1
} else {
// Get the item name, price and add 1 to the quantity
String menuItemName = menuItemArrayList.get(position).getMenuItemName();
String menuItemPrice = menuItemArrayList.get(position).getMenuItemPrice();
SelectionItem selectionItems = new SelectionItem(menuItemName, menuItemPrice, 1);
selectionItemArrayList.add(selectionItems);
}
}
Any idea what I am doing wrong? The exception is thrown on the line where I compare the product name in the main array with the one in the cart array.
This is your loop:
for (int j = 0; j <= selectionItemArrayList.size(); j++)
When iterating over a list, you almost always want to use <, and not <=. This is because lists indices start from 0. So a list of size 2 has items at 0 and 1.
When you use <= on a list with zero elements, it will still try to access the element at index 0 (because 0 is <= 0). But your list is empty; it has no element at index 0. So you crash.
Try this instead:
for (int j = 0; j < selectionItemArrayList.size(); j++)
I have an ExpandableListView and a checkbox to remove items depending on a users preference. It "technically" works but the issue I am having is I have to click the checkbox 4 or 5 times to actually remove all of the needed entries.
Any ideas what I am missing?
chkOnline.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
for(int i = 0; i < devices.size(); i++){
if(!devices.get(i).isOnline()){
devices.remove(i);
}
}
adapter.notifyDataSetChanged();
}
}
});
The problem here is that you are changing the size of the list while removing its items:
Suppose your list had a size of 3:
//first iteration
i = 0, limit = 2 (i < size())
l = [1,2,3]
1 is online => l = [2,3]
//second iteration
i = 1, limit = 1 (because size() now returns 2 and i < size())
l = [2,3]
And the loop ends without checking the last element (3) on the original list.
To fix this you can simply iterate from end to start:
for(int i = devices.size() - 1; i >= 0; i--){
if(!devices.get(i).isOnline()){
devices.remove(i);
}
}
The error:
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at WeekView.getMoreEvents(WeekView.java:614)
In a class that extends View, I get a list of the id in this way:
public void setElencoIdOperatori(int[] id_nome_op){
this.id_nome_op = id_nome_op;
}
Then, with this method, I must compare id_nome_op with eventRect.event.getIdOperatore(). When are the same, I want to add the event to the array eventRects. Thanks for your help
private void getMoreEvents(Calendar day){
// Get more events if the month is changed.
if(mEventRects ==null)
mEventRects =newArrayList<EventRect>();
if(mMonthChangeListener ==null&&!isInEditMode())
thrownewIllegalStateException("You must provide a MonthChangeListener");
// If a refresh was requested then reset some variables.
if(mRefreshEvents){
mEventRects.clear();
mFetchedMonths =newint[3];
}
...
// Get events of this month.
if(mFetchedMonths[1]<1||mFetchedMonths[1]!= day.get(Calendar.MONTH)+1||mRefreshEvents){
if(!containsValue(lastFetchedMonth,day.get(Calendar.MONTH)+1)&&!isInEditMode()){
List<WeekViewEvent> events =mMonthChangeListener.onMonthChange(day.get(Calendar.YEAR),day.get(Calendar.MONTH)+1);
sortEvents(events);
for(WeekViewEventevent: events){
cacheEvent(event);
}
}
mFetchedMonths[1]= day.get(Calendar.MONTH)+1;
}
...
// Prepare to calculate positions of each events.
ArrayList<EventRect> tempEvents =newArrayList<EventRect>(mEventRects);
mEventRects =newArrayList<EventRect>();
for(intj =1;j <=id_nome_op.length;){
ArrayList<EventRect> eventRects =newArrayList<EventRect>();
for(EventRect eventRect : tempEvents){
if(eventRect.event.getIdOperatore() == id_nome_op[j])
eventRects.add(eventRect);
}
computePositionOfEvents(eventRects);
j++;
}
}
It's a bit hard understanding your code, but I think you might have a problem here:
for(int j =1;j <=id_nome_op.length;) {
ArrayList<EventRect> eventRects =newArrayList<EventRect>();
for(EventRect eventRect : tempEvents){
if(eventRect.event.getIdOperatore() == id_nome_op[j])
eventRects.add(eventRect);
}
computePositionOfEvents(eventRects);
j++;
}
When you access id_nome_op[j].
Try changing the loop to:
j < id_nome_op.length
As #Mike M pointed out, you also need initalizing your outer loop to 0, as you want to get all the items in your array:
for(int j = 0; j < id_nome_op.length;)
I am using a multi select listview in my application. Specifically the simple_list_item_activated_1.
I have some of code, a button, that will select all the listview items. I have some logic saying that if all the items are already selected then deselect all the items.
When I press the button the first time, it will select all the items in the list as expected. and when I press the button a second time it deselects all the items as expected.
Here is my problem:
When I press the button for a third time "selectedCount" still equals "childCount". So obviously my code will never enter the If statement.
Would anyone know why this is happening? or maybe there is a better way to do what im trying to achieve?
int childCount = officerList.getChildCount();
int selectedCount = officerList.getCheckedItemPositions().size();
if(childCount != selectedCount){
for (int i = 0; i < officerList.getChildCount(); i++) {
officerList.setItemChecked(i, true);
}
}else{
for (int i = 0; i < officerList.getChildCount(); i++) {
officerList.setItemChecked(i, false);
}
}
}
Try this logic, it will check all the items if none of the items are checked, else will check only the items which are unchecked and vice versa.
public void onClick(View v) {
SparseBooleanArray sparseBooleanArray = officerList.getCheckedItemPositions();
if(sparseBooleanArray != null && sparseBooleanArray.size() >0) {
for (int index = 0; index < sparseBooleanArray.size(); index++) {
if(sparseBooleanArray.valueAt(index)){
officerList.setItemChecked(sparseBooleanArray.keyAt(index),true);
}
else {
officerList.setItemChecked(sparseBooleanArray.keyAt(index),false);
}
}
}
else {
for (int index = 0; index < officerList.getCount(); index++) {
officerList.setItemChecked(index,true);
}
}
}
I managed to answer my own question. Using getCheckItemPositions().size() is an unreliable way of achieving what I want.
This will return a sparseBooleanArray() of all Items checked, so the first time it works correctly as initially there is nothing selected so it will return 0. Then once everything is selected the sparseBooleanArray will be equal to all the items in the list as they were all selected.
However as I learned spareBooleanArray is an Array that stores the position and a Boolean flag of if it is selected or not. In my scenario when I press the select all button for the third, the size of the array is still equal to number of list items.
How I have fixed my issue, is to use getCheckedItemCount() which only returns the number of selected items as I originally wanted. hopefully this answer will help someone else.
int childCount = officerList.getChildCount();
int selectedCount = officerList.getCheckedItemCount();
if(childCount != selectedCount){
for (int i = 0; i < officerList.getChildCount(); i++) {
officerList.setItemChecked(i, true);
}
}else{
for (int i = 0; i < officerList.getChildCount(); i++) {
officerList.setItemChecked(i, false);
}
}
}
I am using an array adapter and to this am adding an array list of string s , the list is multi select , How can i get the values of list items clicked ?
my_contacts_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,conts_list);
my_contacts_list.setAdapter(adapter);
I was trying to do this ,
SparseBooleanArray positions = my_contacts_list.getCheckedItemPositions();
int size=positions.size();
int i=0;
while(i <= size){
conts_list.get(positions.get(i));
i++;
}
But position.get(i) is an array list , how to retrieve the selected items then ?
SparseBooleanArray.get returns a boolean, but I believe you need to check it for each position in your list, e.g.
int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = cont_list.get(i);
/* do whatever you want with the checked item */
}
This API is a mess. Here is what works for me.
SparseBooleanArray checked = tags.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
if(checked.valueAt(i)) {
Tag tag = (Tag) tags.getItemAtPosition(checked.keyAt(i));
Log.i("xxxx", i + " " + tag);
}
}
I believe the fastest way to get the info out of this SparseArray is to iterate over the keys (actually I'm fairly sure that the solutions above won't work in all cases). The ListView will enter a pair (index, true) into the SparseBooleanArray for every selected index.
So the code might look like this:
SparseBooleanArray checked = lv.getCheckedItemPositions();
int size = checked.size(); // number of name-value pairs in the array
for (int i = 0; i < size; i++) {
int key = checked.keyAt(i);
boolean value = checked.get(key);
if (value)
doSomethingWithSelectedIndex(key);
}
I think the Answer from Daren Robbins is Wrong, here is my answer:
ArrayList<String> ids = extras.getStringArrayList("commonids");
SparseBooleanArray checked = lv.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
if(checked.get(i))
Log.i("CheckedItem", ids.get(checked.indexOfKey(i)));
}
Assume ids is an arraylist with the same size of the listview containing the ids of the items in the list view
The thing is you must iterate all the list view items but not checkedPositions.
Define the variables:
listView (The instance of you ListView)
names (the ArrayList you are )
saveCheckedName (save all checked name to this Arraylist)
SparseBooleanArray checkedPositions = listView.getCheckedItemPositions();
for (int i = 0; i < subjectListView.getCount(); i++) {
if (checkedPositions.get(i) == true) {
saveCheckedName.add(names.get(i));
}
}
Like so many other things, multi-select ListViews are a real problem in Android.
Instead of simply requesting the selected items as a List of Objects (dear Google, this is what we expect):
List selected_items = my_list_view.getSelectedItems();
we are forced to use this stupendously ridiculous API:
SparseBooleanArray checked = my_list_view.getCheckedItemPositions();
int num_selected = 0;
for(int i = 0; i < checked.size(); i++) {
if(checked.valueAt(i)) {
num_selected++;
int key = checked.keyAt(i);
boolean value = checked.get(key);
if (value) {
//
}
}
}
The horribly named SparseBooleanArray is populated by calling the even more horribly named getCheckedItemPositions() on the ListView. But instead of returning the positions of each selected/checked item in the list, it returns the position of every item in the list that WAS EVER touched, whether it is currently actually selected or not! Unbelievable, but true.
To calculate whether the item is ACTUALLY CURRENTLY checked, we are forced to test valueAt(i) for truthiness while looping through the 'was ever touched' array of items.
In addition to this madness, if we want to calculate the number of selected items, we appear to be forced to increment our own counter (e.g. num_selected).
With APIs like this one, it's little wonder developers are an angry lot!
I think another option is to just keep track of all of this yourself.
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View selectedItem,
int position, long itemId) {
//Keep a reference here, and toggle a global variable.
HOW I SOLVED THE ISSUE with a second ArrayList :
Created a second ArrayList instance
Updated that ArrayList instance with the UNCHECKED items
added it to the my listadapter
public void removeSelectedItems(){
updatedList = new ArrayList<String>(); //initialize the second ArrayList
int count = lv.getCount(); //number of my ListView items
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
for (int i=0;i < count;i++){
if(!checkedItemPositions.get(i))
updatedList.add(liveNames.get(i)); //liveNames is the current ArrayList
Log.e("TEST", liveNames.get(i));
}
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, updatedList);
setListAdapter(adapter);}
Hope it will be helpfull :)
Foo objectAtCheckedRow = null;
for (int i = 0; i < positions.size(); i++) { //positions.size() == 2
objectAtCheckedRow = adapter.getItem(positions.keyAt(i));
//Do something significant with object here
}
A couple things to understand
It's a key-value pair list.
The key is the index of a row, get it with positions.keyAt(i)
The value is whether the row at that index is checked or not(true or false), get it with positions.valueAt(i)
positions.get(i) returns the same boolean as .valueAt(i)
Careful not to get indexes mixed up. You do not need to(and should not) iterate over your whole list. Use int i to iterate over positions, but don't use i to get objects from your list
But in this specific case(listView.getCheckedPositions()) it only fills in true(checked rows), so you don't actually need to verify using .get(i) nor .valueAt(i)
Example:
Let's say you've checked the 5th and 8th items in the list(index 4 and 7), then positions.size() == 2 and i will be 0 and then 1
So when:
i == 0 then keyAt(i) == 4
i == 1 then keyAt(i) == 7
i == 0 OR i == 1 then valueAt(i) == true AND get(i) == true
FYI, Here is how Google did it:
Excerpted from http://mytracks.googlecode.com/hg/MyTracks/src/com/google/android/apps/mytracks/util/Api11Adapter.java
/**
* Gets the checked positions in a list view.
*
* #param list the list view
*/
private int[] getCheckedPositions(ListView list) {
SparseBooleanArray positions = list.getCheckedItemPositions();
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for (int i = 0; i < positions.size(); i++) {
int key = positions.keyAt(i);
if (positions.valueAt(i)) {
arrayList.add(key);
}
}
int[] result = new int[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
result[i] = arrayList.get(i);
}
return result;
}
and here is my adapted version:
public static List<Integer> getAbsListViewCheckedItemPositions(AbsListView absListView) {
SparseBooleanArray checked = absListView.getCheckedItemPositions();
List<Integer> positions = new ArrayList<>();
int checkedSize = checked.size();
for (int i = 0; i < checkedSize; i++) {
if (checked.valueAt(i)) {
positions.add(checked.keyAt(i));
}
}
return positions;
}
We use this in our Android utility class. The generics help prevent compiler warnings, but you can remove them if your adapter returns multiple types.
public static <T> Collection<T> getCheckedItems(ListView listView) {
Collection<T> ret = new ArrayList();
SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
for (int i = 0; i < checkedItemPositions.size(); i++) {
if (checkedItemPositions.valueAt(i)) {
T item = (T) listView.getAdapter().getItem(checkedItemPositions.keyAt(i));
ret.add(item);
}
}
return ret;
}
Very simple, use below code
listViewRequests.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AppCompatCheckedTextView checkBox = (AppCompatCheckedTextView) view;
if (checkBox.isChecked() == true){
Log.i("CHECK",checkBox.isChecked()+""+checkBox.getText().toString());
}
}
});
for(int i =0; i< listView.getAdapter().getCount(); i++){
if(listView.isItemChecked(i)){
listView.getAdapter().getItem(i); // item
}
}
should be used after setAdapter() method