0

I have a ListView with 12 items each item contains a TextView and a checkbox. I managed to keep the CheckBox check status persistent so that when I scroll up and down I do not lose the check status set to each CheckBox.

The problem I am facing now is, for each item in the ListView I want to add the TextView to an ArrayList if its correspondeing CheckBox is checked, and if the corresponding CheckBox is unchecked, I should delete the TextView from the ArrayList if it is contained in it.

In the below code of the getView() method, the line "Log.d(TAG, "chosen topic size: " + chosenTopics.size());" always returns zero??!!

please help me to solve this issue.

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

LogCat errors after moving initialisation of the ArrayList into the constructor:

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 Answer 1

1

getView() method, the line "Log.d(TAG, "chosen topic size: " + chosenTopics.size());" always returns zero??!!

Because initializing chosenTopics ArrayList inside getView method so when ListView is scrolling up/down chosenTopics object is initializing again. depend on how many times getView method is called.

To fix issue move

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

line inside constructor of Adapter

Sign up to request clarification or add additional context in comments.

3 Comments

getView() will be called when you scroll the list. so initialize the arraylist inside you constructor and use that object to set the widgets
ok, but now i receive "index ut of bound exception" on this line "chosenTopics.remove(position);" please see logcat errors posted above
@Kantesh: Instead of holder.cb.setTag(designList.get(position)); Pass only position as holder.cb.setTag(position); and get position inside onCheckedChanged like int clickedPostion=Integer.parseInt(buttonView.getTag().toString()) now use clickedPostion for getting ItemDesign from designList and use clickedPostion for remove

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.