How to use Adapter to access views in Recycler View - android

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
}
});

Related

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.

How to ensure that no two checkboxes are enabled in android

I have a custom list view and each item of it has a check box and a text field. I want to disable the check box in every other row of the list view if the check box of the first row is enabled.
Since you are stating that if the first check box is clicked all the others should be disabled, do a check in your onBindView() to see if the checkbox is checked. if it is, set a value to say that it is, and for all the following list view entries set the checkbox to disabled.
In your onCheckChangedListener():
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked()){
firstCheckboxChecked = true;
}
});
Then in your onBindView for the rest:
if(firstCheckBoxChecked){
checkbox.setEnabled(false);
}
Here I am putting some rough solution which will help you.
classs model
{
boolean status;
model(boolean status){
this.status=status;
}
}
List<model> list_model= new ArrayList<>();
list_model.add(false);
list_model.add(false);
list_model.add(false);
list_model.add(false);
add this model with listview adapter.
//Adapter coding
checkbox.checked(check)
//write below code in checkbox.setOncheckedItemSelect method.
//you will get already checked parameter in that method.which will put into list
list_model.add(checked);
//here call listview notify datasetchangedListener.
create a boolean variable isFirstEnbled in your adapter. then on click first item's checkbox set this value as isFirstEnbled=true .
adapter.notifyDataSetChanged()
This recreate the view with isFirstEnabled set as true

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);
}

Access adapter from within onCheckedChanged()

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.

Categories

Resources