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;
}
}