I have been trying from two days to solve this problem. I have seen many questions related to this problem. But they didn't work for me.
I have a custom ListView
. It's every row includes one ImageView
, two TextView
s and one ToggleButton
. These all loads fine. When I change state of ToggleButton
to true
, then when I scroll down and again scroll up then that ToggleButton
losses it's state. That again goes to false
.
Here is my ArrayAdapter
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.friends_list_item_layout, parent, false);
RoundedImageView friendThumb = (RoundedImageView) row.findViewById(R.id.friendDp);
TextView friendName = (TextView) row.findViewById(R.id.friendName);
TextView friendNumber = (TextView) row.findViewById(R.id.friendNumber);
ToggleButton shareToggle = (ToggleButton) row.findViewById(R.id.shareLocatioToggle);
friendName.setText(myItems[position]);
friendNumber.setText("8888888888");
//friendThumb.setImageResource(R.drawable.friend_dp_thumb);
shareToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg1) {
Toast.makeText(getApplicationContext(), "Shared with "+myItems[position], Toast.LENGTH_SHORT).show();
}
}
});
shareToggle.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Share your location", Toast.LENGTH_SHORT).show();
return false;
}
});
return row;
}
}
Thanks