Java listview切换开关仅适用于最后一行
我面临的问题是,当我切换开关时,它只适用于listview的最后一项。我切换哪个开关并不重要。我研究了其他被问到的问题,但我想不出来Java listview切换开关仅适用于最后一行,java,android,listview,switch-statement,toggle,Java,Android,Listview,Switch Statement,Toggle,我面临的问题是,当我切换开关时,它只适用于listview的最后一项。我切换哪个开关并不重要。我研究了其他被问到的问题,但我想不出来 @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(getContext()); View customView = inflater.infla
@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;
}
你可以在这里看到我的适配器活动
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;
}
}
公共类适配器\u测试扩展了ArrayAdapter{
公共适配器\u allservicerecords(上下文上下文,字符串[]数据){
super(上下文、R.layout.row、数据);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
LayoutInflater充气器=LayoutInflater.from(getContext());
视图customView=充气机。充气(R.layout.row,父级,false);
字符串allData=getItem(位置);
试一试{
如果(!all_data.isEmpty()){
JSONObject jsonRowData=新的JSONObject(所有_数据);
试一试{
setText(jsonRowData.getString(“TITLE”);
}捕获(例外e){
e、 printStackTrace();
}
}
}捕获(例外e){
Log.e(“失败”,“Json解析错误:”+e.getMessage());
}
返回自定义视图;
}
}
问题的解决方案是,您必须将数据改为表示每一行的内容,而不是将其作为字符串
例如,可以将此类设置为表示行:
class Item {
String data;
boolean toggled;
public Item(String data) {
this.data = data;
}
public void setToggled(boolean toggle) {
toggled = toggle;
}
}
您的数据源现在不能是字符串,因此您应该将列表作为列表,您可以通过传递新项(“此处的字符串数据”)轻松地将字符串添加到列表中;将数据添加到列表时。
所以不是
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;
}
确保您还将数据源更改为数据结构/列表,该数据结构/列表表示您决定使用的类似列表或任何内容
奖金提示:
建议使用ViewHolder来保存视图,并有效回收实例
class ViewHolder {
Switch statusSwitch;
View customView;
}
然后,要在getView()部分中使用它,您可以很容易地。
实际上,您不必创建视图的新实例,因为convertView或方法的第二个参数表示可以重用或实例化(如果为null)的视图
异构列表可以指定其视图类型的数量,因此此视图始终是正确的类型,因此建议您使用该类型
@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;
}
感谢回复,Item=getItem(position);gaves Error:(48,28)错误:不兼容的类型:字符串无法转换为错误。好的,我更新了我的答案。你要做的是在项构造函数上传递字符串数据。还是一样的都德。@user7732643你能显示创建列表的代码吗?它在适配器活动中。