listview切换开关仅适用于最后一行

时间:2017-12-03 21:38:31

标签: 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.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;

    }
}

1 个答案:

答案 0 :(得分:1)

问题的解决方案是你不得不将数据作为字符串,而应该将其改为代表每一行的东西。

例如,您可以将此类设置为代表您的行:

class Item {
    String data;
    boolean toggled;

    public Item(String data) {
       this.data = data;
    }

    public void setToggled(boolean toggle) {
        toggled = toggle;
    }
}

您的数据源现在不能是字符串,因此您应该将列表作为列表,并且您可以通过传递新项目轻松地将字符串添加到列表中(&#34;此处的字符串数据&#34;);将数据添加到列表时。 而不是

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()部分中使用它。 您实际上不必创建视图的新实例,因为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;
}