我面临的问题是,当我切换开关时,它仅适用于 ListView 的最后一项。我切换哪个开关并不重要。我研究了其他提出的问题,但我无法弄清楚。
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.row, parent, false);
String allData = getItem(position);
final Switch status = (Switch) customView.findViewById(R.id.switchStatus);
status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show();
}
});
return customView;
}
您可以在此处查看我的适配器 Activity 。
public class adapter_test extends ArrayAdapter<String> {
public adapter_allservicerecords(Context context, String[] data){
super(context, R.layout.row, data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.row, parent, false);
String allData = getItem(position);
try {
if(!all_data.isEmpty()) {
JSONObject jsonRowData = new JSONObject(all_data);
try {
text.setText(jsonRowData.getString("TITLE"));
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
Log.e("FAILED", "Json parsing error: " + e.getMessage());
}
return customView;
}
}
最佳答案
问题的解决方案是您必须不将数据设置为字符串,而应该将其更改为代表每一行的内容。
例如,您可以设置此类来表示您的行:
class Item {
String data;
boolean toggled;
public Item(String data) {
this.data = data;
}
public void setToggled(boolean toggle) {
toggled = toggle;
}
}
您的数据源现在不能是字符串,因此您应该将列表设置为列表,并且可以通过传递 new Item("THE STRING DATA HERE"); 轻松地将字符串添加到列表中将数据添加到列表时。 所以而不是
String allData = getItem(position);
你会使用
Item item = getItem(position);
然后在 getView 上看起来像这样:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.row, parent, false);
Item item = getItem(position);
final Switch status = (Switch) customView.findViewById(R.id.switchStatus);
status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// here we change the value of the item which is referring to the specific index in the array returned by getItems()
item.setToggled(isChecked);
Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show();
}
});
// here we get what the latest value of the switch is
status.setChecked(item.toggled);
return customView;
}
确保您还将数据源更改为表示有点类似于 List 或您决定使用的任何类似内容的数据结构/列表。
额外提示: 建议使用 ViewHolder 来保存 View 并有效回收实例。
class ViewHolder {
Switch statusSwitch;
View customView;
}
然后在 getView() 部分中使用它就可以轻松地进行。 实际上,您不必创建 View 的新实例,因为 ConvertView 或方法的第二个参数表示如果为 null,则可以重用或实例化的 View 。
异构列表可以指定其 View 类型的数量,以便该 View 始终是正确的类型,因此建议您使用它。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
ViewHolder holder = null;
Item item = getItem(position);
if (convertView == null) { // instantiate if not yet instantiated
convertView = inflater.inflate(R.layout.supplies_list_item, null);
holder.statusSwitch = (Switch) convertView.findViewById(R.id.switchStatus);
}
else {
holder = (ViewHolder) convertView.getTag();
}
// you could set the values/ listeners here
holder.statusSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
item.setToggled(isChecked);
Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show();
}
});
holder.statusSwitch.setChecked(item.toggled);
return convertView;
}
关于java - ListView 切换开关仅适用于最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47623590/