我从两天开始就一直在尝试解决这个问题。我已经看到很多与此问题相关的问题。但它们对我不起作用。
我有一个习惯ListView
。它的每一行包括一个ImageView
,两个TextView
s和一个ToggleButton
。这些都很好。当我将状态更改为ToggleButton
totrue
时,当我向下滚动并再次向上滚动时,就会ToggleButton
失去它的状态。这又是false
。
这是我的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;
}
}
谢谢