0
我有列表视图切换button.i面临一个问题,当我向下滚动切换按钮切换按钮的状态改变,还当我点击第一个toggleButton第四切换按钮也被选中。我还想要在arraylist中保存所有切换按钮状态和特定行上的文本。我也想在应用程序关闭时保留切换按钮状态。如何保留切换按钮状态在Android中的列表视图
我试图使用持有人,但它不工作。
下面是我的代码
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
final 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);
//final ToggleButton tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);
// Populate the data into the template view using the data object
holder.tvName.setText(user.name);
holder.tvHome.setText(user.hometown);
// holder.tgbtn.setTag(position);
/** The clicked Item in the ListView */
RelativeLayout rLayout = (RelativeLayout) convertView;
/** Getting the toggle button corresponding to the clicked item */
holder.tgbtn = (ToggleButton) rLayout.getChildAt(2);
holder.tgbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.tgbtn.isChecked()) {
holder.tgbtn.setChecked(true);
String homet=holder.tvHome.getText().toString();
Toast.makeText(getContext(),homet+"Blocked", Toast.LENGTH_SHORT).show();
} else {
holder.tgbtn.setChecked(false);
Toast.makeText(getContext(), "Unblocked", Toast.LENGTH_SHORT).show();
}
}
});
// Return the completed view to render on screen
return convertView;
}
private class Holder{
TextView tvName;
TextView tvHome;
ToggleButton tgbtn,tg1;
}
}