I have a listview that is built from textviews with the built in Resource Layout simple_list_item_multiple_choice.
syntax like this :
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, internet);
If the user clicks an item list, the checkbox of selected item has to be checked.
I know to implement OnItemClickListner, But after it what should I do to check the selected item.
Yu can try this ,referred from https://stackoverflow.com/a/4590897/5193608.
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 */
}
Related
I have a ListView which am setting that to my adapter. The ListView item contains two view elements which are chekbox and TextView.
I have given the setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) for the ListView.
When i want to select one or more element, I want to get the count (based on selecting and deselecting).
I can't do this stuff in onItemClickListener of ListView.
So I have written the logic in BaseAdapter.
holder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count += 1;
}
});
But If i deselect the item then the value have to decrease. If iam having only one item in ListView then I can write
SparseBooleanArray checked = listView.getCheckedItemPositions();
and get the value in Fragment. Just I get confused. Could someone help me?
Use this.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}
// use count as you wish
Make sure count is in a local (or block of the OnClickListener) scope so that every time you click a button or something, count gets reset and it recounts how many items are checked/unchecked.
Let me know if you have more question :)
I have a listview , i would like to put some position preselected; in fact i have some values that i would like to put them selected. this is my code
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i)) {
selectedItems.add(adapter.getItem(position));
}
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
My selectedItems firstly is not empty; i would like to put its value as selected.
Pass your selectedItem list to your custom adapter and inside getView() method compare position. If it is available in seloectedItem list change the background of view.
Use a custom adapter and pass the list inside there and render it on getView() by inflating the item.xml layout and use a static class viewHolder to create a viewholder that transfers the data from your list into the appropriate widgets.
Make sure you check if the convertView has been created previously as you do not want to create the view every time android hits getView from your adapter as it will slow your listView UI.
Good luck
How to delete the checked Items in a multiple choice listview with a contextual action for deletion -
ArrayList<String> liveNames = new ArrayList<String>() {
{
add("dani");
add("john");
add("dave");
add("alen");
add("deno");
add("feliks");
add("jupi");
}
};
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_multiple_choice, liveNames);
setListAdapter(adapter);
.......
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
lv = getListView();
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.item1:
if(lv.getCheckedItemCount() > 0){
removeItems = lv.getCheckedItemIds();
deleteSelectedItems(removeItems);
}
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
now how should I implement it in the deleteSelectedItems(long[] delItms) method so that the selected item IDs from the ListView be deleted inside the "names" ArrayList. Please for some hints
I know that I can update the adapters list with
adapter.notifyDataSetChanged();
but how to get the positions of the items inside the listview with their IDs so I could just
name.remove(position) - but I have only the IDs.
Thanks
This method should do the trick for you, I guess:
/* returns item's position in ArrayList by ID */
public int getItemPositionById(int id)
{
for(int i = 0; i < names.size(); i++) {
if(names.get(i).getId() == id) {
return i; // if found item with specified id, return it's position
}
}
return -1; // didn't find item with specified id
}
Just call it for all the ids you have and store those positions somewhere. Then you can remove all items at those positions.
Anyway I could not retrieve the checked Ids with this method
removeItems = lv.getCheckedItemIds();
because the the adapter needs to have stable Ids ...or something like that
so I tried to retrieve the positions of checked items with
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
and then to delete them and update the arraylist and the adapter
public void removeSelectedItems(){
int count = lv.getCount();
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
for (int i=0;i<count;i++){
if(checkedItemPositions.get(i))
Log.e("TEST", liveNames.get(i));
liveNames.remove(i);
}
adapter.notifyDataSetChanged();
}
The problem is I suppose with the liveNames ArrayList which dynamically changes its element indexes every time I remove one element so the end results come wrong.
Here is a link of a discussion for this type of problem but without a solution -
How to get Selected items from Multi Select List View
HOW I SOLVED THE ISSUE:
Created a second ArrayList instance
Updated that ArrayList instance with the UNCHECKED items
added it to the my listadapter - here is the method
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));
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 helpful :)
I got on my code a listview with check boxes, but How can I check if the value of the checkbox?
I'm implementing a program with tabs, and one of the tabs has the ListView with checkboxes, the code is as follows:
spec = tabHost.newTabSpec(OPTS_TAB_TAG).setIndicator("Options",
res.getDrawable(R.drawable.ic_tab_options))
.setContent(new TabContentFactory()
{
public View createTabContent(String arg0)
{
DbAdapter databaseManager = new DbAdapter(BusTrackerBetaActivity.this);
databaseManager.open();
List<String> BusLinesList = new ArrayList<String>();
BusLinesList = databaseManager.toStringList(databaseManager.getAllBusLines(), 1);
String[] BusLinesArray = BusLinesList.toArray(new String[BusLinesList.size()]);
databaseManager.close();
ListView ls1 = new ListView(BusTrackerBetaActivity.this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
BusTrackerBetaActivity.this,
android.R.layout.simple_list_item_multiple_choice,
BusLinesArray);
ls1.setAdapter(adapter);
ls1.setOnCreateContextMenuListener(BusTrackerBetaActivity.this);
ls1.setItemsCanFocus(false);
ls1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
return ls1;
}
});
It's working fine, acctually I got this way of implementing the list from some examples, but my question is: How can I check the value (true or false) of each checkbox?
You can use ListView's getCheckedItemPositions function to accomplish this. It will return a SparseBooleanArray object containing the checked status of each item in the ListView. Loop through the SparseBooleanArray to determine which items are checked.
You will need to keep a reference to your ListView object somewhere so that you can easily access it later when you need to determine checked item status.
SparseBooleanArray checkedItems = ls1.getCheckedItemPositions();
if (checkedItems != null)
for (int i = 0; i < checkedItems.size(); i++)
{
if (checkedItems.valueAt(i))
{
String s = ls1.getAdapter().getItem(checkedItems.keyAt(i)).toString();
//s contains your checked item, checkedItems.keyAt(i) is the index of the checked item
}
}
i have listview which is multiple choice mode
lView = (ListView) findViewById(R.id.ListView01);
lView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, lv_items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this contains multiple choice list items
i want to check whether selected item is checked or not
so how i can do that.
You need to grab the items that have been clicked and then iterate through them to find the checked ones like so:
// Using a List to hold the IDs, but could use an array.
List<Integer> checkedIDs = new ArrayList<Integer>();
// Get all of the items that have been clicked - either on or off
final SparseBooleanArray checkedItems = lView.getCheckedItemPositions();
for (int i = 0; i < checkedItems.size(); i++){
// And this tells us the item status at the above position
final boolean isChecked = checkedItems.valueAt(i);
if (isChecked){
// This tells us the item position we are looking at
final int position = checkedItems.keyAt(i);
// Put the value of the id in our list
checkedIDs.put(position);
}
}
Note the getCheckedItemPositions() gets the items that have been checked by the user regardless if the checkbox was left checked or not.
I used the isChecked property of the given list item when clicked:
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView item = (CheckedTextView)v;
if(item.isChecked()){
//do what you want
}