Delete all list items which are checked - android

I have a ListActivity binding to a database with a Custom SimpleCursorAdapter.
in each of my list items I have 4 texts, image and checkbox (each one is set to focusable:false).
In the list itself I have a long button so when I press on it, I want it to delete all the
rows which thire checkboxes are 'checked'.
Now, I have tried many techniques in order to achieve a simple operation (click listeners, CheckedtextView as shown in the tutorial) but was not successful.
There is also another weird phenomenon occurring, after I #Override onListItemClick, also i dont get any calls at all while I am pressing on any of the list's rows.
Does anybody have any idea how to solve these issues? Thanks.
Thanks,
Moshic.

Are you calling super.onListItemClick(ListView, View, int, long);? if so, try write out a Log.d("ListView", "My list has been clicked"); and see if that comes out in the Logcat. If it doesn't, try extends ListActivity in your activity declaration. comment back if you want my help, I'll be ready to help as much as possible

create your own adapter with following features
-in getView() you have to set click listener for check box(it will be own listener for each check box)
-each element of adapter contains flag checked/unchecked, and you should set it by check box click listener from previous point
in activity with delete button click listener you have to get checked elements from adapter and do with them what you want
after then you should update list view for example by calling (your adapter).notifyDataSetChanged()

Related

Android: ListView with Checkbox

