Android: Dynamically show all checkboxes in adapter onLongClick - android

I was learning custom adapter concepts recently.
Problem is during onlongClick in a row,
I want to show checkboxes checked in the longclicked row, which is in android:visibility="gone" initially. And also to show checkboxes in other rows which are not clicked in unchecked state.
I changed some parts of this code here.
http://windrealm.org/tutorials/android/listview-with-checkboxes.php
In simplerow.xml I made
android:visibility="gone" initially.
Now I made a onLongClickListener inside getView(...) method
textView.setOnLongClickListener (new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
CheckBox cb = (CheckBox) v;
Planet planet = (Planet) cb.getTag();
planet.setVisibility(View.VISIBLE);
planet.setChecked(true);
}
});
Now the above code will affect only the longClicked row. How do I make changes in the non-clicked rows?
Calling notifyDataSetChanged() on long click did not work because the other rows have checkbox initially in android:visibility="gone".
Please help. Is any other work around possible?

ListView accompanies Adapter. All your model items would be inflated into the view items through the implemented method named getView from Adapter. If you want to update view without changing data, call notifyDataSetChanged afterwards.

Related

Android: Identify listviewItem when checkbox of item is checked/unchecked

I got a listView with dynamically created items. Each item has a checkbox which can be checked/unchecked. When the checkbox is pressed this function is called:
public void updateClientList(View v) {
}
Now I want to change the specific item in my arraylist which is the base of the listview. But how do I know which checkbox was checked?
How can I do that? Thanks for your help!
If you only have a checkbox, you can use the OnItemClick listener that gives you position besides the view, but with this solution you have to update the checkbox manually so it doesnt consume the item click.
Another way is to use setTag(position) on the views you supply when binding.
You should consider moving on to RecyclerView.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

Android setChecked animation

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

Checkboxes in a ListView - When I select the first, it checks the last and vice versa

I've set up a ListView that contains a Checkbox in each row (along with a TextView). For some reason, when I "check" one of the boxes, it seems to "check" whichever box is opposite in the ListView (i.e. when selecting the top box, the bottom checkbox becomes selected while the top remains unchecked.
I realize there isn't any code here to work with, but I was just wondering if this is a general problem? If not, I can try to post some code. Thanks!
Views are recycled in a ListView. That's why some are checked when you think shouldn't be.
Here's the deal: The checkbox has no idea which item in your adapter it represents. It's just a checkbox in a row in a ListView. You need to do something to "teach" the rows which item in your data set they are currently displaying. So, instead of using something as simple as a String array as the data for your adapter, create a new model object that stores the state of the checkbox in it. Then, before you return the row in getView(), you can do something like:
//somewhere in your class
private RowData getData(int position) {
return(((MyAdapter)getListAdapter()).getItem(position));
}
//..then in your adapter in getView()
RowData object = getModel(position);
if(object.isChecked()) {
myCheckbox.setChecked(true);
} else {
myCheckbox.setChecked(false);
}
//then retun your view.

Android - setSelected in OnItemClick in ListView

I am trying to set item selected in OnItemClick event in ListView and it just wouldn't leave item selected. What am I doing wrong?
lView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(#SuppressWarnings("rawtypes") AdapterView parent, View clickedview, int position, long id)
{
clickedview.setSelected(true);
mItemsAdapter.select(position);
}
});
few things:
1. I am trying to implement Multiple Select on the list View.
2. I cannot extend from ListActivity because Activity extends from BaseActivity custom class already.
3. mItemsAdapter is a custom ItemsAdapter adapter that extends BaseAdapter.
4. I don't need a checkbox in there, just to be able to see the row selected is fine.
5. ItemsAdapter overrides getView() and sets the layout of the row by inflating xml
I could manage to get an item of a ListView to be set as selected when long-clicked:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
parent.requestFocusFromTouch(); // IMPORTANT!
parent.setSelection(position);
return true;
}
It only worked after I called requestFocusFromTouch().
I currently don't have much time. So I'll take a look again later this day.
Anyway take a look at my previous questions, I was struggling with the same:
Change ListView background - strange behaviour
Clickable ListView
In case the solution for you is not in there (I think it's in the first one) we will need more code, in order to help you.
Hope this helps a bit.
I have not had much luck using the built in functionality for selection.
I see you have your own custom adapter, which I am guessing means your inflating custom views as rows. If your row has anything more then a checkedtextview I don't think you will be able to correctly use setSelections.
I solved this problem by using my own models and functions. Each item in the list had data with it to determine it if was selected or not. I could then iterate though that array, toggle selections with and and even update the UI by changing values and calling notifydatasetchanged on the adapter (which used getView and checked against my selection model to draw checks).

Custom list clicking with checkboxes

I've populated a ListActivity from a Cursor using SimpleCursorAdapter that starts another activity when one of the list items have been clicked. I'm also using ViewBinder to do some custom transformation of the data.
I want to add a CheckBox to each row in the list so I've changed the view and added a CheckBox with gravity right.
Adding the CheckBox has removed the ability to click on the items. The onListItemClick method I was overriding in ListActivity is no longer called when you press on a list item. Removing the CheckBox fixes this. Why is this?
Also, how can I set up the list so that it continues to perform my required functionality if the main part of the list item is clicked but have additional functionality when the CheckBox in the item is checked? Will setting a onCheckedChangedListener work or is the same view instance reused for each item in the list?
As explained here, the click listener only works if no other view is focusable. Setting your CheckBox to focusable="false" should do the trick:
<CheckBox android:focusable="false" />
Looks like SimpleCursorAdapter is too primitive for what I wanted to achieve.
I've switched to implementing CursorAdapter and returning a new view using the LayoutInflater in my implementation of the newView method.
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.alarm_row, parent, false);
}
In bindView I then set a custom OnClickListener to my main LinearLayout and then another OnCheckedChangeListener to the CheckBox.
For all this to look right I had to set the LinearLayout's background to android's menuitem drawable:
android:background="#android:drawable/menuitem_background"

Categories

Resources