I have listview with toggle button .when i checked first toggle button the 5th toggle button get checked automatically. and when i checked 2nd toggle button 7th toggle button get checked .and when i unchecked that 5th toggle button it is toast the null value .
the below is my code
public class CustomUsersAdapter extends ArrayAdapter<User>
{
public CustomUsersAdapter(Context context, ArrayList<User> users)
{
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//Get an instance of our cell holder
Holder holder;
holder = new Holder();
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
// Lookup view for data population
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
holder.tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
holder.tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);
convertView.setTag(holder); //Add this
}
else
{
holder= (Holder) convertView.getTag();
}
holder.tvName.setText(user.name);
holder.tvHome.setText(user.hometown);
/** The clicked Item in the ListView */
RelativeLayout rLayout = (RelativeLayout) convertView;
/** Getting the toggle button corresponding to the clicked item */
final ToggleButton tbt = (ToggleButton) rLayout.getChildAt(2);
tbt.setOnClickListener(new OnClickListener() {
String homet;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (tbt.isChecked()) {
//tbt.setChecked(true);
ViewGroup parent = (ViewGroup) v.getParent();
TextView tvName = (TextView) parent.findViewById(R.id.tvName);
homet=tvName.getText().toString();
Toast.makeText(getContext(),homet+"Blocked", Toast.LENGTH_SHORT).show();
} else {
tbt.setChecked(false);
Toast.makeText(getContext(),homet+ "Unblocked", Toast.LENGTH_SHORT).show();
}
}
});
// Return the completed view to render on screen
return convertView;
}
//this holder class will be filled from the layout xml and attached to the row as a tag object
private class Holder
{
TextView tvName;
TextView tvHome;
ToggleButton tgbtn,tg1;
}
}
please help me...
and how to save the all toggle button states , so that i can used that saved state to preserve the state of toggle button when the app reopened.