i was following an example i saw online to retrieve checked items
but in the example it only retrieves one value
int len = listview.Count;
SparseBooleanArray checkedItems = listview.CheckedItemPositions;
for (int i = 0; i < len; i++)
if (checkedItems.Get(i))
{
string item = list[i];
/* do whatever you want with the checked item */
txt.Text = item;
}
So how do i Retrieve all of the checked items values?
You need to create a data structure of some sort
List<string> checkedItems = new List<string>();
int len = listview.Count;
SparseBooleanArray checkedItems =
listview.CheckedItemPositions;
for (int i = 0; i < len; i++)
if (checkedItems.Get(i))
{
checkedItems.Add(list[i]);
}
Related
I have ListView with multiple choice items, I want whenever user check more than one item, return all its checked values.
SparseBooleanArray checked = lv.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
int position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(String.valueOf(ard.getItem(position)));
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
Pushbots.sharedInstance().setAlias(outputStrArr[i]);
}
...
In this case, only last checked value is took ...
I have a list which has multiple choice enabled and I needed to get the rows that are selected and will be passed to the previous activity. The getCheckedItemPositions() returned the number of "checkable" (true or false) rows not the rows that are selected. In my opinion, the name of that method is misleading. I'll be iterating the getCheckedItemPositions() to get the true ones and pass the number of selected rows to the previous activity. I'm wondering it might be something that is better than my solution?
Here are the previous code -
final SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
Team team;
final int checkedItemsCount = checkedItems.size();
int[] ids = new int [checkedItemsCount];
for (int i = 0; i < checkedItemsCount; ++i) {
team = new Team();
team = mListAdapter.getItem(checkedItems.keyAt(i));
list.add(team);
ids[i] = team.getTeamId();
}
Intent i = new Intent();
i.putExtra(INTENT_TEAM_SELECTION_SIZE, list.size());
i.putExtra(INTENT_TEAM_IDS, ids);
setResult(RESULT_OK, i);
finish();
My updated code -
final SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
Team team;
final int checkableItemsCount = checkedItems.size();
int selectedItemsCount = 0;
//get the count of selected rows
for (int i = 0; i < checkableItemsCount; ++i) {
if (checkedItems.valueAt(i)) {
selectedItemsCount++;
}
}
//Initialize the array of ids by the number of selected rows
int[] ids = new int[selectedItemsCount];
int index = 0;
for (int i = 0; i < checkableItemsCount; ++i) {
if (checkedItems.valueAt(i)) {
team = new Team();
team = mListAdapter.getItem(checkedItems.keyAt(i));
ids[index] = team.getTeamId();
index++;
}
}
Intent i = new Intent();
i.putExtra(INTENT_TEAM_SELECTION_SIZE, selectedItemsCount);
i.putExtra(INTENT_TEAM_IDS, ids);
setResult(RESULT_OK, i);
finish();
This solution looks a bit cleaner, perhaps check it out? :)
https://stackoverflow.com/a/8726923/1337802
My listview is from a layout where its id is #android:id/list. It is part of the android.R.id.list. I used a SimpleCursorAdapter to setAdapter to listview. The listview contains checkboxes with contact names such as Emily Andrews, Susan John... I wanted to know the names of the contacts which were selected by the user. When using the code below, for some reason, it gives me random contact names in my unchecked list. For instance though I have Emily checked, and Matt Jones unchecked. It has both of them in the unchecked list. Also, no matter what i check and uncheck in the listview, it always gives me the first two contacts in my unchecked list and doesn't print anything for my checked list. Is there any way I can solve this?
public void blockCheckedItems(View view) {
// int cntChoice = lv.getCount();
checked = new ArrayList<String>();
unchecked = new ArrayList<String>();
int itempositions=adapter.getCount();
SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
int countChoice = lv.getChildCount();
Log.d("" + countChoice, "CountChoice===============================");
Log.d("" + sparseBooleanArray,"sparseBooleanArray -------------------------");
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.valueAt(i) == true)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
checked.add(name);
}
else if(sparseBooleanArray.valueAt(i) == false)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
unchecked.add(name);
}
}
for(int i = 0; i < checked.size(); i++){
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for(int i = 0; i < unchecked.size(); i++){
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
try this and let me know if i am wrong
public void blockCheckedItems(View view) {
ArrayList<String> checked = new ArrayList<String>();
ArrayList<String> unchecked = new ArrayList<String>();
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getCount(); i++) {
Cursor cursor = (Cursor) listView.getItemAtPosition(i);
String name = cursor.getString(1);
if (sparseBooleanArray.get(i)) {
checked.add(name);
} else {
unchecked.add(name);
}
}
for (int i = 0; i < checked.size(); i++) {
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for (int i = 0; i < unchecked.size(); i++) {
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
i have problem in data binding in expandable list view. here i use
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ExpandListGroup for data binding. but in sum 2d array there is null value.Data is coming dynamically .
eg:
String [] [] array1 = [[one,two,three,null],[seven,six,null]] ;
I want to remove the null column from this two dimensional array
You need to take a intermediate arraylist and result array and follows the below steps.
First copy the source array(containing null) into arraylist.
Then remove null from arraylist
Then create new array and copy data from arraylist to array.
private void remove_nulls()
{
String [] [] array1 = {{"one","two","three",null},{"seven","six",null}} ;
ArrayList<ArrayList<String>> contact = new ArrayList<ArrayList<String>>();
for(int i=0;i<array1.length;i++)
{
ArrayList<String> con = new ArrayList<String>();
for(int j=0;j<array1[i].length;j++)
{
if(array1[i][j]!=null)
con.add(array1[i][j]);
}
if(con.size()>0)
contact.add(con);
}
String [] [] array2 = new String[array1.length][];
for(int i=0;i<contact.size();i++)
{
array2[i]=new String[contact.get(i).size()];
for(int j=0;j<contact.get(i).size();j++)
{
array2[i][j]=contact.get(i).get(j);
}
}
}
Unless there is some trick to the question...
String[][] array1 = {{"one", "two", "three", null}, {"seven", "six", null}};
List<String[]> newList = new ArrayList<>();
for (int i = 0; i < array1.length; ++i) {
List<String> currentLine = new ArrayList<>();
for (int j = 0; j < array1[i].length; ++j) {
if (array1[i][j] != null) {
currentLine.add(array1[i][j]);
}
}
//create the array in place
newList.add(currentLine.toArray(new String[currentLine.size()]));
}
//no need to use an intermediate array
String[][] array2 = newList.toArray(new String [newList.size()][]);
//And a test for array2
for (int i = 0; i < array2.length; ++i) {
for (int j = 0; j < array2[i].length; ++j) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
System.out.println("Compared to...");
//Compared to the original array1
for (int i = 0; i < array1.length; ++i) {
for (int j = 0; j < array1[i].length; ++j) {
System.out.print(array1[i][j] + " ");
}
System.out.println();
}
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++;
}
}