Android - Get item from listview with custom adapter through checkbox - android

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)

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!!

How to get correct position of a checkbox in a scrollable listView?

I have a scrollable listview that has 2 textViews, 3 imageButtons and 1 checkBox in each row.
In the Header of the listView, I have a delete button just above the column containing all the checkboxes of the listView.
I have to perform the delete operation of objects in the listView when 2 or more checkBoxes are checked randomly by scrolling the listView and thereafter the delete button at the top is clicked.
But the problem is that i am not getting the correct poition of the checkbox that was selected. Moreover, sometimes i get the correct position but still the object to be deleted passed is wrong. Hence the entire functionality is affected maybe due to the scrolling nature of the list.
Should i take the position in the holder of the adapter class and also bind the state of the checked or unchecked checkbox with my object.
And should I use checkBox.setOnCheckedChangeListener() or deleteButton.setOnClickListener().
If i use the latter one, then how to get all the corresponding objects of the list whose checkboxes were checked before pressing the delete button?
And where should all the related code be placed..in the listAdapter class or in the activity?
Please help me find a solution to this problem..
first of all in your getView() method you should set a specific Tag to each checkBox. For example: check1.setTag(position) then you should implement both OnCheckBoxChangeListener for your checkBoxes and OnClickListener for your delete button. As you know setting of onCheckBoxChangeListner have to be in getView() method. Then you add positions of list that their checkBox is checked to the a ArrayList with the help of getTag() method of chechBoxes in onCheckedChanged() method.
Try getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {}

Preselect items in listView Android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.
Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

Android - updating the simpleCursorAdapter

I have a ListActivity which gets its data from a database query. I also have a custom adapter which extends simple cursor adapter.
To show the customised content, I have overridden the newView and bindView methods.
Each element of the view has:
TextView containing a title
ImageView containing number of stars - the image shown is changed based on a value obtained from the database
A button - the button text changes on clicks (Favourite/Make Favourite), and triggers a database update event.
My problem is this - when I scroll the ListView, the changes I made seem to disappear .. for instance, the first item is marked favourite, and the list is scrolled ... when I come back to the first item, the text reverts back to its previous value, although internally the db has been updated.
I read that the notifyDatasetChanged() is not right for this case as the adapter should be notified of the database changes. I am trying to use the reQuery() method, but do not know where to place it.
Should I place the reQuery() in the onClick method of the button? If not, where should it be placed?
What so ever Adapter is you are using must have getView Method.. Whenever a list is scrolled this getView Method got called for all the listItem in the view. Make Sure in the getView Method. That the view is getting generated dynamically.
It should check for clicked or changed value.
to update the list without scrolling your have to call listView.getAdapter().notifyDataSetChanged()
Check the following link. here i mentioned a complete code to use listview properly. Using this we can achieve any listview behavior. We can embed animation also.
Change ListView background - strange behaviour
Hope this help :)
If you have extended BaseAdapter ,
after data is changed call , listView.setAdapter() and in getView() , update the view's data too.
Use SetListAdapter with ArrayAdapter to display the list. Use ConvertView to avoid inflating View.
if (convertView==null)
{
//inflate your list
}

Delete all list items which are checked

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()

Categories

Resources