Access adapter from within onCheckedChanged() - android

I've created an adapter for ViewPager. When I create it, I also have to dynamically create radio buttons on the layouts. However, by clicking one of the radio buttons, i need to notifyDataSetChanged of the adapter it's inside. How do I access that adapter? For example, if I wanted to do this:
radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
viewPagerLength++;
// need to call notifyDataSetChanged();
}
}});

This can be done by setting radioButton like so:
final RadioButton radioButton = (RadioButton) findViewById(R.id.rb);
and then you can access radioButton from within.

Related

How to use Adapter to access views in Recycler View

I want to use some methods on this switch which is within a RecyclerView. I want to use methods like mSwitch.setEnabled(true) in the MainActivity:
how do I do this? Please help! I'm new to android and the sad part, I haven't subscribed any android course
you click on child views like this in recyclerView from recyclerViewAdapter
viewHolder.switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the On position
}
});

Switch in Custom Listview

I am populating a ListView via CursorAdapter from SQLite Database Layout contained Switch.
How can I listen to the Switch change event for a particular ListItem?
Register a callback to be invoked when the checked state of this button changes.
Example:
mySwitch = (Switch) findViewById(R.id.mySwitch);
// attach a listener to check for changes in a state
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// do something on event
}
});
Also check viewholder pattern.

Disable listview row when checkbox is checked

In my application I am using Custom Listview with row as TextView and CheckBox.
Now I want that when I check CheckBox at that time that respective row have to Disable(unfocasable).
What should be the code inside CheckBox OnCheckedChangeListener
I also tried to disable rowlayout inside Listener but not got success...
following is code I am using inside getView method of Adapter
final RelativeLayout RL=(RelativeLayout)convertView.findViewById(R.id.VTRLmain);
check.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if (isChecked)
{
RL.setEnabled(false);
templist.add(getItem(position));//using for another activity
}
}
});
give in xml code for checkbox
android:focusable="false"
it won't hide listview textboxs.
and check the application theme.
In the adapter there is a method named isEnabled which you can overide. Try to do in your custom adapter
Try this code:
#Override
public boolean isEnabled(int position) {
if(checkbox1.isChecked()){
return false;
}
return true;
}

CheckBox inside a View inside a ListView

Here's my aproach:
I have a custom ListView containing a custom Adapter containing two different kind of Views. One of them has a CheckBox in each View.
I just want to notify the Activity when one of those CheckBoxes have been clicked, and to pass it a boolean: true if ANY of the boxes are checked, false otherwise.
How should I do it?
I just need the theorethical answer, not code.
Thank you very much.
1- implement the activity by checkedChangeListene and override the code in activity
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Object obj = buttonView.getTag();
// perform logic
}
}
2- pass the activity in custom adapter constructor.
3- set in getView
CheckBox chkBx = (CheckBox ) findViewById( R.id.repeat_checkbox );
if(null!=chkBx ){
chkBx.setOnCheckedChangeListener(mActivty);
chkBx.setTag(position);
}

Android : Custom ArrayAdapter showContextMenu onClick

In my Android application I have a custom ArrayAdapter for managing rows in a List that implements onCLickListener.
In the onClick() method I want to show the context menu.
public class MyAdapter extends ArrayAdapter<MyClass> implements
OnClickListener {
public void onClick(View v) {
v.showContextMenu();
}
}
In my main List Activity class I override the onLongListItemClick() method that starts a different Activity.
When I click on a row in the list - the context menu is shown correctly but a second later the onLongListItemClick() is also executed. How to stop this happening?
thanks!
After your comments, I think I have a solution. Don't use the row onClick for the context menu, set a click listener in your adapter for the checkbox and show your context menu from there. That way you avoid the onLongListItemClick.
To make both the checkbox and the row clickable, add this to the checkbox definition in your XML:
android:focusable="false"
android:focusableInTouchMode="false"
EDIT
This should do the trick:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
View v = buttonView.getParent(); // get the parent row view containing the checkbox
v.showContextMenu(); // show the context menu
}
}
}

Categories

Resources