列表视图切换开关只能最后一行

问题描述 投票:0回答:1

我现在面临的一个问题是,当我拨动开关它的作品列表视图的只有最后一个项目。不要紧,哪个开关拨动我。我研究等提出的问题,但我不能弄明白。

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

    }
}
java android listview switch-statement toggle
1个回答
1
投票

该问题的解决方法是你要的不是使您的数据为字符串,则应该将其更改到的东西,将代表每行。

例如,你可以设置这个类来表示你的行:

class Item {
    String data;
    boolean toggled;

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

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

您的数据源,现在绝不能是字符串,所以你应该不是有你的名单List和你可以很容易地通过新的项目(“字符串数据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;

}

请确保您还更改数据源到数据结构/表表示有点像列表或任何类似的,你决定使用。

额外提示:建议使用ViewHolder握住你的查看和管理情况的有效回收。

class ViewHolder {
    Switch statusSwitch;
    View customView;
}

然后使用你可以很容易的getView()的一部分。你其实不必创建视图的新实例,因为convertView或方法的第二个参数表示可以重复使用或实例,如果空的观点。

异构列表可以指定其视图类型的数量,所以这种观点始终是正确的类型,因此建议您使用。

@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;
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.