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 I use the code below, it prints an object android.content.ContentResolver.cursorWrapper... I am not sure how to interpret this and know which contact name was selected. Is there a way I can make the output of listview.getItemAtPosition() readable?
Code:
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.get(i) == true)
{
checked.add(lv.getItemAtPosition(i).
.toString());
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(lv.getItemAtPosition(i).toString());
}
}
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));
}
}
When listview adapter is a cursor adapter then listview.getItemAtPosition() return a cursor pointed to that row. So you have to get data from that cursor like this:
Cursor cursor = listview.getItemAtPosition();
String name = cursor.getString(0);
I just assume that 0 is the index of first name column in that database which cursor is representing, It could be 2 , 3 ...
Related
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
I wanted to view the items selected in my listview with checkboxes. However, when I try to print out lv.getItemAtPosition(i).toString() it gives me a `` object. How can I convert this to a readable format?
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.get(i) == true)
{
checked.add(lv.getItemAtPosition(i).toString());
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(lv.getItemAtPosition(i).toString());
}
}
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 something like this:
if(sparseBooleanArray.get(i) == true)
{
checked.add(Boolean.toString(sparseBooleanArray.get(i)));
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(Boolean.toString(sparseBooleanArray.get(i)));
}
I have solved the above questions by changing the following code and using Cursor and keeping everyother part of the above code the same.
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);
}
}
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));
}
}
Android :How to count no checked items of checkboxes in listview when using Simple cursor Adapter
Call getCheckedItemCount() on the ListView.
int cntChoice = listView.getCount();
String checked = "";
String unchecked = "";
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
// checked = listView.getItemAtPosition(i).toString() ;
checked= list1.get(i).toString();
Log.i("CheckedItem", checked);
}
else if(sparseBooleanArray.get(i) == false)
{
//unchecked= listView.getItemAtPosition(i).toString();
unchecked= list1.get(i).toString();
Log.i("uncheckedItem", unchecked);
}
}
Hope this will help you. For more reference.
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());
}
}