繁体   English   中英

listview切换开关仅在最后一行有效

[英]listview toggle switch works only last row

我面临的一个问题是,当我切换开关时,它仅适用于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;

}

您可以在这里看到我的适配器处于活动状态。

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

现在,您的数据源不能是字符串,因此您应该将列表作为列表,并且可以通过传递新的Item(“这里的字符串数据”)轻松地将字符串添加到列表中。 将数据添加到列表时。 所以代替

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来保存View并有效回收实例。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM