如何将listView中的checked项添加到ArrayList?

时间:2015-02-12 13:16:17

标签: android listview android-listview getview

我有一个包含12个项目的ListView,每个项目包含一个TextView和一个复选框。我设法保持CheckBox检查状态持久,这样当我向上和向下滚动时,我不会丢失为每个CheckBox设置的检查状态。

我现在面临的问题是,对于ListView中的每个项目,如果选中相应的CheckBox,我想将TextView添加到ArrayList,如果未选中相应的CheckBox,我应该从ArrayList中删除TextView,如果它包含在其中。

在下面的getView()方法代码中,行" Log.d(TAG,"选择的主题大小:" + chosenTopics.size());"总是返回零?? !!

请帮我解决这个问题。

getView()

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view = null;

    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        view = layoutinflater.inflate(R.layout.list_items_layout, null);

        final ViewHolder holder = new ViewHolder();

        holder.tv = (TextView) view.findViewById(R.id.tvlist_topic);
        holder.cb = (CheckBox) view.findViewById(R.id.cbList_hook);
        holder.iv = (ImageView) view.findViewById(R.id.ivList_delete);

        holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                ItemDesign element = (ItemDesign) holder.cb.getTag();
                element.setChecked(buttonView.isChecked());

                if(element.isChecked()) {
                    if (!chosenTopics.contains(designList.get(position).getTopic()))
                        chosenTopics.add(designList.get(position).getTopic());
                }

                if (!element.isChecked()) {
                    if (!chosenTopics.isEmpty()) {
                    if (chosenTopics.contains(designList.get(position).getTopic()))
                        chosenTopics.remove(position);
                    }
                }
            }
        });

        Log.d(TAG, "chosen topic size: " + chosenTopics.size());
        view.setTag(holder);
        holder.cb.setTag(designList.get(position));

    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).cb.setTag(designList.get(position));
    }

    final ViewHolder holder = (ViewHolder) view.getTag();
    holder.tv.setText(designList.get(position).getTopic());
    holder.cb.setChecked(designList.get(position).isChecked());

    holder.iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (holder.cb.isChecked())
                holder.cb.setChecked(false);

            designList.remove(position);
            notifyDataSetChanged();
        }
    });

    return view;
}
将ArrayList初始化为构造函数后

LogCat错误

02-12 14:40:14.117: E/AndroidRuntime(22500): FATAL EXCEPTION: main
02-12 14:40:14.117: E/AndroidRuntime(22500): Process:  
com.example.mqtt_designlayout_02, PID: 22500
02-12 14:40:14.117: E/AndroidRuntime(22500):   
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
02-12 14:40:14.117: E/AndroidRuntime(22500):    at 
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
02-12 14:40:14.117: E/AndroidRuntime(22500):    at 
java.util.ArrayList.remove(ArrayList.java:403)
02-12 14:40:14.117: E/AndroidRuntime(22500):    at 

com.example.mqtt_designlayout_02.listviewadapter.ListViewAdapter $ 1.onCheckedChanged(ListViewAdapter.java:95)

1 个答案:

答案 0 :(得分:1)

  

getView()方法,行" Log.d(TAG,"选择的主题大小:" +   chosenTopics.size());"总是返回零?? !!

因为在chosenTopics方法中初始化getView ArrayList所以当ListView向上/向下滚动时chosenTopics对象再次初始化。取决于调用getView方法的次数。

解决问题移动

this.chosenTopics = new ArrayList<String>();

适配器

的构造函数内的行