I have an Android application. In which I have ListView. List view is constructed by ArrayAdapter and layout file.
In layout file I have checkbox. So I want to see if all checkboxes are checked or not by pressing button. How I can do these?
Update: I want to see which checkboxes are checked
You can get the number of checked items in your list with the code:
private int getChecked()
{
SparseBooleanArray checked = yourList.getCheckedItemPositions();
return checked.size();
}
Edit:
To check that every item is checked:
private boolean isEveryChecked()
{
SparseBooleanArray checked = yourList.getCheckedItemPositions();
return checked.size()==yourList.getCount();
}
yourList is a ListView.
Related
if (RFidList.size() >0){
for (String s: RFidList){
RFidTagValueList.remove(s);
}
}
mTimeAdapter.notifyDataSetChanged();
dialog.dismiss();
when choose multiple items in recyclerview checkbox not working / and am use private ArrayList<String> data = new ArrayList<>();
You can't move and delete from the list. The best option to do this is to have an array that will remember the state of the checkbox. Scrolling through your recycler view will always change the state of the checkbox and maybe won't remember it. The way to do this is to add an array of boolean
private boolean[] checkStates;
then inside your constructor of the adapter do this.
checkStates = new boolean[data.size()];
This way you'll create an array of boolean filled with FALSE value the same size your data array is. Now, inside your adapter when you are binding your view do this for the checkbox.
holder.checkbox.setChecked(checkStates[position]);
Also, don't use onCheckedChangeListener on checkbox inside adapter. This will be called even when you scroll and it will change whatever you are doing inside the function. What you can do is override onClick for the checkbox, but there is something tricky here. When you click the checkbox to check it, inside the onClick method the view will have the state as it is already checked so to follow this do it like this:
holder.checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Checkbox c = (CheckBox) v;
checkStates[position] = c.isChecked();
}
});
I wrote this from my head and maybe there are some mistakes but you'll get the point. The animation of the check state will be handled you just need to handle changes inside the checkStates array.
Now your checkbox will always have the state it had before. After this, you can create a function to delete items from your data.
public void removeItems() {
ArrayList<YOUR-MODEL> items_to_delete = new ArrayList<>();
for (int i = 0; i < checkStates.length(); ++i) {
if (checkStates[i]) items_to_delete.add(data.get(i]);
}
data.removeAll(items_to_delete);
notifyDataSetChanged();
checkStates = new boolean[data.size()];
}
This works if your data is a type of ArrayList. I think this is the best way to go.
I have approx 150 items in my custom listview. I am trying to implement the following:
list.setOnItemClickListener(new OnItemClickListener()
{
1. I get position of the item
2. Get the data from an ArrayList
3. Add the data to database.
This part is working fine.
But if I want to remove a particular item from the database by clicking on the item I am not able to achieve this part?
Reason because I am not sure if the item is clicked or not? I have this problem mainly when I want to implement search in the listView and add the selected item to database and remove selected item from database.
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
if (notfromsearch == true)
{
String profile_ID = profileList.get(position).get(KEY_ID);
String profile_DISPLAY_NAME = profileList.get(position).get(KEY_DISPLAY_NAME);
//add to database now
}
elseif (notfromsearch == false)
{
String SearchResult_profile_ID = searchProfileResults.get(position).get(KEY_ID);
String SearchRResult_profile_DISPLAY_NAME = searchProfileResults.get(position).get(KEY_DISPLAY_NAME);
//add to database now
}
}
});
But how to implement remove from database? I am not able to differentiate if the item is clicked before or not to remove from database.
Can somebody help me out with this?
Thanks!
On your list view, use
setOnItemClickListener
and you can do what you want next .
use context menu for deleting items from the database
on long press display delete/edit option and perform further operation as needed.
check here for context menu sample
EDIT
You can also put button/image in the listview to delete item and then implement your delete operation in the click method.
For Checkbox selection
model class
class CustomClass
{
boolean checked;
//other fields
}
in your custom adapter class
ArrayList<CustomClass> data = new ArrayList<CustomClass>();
and in getView() method
checkboxItem.setChecked(data.get(position).getChecked());
and don't forget to change the checked boolean value on checkbox change listener..
I have a list view with multiple items, where i need to select and deselect the list items, and also delete the selected items.
So i have looked into the example in the below link but its for android:minSdkVersion="11"
but i am working on minSdkVersion="10".
Link : http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
And yes we can do with checked text view, check box and radio button, but the requirement is like that i cannot use that.
Is there any other way that we can acheive this?
Make custom list adapter and get the click of that each view and maintain flag in adapter. If flag is true that means item selected otherwise item deselected, according to that you can change item view like disable that particular item or show some check box.
What I did was created an ArrayList that stores all the position of selected items, and toggle the background colors on clicks.
In my Adapter I define:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
With the following method :
public void toggleSelected(Integer position)
{
if(selectedIds.contains(position))
{
selectedIds.remove(position);
}
else
{
selectedIds.add(position);
}
}
which addes\removes items from the ArrayList
In my getView method :
if (selectedIds.contains(position)) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
else
{
convertView.setSelected(false);
convertView.setPressed(false);
convertView.setBackgroundColor(Color.parseColor("#000000"));
}
This checks if the position is storred in the ArrayList. if it does, paint it as selected. if not, the opposite.
all is left is the OnItemClick listener, i added :
((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));
When YourAdapter is the adapter of your ListView
Hope this helps anyone, as it's a generic answer :)
Thanks to eric.itzhak here : How to change background color of selected items in ListView?
I have a listview that contains a checkedtextview. My app moves from the top of the list view to the bottom. I want to check if the item is checked before calling an action. If it is not checked I want to move to the next item in the list.
E.g.
Item 1 - Checked
item 2 - Checked
Item 3 - Not Checked
Item 4 - Checked
So, I want the app to process as follows:
Item 1
Item 2
Item 4.
I am not sure how to access the checked status of the item from the listview position.
The logic that I want is as follows:
Is Current Item checked?
Yes:
Call action
No:
Move to next item.
Reloop to top of void.
I will need something in there to stop an infinite loop.
1.) First create an array, what indicates the items checked state in your adapter
(assuming you extend the BaseAdapter class for this purpose):
private boolean [] itemsChecked = new boolean [getCount()];
2.) Then create an OnCheckedChangeListener:
private OnCheckedChangeListener listener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton button, boolean checked)
{
Integer index = (Integer)button.getTag();
itemsChecked[index] = checked;
}
}
3.) In your adapters getView() method:
public View getView(int index, View view, ViewGroup parent)
{
/*...*/
CheckBox checkBox = /*get the checkbox*/;
checkbox.setTag(index);
checkBox.setOnCheckedChangeListener(listener);
/*...*/
}
4.) In the onClick() method:
public void onClick(View view)
{
//just get the boolean array somehow
boolean [] itemsChecked = adapter.getItemsCheckedArray();
for(int i=0; i<itemsChecked.length; i++)
{
if(itemsChecked[i])
{
//the i th item was checked
}
else
{
//it isnt checked
}
}
}
One solution will be to use an ArrayList of positions.
When the user check/uncheck a checkbox, add/remove accordingly the position in your ArrayList.
Then when the user is over, just iterate through the list to know which position have been selected.
I'm suffering from the same issue as this question: Wrong item checked when filtering ListView in android
As suggested in the above question, I have an Hashset holding all the selectedIds, but I can't figure out how to use it when the cursor is repopulating the checked items.
My issue is only cosmetic - for example:
"Facebook" is located at the 5th position in the unfiltered list.
User searched for "face", only "Facebook" appears in the 1st position in the filtered list.
User checks "Facebook" as selected and goes back to the unfiltered list.
The checked item is the 1st item in the list and not "Facebook" (positioned 5th).
Note:
Except this issue, everything else works great.
For example, "delete" will delete the right items because I use the selectedIds to do it (even if the checked items are wrong).
Single click on a list item:
OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//gets the Bookmark ID of selected position
Cursor cursor = (Cursor)parent.getItemAtPosition(position);
String bookmarkID = cursor.getString(0);
boolean currentlyChecked = checkedStates.get(position);
checkedStates.set(position, !currentlyChecked);
if (!selectedIds.contains(bookmarkID)) {
selectedIds.add(bookmarkID);
selectedLines.add(position);
} else {
selectedIds.remove(bookmarkID);
selectedLines.remove(position);
}
}
};
Inside the Cursor: - this is where the problem lies.
This repopulates the checked items - the problem is it does it by position (pos) and what was the right position of the item in the filtered list, is not its position in the unfiltered list - resulting in a wrongly marked item.
CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
markedItem.setChecked(checkedStates.get(pos));
Would appreciate any help!
Could it be simply that you ListView does not know the data set changed? Call notifyDatSetChanged() on your adapter to let it know.
my_adapter.notifyDataSetChanged();
This will force a redraw of the list, with the updated data.
Solved the issue.
As suggested in the question I added to mine, when you populate the checkboxes, the other List/Hashset should determine whether to mark item as checked or not.
Using my code:
//first you will need to get the current item ID
String bookmarkID = cursor.getString(0);
//Then, check if the current ID is in the selectedIds hash/list
//If true - mark current item view as checked, else - uncheck.
CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
if (selectedIds.contains(new String(bookmarkID))) {
markedItem.setChecked(true);
} else {
markedItem.setChecked(false);
}
Really hope this will help anyone! :)
This is my solution:
To selecte an item in list view after filtering, at the beginning, I got wrong item because I used this:
ItemData item = listItems.get(position);
The correct way should be like this:
ItemData item = (ItemData) parent.getItemAtPosition(position);
To delete an item after filtered, I have tried this:
for(int i=0;i<listItems.size();i++){
if(listItems.get(i).getItemID() == item.getItemID()){
listItems.remove(i);
myAdapter.notifyDataSetChanged();
}
}
But that didn't worked because my custom adapter. In my custom adapter, to use filter, I have two listItems:
ArrayList<ItemData> listItemsToShow = new ArrayList< ItemData >();
ArrayList< ItemData > listItemsBackup = new ArrayList< ItemData >();
So to delete an item, I added new method in my custom adapter:
public void deleteItem(String itemID) {
for (int i = 0; i < listItemsToShow.size(); i++) {
if (listItemsToShow.get(i).getId().equals(itemID)) {
listItemsToShow.remove(i);
break;
}
}
for (int i = 0; i < listItemsBackup.size(); i++) {
if (listItemsBackup.get(i).getId().equals(itemID)) {
listItemsBackup.remove(i);
break;
}
}
Finally to delete an item in list view:
subjectBaseAdapter.deleteItem(subject.getId());
subjectBaseAdapter.notifyDataSetChanged();
Hope this help