android 如何在列表视图中保留切换按钮的状态
How to preserve the state of toggle button in listview in android
我有带切换的列表视图 button.i 当我向下滚动切换按钮时遇到问题,切换按钮的状态改变了,而且当我点击第一个切换按钮时,第四个切换按钮也被选中。我还想保存所有切换按钮状态和数组列表中该特定行上的文本。我还想在应用程序关闭时保留切换按钮状态。
我尝试使用 holder 但它不起作用。
下面是我的代码
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;
}
}
when i scroll down the toggle button the state of toggle button
changed
因为目前 getView
方法没有以正确的方式使用 ViewHolder
模式实现。
请参阅以下教程以使用 ViewHolder
实现 getView
方法:
Performance Tips for Android’s ListView
when i click on first toggleButton the fourth toggle button also
checked
因为 ToggleButton
的 holder.tgbtn
对象用于检查 onClick
上 ToggeleButton 的状态。 holder.tgbtn
保留在上次调用 getView
方法期间分配的引用。
根据 ToggelButton
:
使用 v
从 TextView 获取文本
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) v.getParent();
TextView tvName = (TextView) parent.findViewById(R.id.tvName);
// Use tvName to get TextView text
}
我有带切换的列表视图 button.i 当我向下滚动切换按钮时遇到问题,切换按钮的状态改变了,而且当我点击第一个切换按钮时,第四个切换按钮也被选中。我还想保存所有切换按钮状态和数组列表中该特定行上的文本。我还想在应用程序关闭时保留切换按钮状态。
我尝试使用 holder 但它不起作用。
下面是我的代码
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;
}
}
when i scroll down the toggle button the state of toggle button changed
因为目前 getView
方法没有以正确的方式使用 ViewHolder
模式实现。
请参阅以下教程以使用 ViewHolder
实现 getView
方法:
Performance Tips for Android’s ListView
when i click on first toggleButton the fourth toggle button also checked
因为 ToggleButton
的 holder.tgbtn
对象用于检查 onClick
上 ToggeleButton 的状态。 holder.tgbtn
保留在上次调用 getView
方法期间分配的引用。
根据 ToggelButton
:
v
从 TextView 获取文本
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) v.getParent();
TextView tvName = (TextView) parent.findViewById(R.id.tvName);
// Use tvName to get TextView text
}