I am using this method to check how many items on a list a checked and I get this error that this method is not available for any SDK older than 11.
What is the equivalent this in API level 8
The accepted answer didn't work for me (always returns 0), I had to use the following code:
public static int getCheckedItemCount(ListView listView)
{
if (Build.VERSION.SDK_INT >= 11) return listView.getCheckedItemCount();
else
{
int count = 0;
for (int i = listView.getCount() - 1; i >= 0; i--)
if (listView.isItemChecked(i)) count++;
return count;
}
}
getCheckedItemIds().length seems to do the trick
I'm using this code which I believe is efficient and works in all cases:
public int getCheckedItemCount() {
ListView listView = getListView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return listView.getCheckedItemCount();
}
SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
int count = 0;
for (int i = 0, size = checkedItems.size(); i < size; ++i) {
if (checkedItems.valueAt(i)) {
count++;
}
}
return count;
}
Please inform me if you find a case where it doesn't work.
Related
how does one iterate through a view's children and remove all ImageViews with a Tag that StartsWith a particular string?
All examples I can find this iterator;
for (int pos = 0; pos < mat.getChildCount(); pos++)
{
Object tag = mat.getChildAt(pos).getTag();
if (tag != null)
{
String s = tag.toString();
if (s.startsWith("Z"))
{
mat.removeView(mat.getChildAt(pos));
}
}
}
do a test, then remove the object.
The issue is both 'pos' and getChildCount change throughout the process. If I want to remove the first item and then the 2nd item (which after the first remove is actually the first item) it won't work, as pos is now 1 (ie 2nd item).
thanks
There are a few options.
for (int pos = 0; pos < mat.getChildCount();) {
if (remove) {
mat.removeViewAt(pos);
continue;
}
// only increment if the element wasn't removed
pos++;
}
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
mat.removeViewAt(pos);
// balance out the next increment
pos--;
}
}
// don't remove views until after iteration
List<View> removeViews = new ArrayList<>();
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
removeViews.add(mat.getChildAt(pos));
}
}
for (View view : removeViews) {
mat.removeView(view);
}
// count in reverse
for (int pos = mat.getChildCount() - 1; pos >= 0; pos--) {
if (remove) {
mat.removeViewAt(pos);
}
}
I have a List<Restaurant> restaurants (11 items). I want to get only 6 items from 11 items to show in gridview. Now, I loop all size(). How to loop to get only 6 items.
if (restaurants != null && restaurants.size() > 0) {
for (int i = 1; i < restaurants.size(); i++) {
adapter = new ItemAdapter(MainActivity.this, restaurants);
grid_view.setAdapter(adapter);
}
}
Best way,
#Override
public int getItemCount() {
if(lotteryList.size()>6){
return 6;
}
else{
return lotteryList.size();
}
}
Try this code.
List<Restaurant> restaurantsTemp=new ArrayList();
for (int i = 0; i < 6; i++) {
restaurantsTemp.add(restaurants.get(i));
}
adapter = new ItemAdapter(MainActivity.this, restaurantsTemp);
grid_view.setAdapter(adapter);
no need loop if your list already have 11 records.Pass third parameter in adapter constructer and set it into count return as 6 in the adapter.
adapter = new ItemAdapter(MainActivity.this, restaurants,6);
grid_view.setAdapter(adapter);
Hi you need to use this code, Also you don't need to set adapter in loop.
if (restaurants != null && restaurants.size() > 0) { // restaurants.size() (11 items)
List<Restaurant> modifiedList=new ArrayList();
for (int i = 1; i < restaurants.size(); i++) { // 11 items
modifiedList.add(restaurants.get(i-1));
if(modifiedList.size()==6){
break;
}
}
adapter = new ItemAdapter(MainActivity.this, modifiedList);
grid_view.setAdapter(adapter);
}
ArrayList<String> sixItem = new ArrayList<>;
if (restaurants.size() >= 6) { // restaurants.size() (11 items)
for (int i = 1; i < 7; i++) { // 11 items
sixItem.add(i-1);
}
} else {
for (int i = 1; i < restaurants.size(); i++) {
sixItem.add(i-1);
}
}
adapter = new ItemAdapter(MainActivity.this, sixItem);
grid_view.setAdapter(adapter);
if restaurants.size() is less than 6 it won't show error.
try this in your ItemAdapter getCount() or getItemCount()
#Override
public int getItemCount() {
if(lotteryList.size()>6){
return 6;
}
else{
return lotteryList.size();
}
}
indexOfValue(E) method always return -1 while I am sure my E object exists in my SparseArray object, why? I have :
static final SparseArrayCompat<Long> LOCATION_SHARING_TIME = new SparseArrayCompat<Long>();
static {
LOCATION_SHARING_TIME.put(0, LOCATION_SHARING_TIME_5s);
LOCATION_SHARING_TIME.put(1, LOCATION_SHARING_TIME_1m);
LOCATION_SHARING_TIME.put(2, LOCATION_SHARING_TIME_5m);
LOCATION_SHARING_TIME.put(3, LOCATION_SHARING_TIME_30m);
LOCATION_SHARING_TIME.put(4, LOCATION_SHARING_TIME_1h);
}
I cannot use SparseLongArray because it support API 18+ which my project support API 9 minimum.
Because it uses == instead of equal to determine the equality. See the implementation of the method:
public int indexOfValue(E value) {
if (mGarbage) {
gc();
}
for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i;
return -1;
}
One idea would be to extend the SparseArray and override the method replacing == with equal.
public int indexOfValue(E value) {
if (mGarbage) {
gc();
}
for (int i = 0; i < mSize; i++)
if (mValues[i].equals(value))
return i;
return -1;
}
I have a dynamic listview with one text and one checkbox per line.when i click a button.,i need to get all checked item names & Unchecked item names separately as arraylilst.How could i do that.Examples are much better..
I used..
SparseBooleanArray checked = mainlw.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
if(checked.valueAt(i) == true) {
Planet tag = (Planet) mainlw.getItemAtPosition(checked.keyAt(i));
String selectedName=tag.getName();
Toast.makeText(getApplicationContext(), selectedName, Toast.LENGTH_SHORT).show();
}
}
Try this out and implement this logic according to your requirement.
int cntChoice = myList.getCount();
String checked = "";
String unchecked = "";
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked += myList.getItemAtPosition(i).toString() + "\n";
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked+= myList.getItemAtPosition(i).toString() + "\n";
}
}
use CHOICE_MODE_MULTIPLE in your ListView and use getCheckedItemPositions() to get the checked ones.
So Onclick of button u can do this,From this you will get the items that are checked:-
#Override
public void onClick(View v)
{
System.out.println("check"+getListView().getCheckItemIds().length);
for (int i = 0; i < getListView().getCheckItemIds().length; i++)
{
System.out.println(getListView().getAdapter().getItem((int)getListView().getCheckItemIds()[i]).toString());
}
}
I have a multiselection ListView in my android app. What I need to know is how many items on that list I have selected.
Use getCheckedItemCount() of ListView. This gives you directly an int.
I found a solution.
getCheckedItemPositions() will create a SparceBooleanArray. So it is just to count the elements which are true in that array.
Example:
SparseBooleanArray positions = theListView.getCheckedItemPositions();
int counter = 0;
if (positions != null) {
int length = positions.size();
for (int i = 0; i < length; i++) {
if (positions.get(positions.keyAt(i))) {
counter++;
}
}
}
Instead of using keyAt followed by get, we can use valueAt directly.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}