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.
Related
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
}
});
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;
}
I am using ToggleButtons in a listview, and I set the state of togglebutton from the webservice data, now I changed the state of togglebutton, but it is reset to previous when I scrolling the listview.
I used the following in my adapter class.
tbtnStatus=(ToggleButton)view.findViewById(R.id.togglebtn);
tbtnStatus.setTag(new Integer(position));
tbtnStatus.setOnCheckedChangeListener(null);
tbtnStatus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
String update_status;
String current_status=buttonView.getText().toString();
if(current_status.equals("ON"))
{
update_status="NO";
}
else
{
update_status="YES";
}
String res=UrltoValue.getValuefromUrl(DataUrls.updateindividualstaus+"?pilotid="+DataUrls.pilotid+"&friendid="+friendid[position]+"&status="+update_status);
}
});
State of your toggle button will be cleared to it's default value from webservice data each time when you sroll it out of the screen, because views of all not currently visible rows are deleted and will be recreated with default values. You need to save state of you toggle button. For example, you can save it in object associated with this row if you use ArrayAdapter<Object>.
save your position of View when u check the toggle button of that respective view...and in getView() write this code
if(yourposition==position)
tbtnStatus.setChecked(true);
Suppose you clicked 3rd position toggle..save 3 as yourposition and when u will scroll getView gets called and will satify the condition checking your toggle to yes
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);
}
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.