Reputation: 9101
I have populated a list view using the custom cursor adapter, That listview consists of a checkbox
and button
in the below manner.
checkbox1 button1
checkbox2 button2
.
.
.
Now I want to navigate to another activity when click on the button in list view now my issue here is when I click on the button1
then I want to see the data in checkbox1 and similarly for other rows as well.
Now I am confused on how to maintain a sync
between the checkbox and button and how to achieve the click functionality. How can I achieve this?
Upvotes: 0
Views: 413
Reputation: 1242
Check this post might help. You have to preserve the state of row in listview so row can be populated with its state.
Upvotes: 0
Reputation: 10093
Use a ViewHolder
class in adapter class and use that in getView()
, Then within getView
viewHolder.yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(viewHolder.checkbox.isChecked())
// toggle the check and do your work here
}
});
You can learn ViewHolder
pattern here its very simple
Another example link
Upvotes: 1
Reputation: 1111
This Selectable ListView Tutorial link should help in your solution.
Upvotes: 0
Reputation: 11999
You will need to find your corresponding view in the list view implementing the onclickListener
For eg
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Button b = (Button)v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(this,SecondActivity.Class)
startActivity(i);
}
});
//Similarly for the checkbox
}
});
Upvotes: 1
Reputation: 13520
You can do the following on the Button
's onClickListener
if the Button
and CheckBox
share the same parent
boldButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
View parentView = (View) view.getParent();
CheckBox checkBox = (CheckBox) parentView.findViewById(R.id.check);
}
});
This will give you the CheckBox
corresponding to the clicked Button
Upvotes: 1