I'm trying to implement removing chosen TextViews from my ListView.
The one way to do this according to android dev reference is by using SparseBooleanArray. The problem is, I don't understand the logic behing this array and every time I call the getCheckedItemPositions(), method returns the empty array.
Here is the sample of my code:
List<Record> records = new RecordDAO(this).findAll(); //gets all records saved in ListView from database
SparseBooleanArray checkedPositions = recordListView.getCheckedItemPositions();
for(int i = 0; i < records.size(); i++){
if(checkedPositions.valueAt(i)){
System.out.println("chosen at pos: " + i);
}
}
I choose the last 2 TextViews press button
and the output is:
I/System.out: chosen at pos: 0
I/System.out: chosen at pos: 1
How do I properly solve my problem?
Can anyone explain me how does
the SparseBooleanArray working?
Related
I have a Sqlite table In that I am Selecting one row at a time with Limit 1.. like
cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + SQLiteHelper.TABLE_NAME + " WHERE status in ('new') LIMIT 1", null);
So Now I want to read the values of all columns with previous/next options
I tried with String list but its not working
I am Using this for Voice Based application So If User Says Next/Previous It should Say/Display Next Value..
I have Done with Speech to text and Text to speech but I Struck at Previous Next
If I Filter Previous and Next from that row I can add voice to them
My Column Values like
1,Android,Oreo,4gb,64gb,2.2Ghz,4000mhz,$800,May2019.
I want to get these Column values one by one
I googled a lot but I got previous and next with rows.. but not column values
You could use the following as the basis
........ existing code
int current_column = 0;
show_value();
}
// Called when event requires next
private next_column() {
if (current_column < (cursor.getColumnCount() - 1) {
current_column++;
show_value();
}
}
// Called when event requires prev
private prev_column() {
if (current_column > 0 {
current_column--;
show_value();
}
}
private void show_value() {
String current_value = cursor.getString(current_column);
your_appropriate_view.setText(current_value);
}
I am trying to create a GridView that is based on the user input.
If the users input is x then the GridView shall show x elements. The problem is I don't want to have elements in GridView that starts with 0, as an Array in Java starts with index 0.
Here is a code snippet:
int numberOfTables=10; //let's say this is user input
String[] gridViewStrings = new String[numberOfTables];
for(int i =0; i<numberOfTables; i++){
gridViewStrings[i]="Table " +i;
The table descriptions should not start with 0, it should start with 1.
I tried to increment the array size by +1 but I get a BoundofException.
When you compile the code the output is :
Table0, Table1, Table2,...,Table9
The output I want is :
Table 1, Table2,...,Table10
How can I handle this ?
for(int i =1;i<numberOfTables; i++){ }
Try making your i to be 1. This might make the loop start at one not zero. and you moght not get an error
or you can try i+1
I am trying to display a list from current year plus five year in a Spinner item. But getting error java.lang.IndexOutOfBoundsException: Invalid index 2014, size is 0
I used this code,
ArrayList<String> years=new ArrayList<String>();
Calendar cal=Calendar.getInstance();
int curYear= cal.get(Calendar.YEAR);
for(int i=0;i<6;i++)
{
years.add(curYear+i, null);
}
Spinner spnYear= (Spinner) findViewById(R.id.spnYear);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, years);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnYear.setAdapter(adapter);
Please help.
Thanks.
When you use add() with two parameters, the first is the index you would like to add it at. You'll want to just call years.add(String.valueOf(currYear + 1)). I've also added the String.valueOf() around the item you're adding, because you have an ArrayList<String>
It's how you're adding things to your ArrayList. For one, you're trying to add an int curYear + i to an ArrayList<String>. That won't work. Second you're adding them in such a way that you're not actually passing an object to the list. The version of add you're using add(int index, E object) takes the index as its first argument - not what you want to add to the list. Your second argument, which is what you actually want to add, is null. That explains why you're getting list size of 0. You should just add your values normally and know that index 0, 1, 2, 3, 4 will be the next 5 years.
here is your issue
for(int i=0;i<6;i++) {
years.add(curYear+i, null);
}
you are using the add method that takes a first paramter the index and as second the object. The documentation states that it throws
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).
At the first iteration size() returns 0, and you are try to put the element at position 2014
Change it in
for(int i=0;i<6;i++) {
years.add(String.valueOf(curYear+i));
}
I've a ListView allowing multiple selection configured with an ArrayAdapter, everything works fine but when I try to set to false some selected items the getCheckedItemCount value doesn't change
Just to replicate the behavior I unselect all items using the code shown below and the printed value is the same before and after the getCheckedItemCount call
Why?
for (MyObject post : list) {
int position = photoAdapter.getPosition(post);
System.out.println("count before " + photoListView.getCheckedItemCount());
photoListView.setItemChecked(position, false);
System.out.println("count after " + photoListView.getCheckedItemCount());
}
Obviously to unselect all items we must call clearChoices() but the code above is used only to demonstrate the problem
#dafi i am not sure this is working in your case but below solution give me right count. The easiest solution is to clear the checked count manually.You just try it.
this.markersList.clearChoices();
for(int i = 0; i < this.markersList.getCount(); i++)
{
this.markersList.setItemChecked(i, true);
}
Try out this solution.
Scenario:-
I have two ArrayList
list contains images
postList contains position of selected images
now when ever i am selecting the images and press delete menu ,it should delete the selected images .
when i am running the code in debug ,its working fine and give the desire output.
but when i am running it normal mode ,its crashing and giving above exception.
if (posList.size() > 0)
{
Toast.makeText(getBaseContext(), "i value" +posList.size(),
Toast.LENGTH_SHORT).show();
for (int i = 0; i < posList.size(); i++)
appAdp.list.remove(appAdp.list.get(posList.get(i)));
appAdp.notifyDataSetChanged();
posList.clear();
Toast.makeText(getBaseContext(), "You deleted selected items",
Toast.LENGTH_SHORT).show();
}
return true;
postList values here
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
boolean checked) {
posList.add(position);
error showing here
appAdp.list.remove(appAdp.list.get(posList.get(i)));
logcat:-
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
why its behaving like this ,not getting any clue.
Thanks for any help.
You are trying to perform operation on Same ArrayList because of that when ever you are remove elemnt from the ArrayList then its size will reduce so, You'll get ArrayIndexoutofBoundsException.
i.e when you remove item from the appAdp.list , then the size of the appAdp.list will also change
consider if your list has originally 3 elemnts.
you have the position in your posList 0,2
then when you remove item from the 0 item from appAdp.list, its size will become 2 for the next time when you try to remove item at position 2, you will get AIOBE
Solution:
Save all items that needs to be removed in separate List and the use removeAll(list) method to remove items from your appAdp.list
Example:
ArrayList<yourappListtype> templist=new ArrayList<yourappListtype>();
for (int i = 0; i < posList.size(); i++)
templist.add(appAdp.list.get(posList.get(i)));
And then
appAdp.list.removeAll(templist);
try this code,
postList.remove(position);
then,
notifyItemRangeChanged(positon,postList.size());
Before you change the data source of adapter, you can call adapter's notifyDataSetInvalidated() function to make the origin data source invalid, then call adapter's notifyDataSetChanged() after finish changing the data source.
remember that index are starting from zero. I think you when get position it is +1 higher than index of array, so you get out of bounds exception
The error is Invalid index 2, size is 2
Possible issue is your posList.size()=2 where as appAdp.list.size()<2
Make sure that your appAdp.list has more than two entries.
if (posList.size() >0)
i=appAdp.list.size();
while(i<=posList.size() && i<=appAdp.list.size())
{
appAdp.list.remove(appAdp.list.get(posList.get(i)));
i--;
}
The basic meaning of the exception java.lang.IndexOutOfBoundsException: Invalid index x, size is y where x and y are the index position and size respectively means you are attempting to get value at a position that does not exist. Take for example an ArrayList with a size 2, typically you would want to get value(s) by specifying position index which in this case the valid position would either be 0 or 1 and end up specifying a position 2 (index 2 does not exist). I believe understanding basic meaning of Java errors will save you a lot of valuable time in your development.
just use break command at the end of for loop, like this:
fun deleteUser(userID: Int) {
for(i in 0 until usersList.size) {
if (usersList[i].id == userID) {
usersList.removeAt(i)
notifyItemRemoved(i)
break
}
}
}