I am displaying a list with checkboxes in a dialog. The list looks something like-
Item 1
Item 2
All
with a checkbox beside each item. Now the requirements is-
If Item 1 or Item 2 or both are already checked, and All is selected, Item 1 & 2 should be unchecked.
To accomplish this, I implemented DialogInterface.OnMultiChoiceClickListener 's onClick listener.
public void onClick(DialogInterface dialog, int which, boolean isChecked)
{
if(which == 2 && isChecked)
{
((AlertDialog)dialog).getListView().setItemChecked(0, false);
((AlertDialog)dialog).getListView().setItemChecked(1, false);
}
}
But this does not work. I even tried invalidating the listview by calling Invalidate() & InvalidateViews(), but no success.
Any help will be really appreciated.
Thanks,
Akshay
If I understand correctly, the checkboxes are in a list. There's been a question with the opposite situation: trying to uncheck all boxes. The solution seems to be to call
adapter.notifyDataSetChanged()
Here is the link to that question: Uncheck all checkboxes in a custom ListView
EDIT: Okay, I'll try again :-) Found another question about it: android: Refresh ListView using ListAdapter and SimpleCursorAdapter Hope this helps! :-)
Related
I have a problem on checkbox animation when setChecked(true).
Let me explain my condition.
I have a gridview inside viewpager and one checkbox above the viewpager.
I set an adapter to gridview. There are a checkbox and a icon in the adapter.
When click an item, checkbox animation works fine. However, when try to select all items by one checkbox above the viewpager, all items are selected without checkbox animation, just can see only 2 views, checked box and unchecked box.
Please help me out. Thanks.
// ViewHolder
CheckBox mCb;
mCb.post(new Runnable(){
#Override
public void run(){
mCb.setChecked(Model.isChecked());
}
});
Um... The problem with this code is a bit slow
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!!
I have a simple onclick listener. On click of any list item, I have to make that list item completely visible.
For example I have 10 items in list view, if I click the 10th item which is partially visible, the complete list item should get displayed to the user.
I have tried with the following options. But its not working as expected. Could anyone please share your suggestions to acheive the same.
v.setFocusable(true);
......................
if(!v.isFocused()){
v.setFocusable(true);
}
.......................
I have tried with the following code snippet. It's working fine for me.
public static void setSelectedListItemVisible(int position) {
myListView.setSelection(position);
}
I called this function inside my onClickListener.
I added a button in a listview item, and after the button is clicked, I want the button is disabled. I used the below setOnClickListener for my button in the custom adapter, but the problem is when i clicked on a button, the button of another list item will also be disabled. For example, when I clicked the button of item 1, the button of item 1 then is disabled, but the item 4's button will also be disabled at the same time although i didn't click on it.
And also, when I scroll up and down, all item's button just enable and disable randomly.
Anyone know why is this happening?
holder.viewBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.viewBtn.setEnabled(false);
showInfo();
} });
I was so frustrated when I first faced this problem!
The problem here is that the listview just doesn't remember the state of the button. Dunno if it is a bug but anyways I needed a way out and this is what I did.
I believe you are using a custom adapter with a viewholder. Means you are on the right path. You need to keep an array of booleans whose size is equal to the number of items in your list. in your btnClick() set the state of the item in the array.
Now everytime you scroll, or do soemthing that makes the list redraw, getView() is called. Put a check in your getView() for the items state and enable/disable it. One more thing, Make sure you implement both if{} and else{} for the check.
if(checked){
holder.viewBtn.setEnabled(false);
}else{
holder.viewBtn.setEnabled(true);
}
if you don't do this you'll see weird behaviours. One more thing if you are using the
if(convertview == null){
//create the holder
}else{
convertview = getTag();
}
method, make sure you populate the state after the above step.
I have not seen your implementation but I had to pop up a button in the item and then delete the item from the list using it. So I had to take extra care for maintaining the state.
So be careful about the states once the underlying data has changed.
Sorry about the long post but the problem is such :(
I found a link that has the solution in a basic format
This is happening because ListView reuses Views in an erroneous manner. Either implement your own ListAdapter without View reuse or file a bug report with Google
I'm using a ListView with CheckBox, but as most of you know, when you roll down the scroll, a checked CheckBox gets unchecked as you roll up back the scroll. So i've been reading and i found out that you can pass (using getView) the id of the CB to the position parameter of getView to save the CheckBox state!
But i can't use getView with SimpleCursorAdapter, can i? Because i'm using bindView!
Thanks
What is happening is recycling. 7 rows fit in your screen and when you scroll down, the top one is being recycled for the new one at the bottom. What you should do is to save the states of the checkboxes.
Here is a good solution to a similar problem:
https://github.com/commonsguy/cw-android/tree/master/FancyLists/RateList
I managed to get the checkbox state restored after i scroll up/down using setViewBinder (saw it in another answer):
mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex==4) {
cb=(CheckBox)view;
cb.setChecked(cursor.getInt(4)==0? false:true);
return true;
}
return false;
}
});
But still something weird happens, the CheckBox is being recycled after 7 or 8 positions. If i check the first CheckBox and theres more than 10 positions/rows, the 8th is also checked, same happens when i check the last one, 8 positions up there will be a checked CheckBox.
Any thoughs? Ideias? Help!