I've searched high and low for this issue but am not able to find something concrete.
Basically, I have a list of friends in a ListView and I am trying to create a group by choosing specific friends. To do this I have created an Activity with a SearchView and a ListView. Searching will filter and display friends that I want to add. To do so, I want to use a checkbox. All the "checked" friends will then be passed to another activity where the group creation can be complete.
The activity's layout has a ListView.
I have written my own adapter and this adapter inflates the row layout for the ListView. The layout of the adapter has a CheckBox that I have explicitly added.
So activity layout has: ListView
Row layout has: TextView and Checkbox
But am having issues with:
1. Trying to keep the checkbox checked when scrolling/searching the listview as the view refreshes and the checkbox is recreated as unchecked - I've solved this for the time being by maintaining a list of the checkboxes clicked. I am doing this in the getView() method of the adapter.
2. Trying to determine which row of the ListView the checkbox belongs to. I'd need this to then create my group of friends.
3. Allowing to edit the list of chosen friends - to add/remove friends before creating the group.
Here is a diagrammatic representation:
EDIT
Adding in another diagram to show how it works.
So from the first activity, I go to the second one to search and add friends. Once I've chosen my friends, I send the list back to my first activity when I click on 'OK' button.
I finally figured this out.
As mentioned, I was not sure how to pass the "control" back to the activity when the checkbox is checked. This was an issue because the checkbox was part of the layout inflated by the adapter.
Within the adapter, I set this up to pass the "control" back to the activity to handle the check on the checkbox:
checkbox.setOnCheckedChangeListener((SearchFriendActivity) context);
And within the SearchFriendActivity, the check on the check box can be handle by:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, "Inside onCheckedChanged");
//The below code line will get the position of the row item
int pos = friendlistview.getPositionForView(buttonView);
if(pos != ListView.INVALID_POSITION)
{
//Logic Here
}
}
Remember to implements CompoundButton.OnCheckedChangeListener in the activity.
A neat little tutorial on this
here
There is AlertDialog with method setMultiChoiceItems() You can make dialog with checkbox list. Could it help?
I don't know it work or not because i'm not on my dev pc right now i can't post you the code. But try this:
1 Implement checkbox event listener (I'm not sure maybe onCheckChanged()) and store the checkbox value or index in list (better use index)
2 if u had store index, u can tell which checkbox belong to which row. if not maybe u should setTag() to row or TextView of row so u can compare it before intent to next activity.
3 you don't tell us what view that group use. It hard to answer but i think value u got from getIntent() maybe a list so u can display it with some view with remove button. So just remove value from list before create group.
For add more friends maybe u should intent the list intent back to first activity and check the checkbox where it checked and allow user to check/uncheck it again. After that just intent it to that group activity again.
This is an easy task, follow the steps:
make a layout xml file and define how a single check box will look
create a list view.
populate the listview as listview is populated for simple textview.
If you don't have enough idea about populating list view then try this link
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html
let me know if you face any problem with this.
happy coding!!

Android - Get item from listview with custom adapter through checkbox

I have a listview with a custom adapter (extends BaseAdapter).
It recieves a list of objects I have to populate a ListView.
One of my object's atribute is a boolean called "checked".
on my method getView, this atribute is responsible for checking or not checking a CheckBox on my View.
Everything is working just fine and when my Activity loads, the ListView itens apear as they are on my list of objects (which was received from my database), some checked and some not checked.
But when I check one of my ListView's checkbox, I need to update my object and therefore it's value on my database. The problem is:
"How do I know which item (object) I have to update just by checking my CheckBox?"
"Don't they have the same name?"
I have a listView.setOnItemClickListener(...) where I can get my object by it's position, but it works when I click on the "row" of my list view itself, not on my checkbox... I thought about using it to check/uncheck my CheckBox... But how would I do that? Can I use the position to get a specific CheckBox from my listView?
In the end, I also thought that the best method would use the "listView.setOnItemClickListener(...)" to check my CheckBox, once it would be easier for my user to check one Item by it's row than by a tiny CheckBox, so can someone help me with the best way to solve my problem?
I'm sorry I didn't post my code, but right now I can't access it.
Take a look at this tutorial
You need to add an method in the onClick of the checkbox that will be implemented by the activity. One way to do that is to add an abstract method to the adpater and make the activity implement it. lets call onCheckBoxClicked(int position)

Change imageview for selected item in listview

In my layout I have a button and a listview. How can I change the imageview of item I selected when the button is clicked. So lets say, I select 5 items, and after I click the button, images for those 5 items will be changed.
So I am confused what function I should use. Right now i used button.setOnClickListener but it seems wrong because only the very first item's imageview will be changed when button clicked. Should I use listview.setItemOnClickListener? Or is there any other way I can do this?
Thanks a lot!
Add a boolean to the data object in your adapter. Say you've got ArrayAdapter<MyDataObject>. Add some kind of "selected" field in the MyDataObject, and toggle it when you "select" the row.
Override getView in the adapter (you'll need a custom Adapter, btw. I'd just extend ArrayAdapter). When you render the row, if the "selected" field is true, show the "other" image.
When you click the button, call 'notifyDataSetChanged' on the adapter. The will cause the visible rows to refresh themselves (and call getView for each).
I think that'll work.
Since you only want images to change when you click the button, you'll need to have some kind of global boolean, so the getView won't show the image until the button has been clicked.
The complication here is, you have to deal with rows that may have been scrolled out of view, which don't have active views, but logically exist. It would be really hard to explain the concept here. I'd suggest some tutorials on ListView if you're not familiar with the recycling of row views.
You can used custom baseadapter for listview and setonclicklistener in the getView() method.... see tutorial here this is hope for help
You can use button click listener as well as on itemClickListener too but to make a image view in selected state in a list, you have to call setSelected method of imageview parent layout.
Please put a comment if you don't get me.
Thanks

ListView updating Items without being recreated

I want my ListView to work something like the following:
When I press a button (probably from context-menu), I want the user to be able to select more then one item from ListView (probably using check-boxes), but those check-boxes should not be visible before that.
So, the point is, after the user presses a button (let's say "Delete more items"), the listview, should update itself, and appear on every row of the list, a checkbox should appear (allowing me to select the items ID to pass those to server).
How can I achieve that, without having to recreate the list from zero? (how to setVisibility ON, keeping the other content of the ListView as it is, and not doing another request to server).
PS. If you guys, have another better idea, on achieving the Delete More Items, would be much appreciated!
This is just an idea, haven't tried it myself: you build in a checkbox in your listitem layout. Normally, in the getView of your adapter, you set it invisible with
checkBox.setVisibility(8);
When you want to show them, you set some boolean
showBoxes
of your adapter to true, then in the getView oyu don't hide the checkboxes.
Then
notifyDataSetChanged
on the adapter.
Hope it's clear what I mean.

Android: how to make only one item of ListView clickable?

I am working on a simple to-do list app, and on the main view I have a list of tasks with a checkmark aside to each, as the image below:
Upon clicking on the checkmark I want the respective task to be removed from the list. I tried to implement this as TextViews (for the tasks) and Buttons (for the checkmark), but I would need to know the number/position of the clicked checkmark (0,1,2 or 3) to remove the correct task from my array. Can I get this some how?
I also thought about implementing the tasks/checkmarks as a ListView, but then I would need to set a onItemClickListener only on the checkmark, and not on the task text. Is this possible?
Any other ideas? Thanks.
Checkbox has a tag property which you can use to set custom data like row number. Something like this should work,
checkbox.setTag(row_number);
So when you click on it, do something like
int rowNum = Integer.parseInt(checkbox.getTag());
removeTaskAt(rowNum);
If you're using a Custom Adapter, you can add a click listener on the check on getView, so the click is only on the checkbox and not on the full item.
The only problem with this is that the scroll may not work if you try to scroll by clicking on the checkbox.

Categories

Resources