开发者

Simple List Item Checked is checking multiple items on click

开发者 https://www.devze.com 2023-03-28 14:05 出处:网络
Alright, so I\'m making a simple todo list app that reads the items from a set string array, and displays them in a list view as a simple_list_item_checked.Everything works great, I\'ve set the onClic

Alright, so I'm making a simple todo list app that reads the items from a set string array, and displays them in a list view as a simple_list_item_checked. Everything works great, I've set the onClickListener so it will check what I'm tapping on, however, when I tap on one item, it also checks the item 8 items away, repeating. (I.e. I tap on the top item and it gets a check, and the item 8 items down gets checked, then 8 down after that gets checked, etc, etc) Here is the code from the main activity:

 package com.disneyland;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Launch extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] Rides = getResources().getStringArray(R.array.rides);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, Rides));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                   开发者_高级运维 long id) {
                CheckedTextView check = (CheckedTextView)view;
                check.setChecked(!check.isChecked());

            }
        });



    }
}

Obviously, my question is why is this happening, and how can I fix it?


I ran into this problem as well, and managed a quick fix. I followed icecreamman's link and found that my ListView XML was missing the line:

android:choiceMode="multipleChoice" 

This allows multiple items to be selected. If I had chosen singleChoice instead, only one one item in the list could be checked at any moment, like radio buttons. This line fixed the issue I had.


I can't say for sure, but I have a guess. Are you doing something with a convertView in your adapter's getView method? It appears that you are not following the convertView pattern correctly if that is the case. Is it the case that there are around 7 or 8 list items on your screen at a given time? You might not be correctly returning the correct view.

This excerpt from an Android book will provide a good explanation of ListViews if you are new to using them: http://commonsware.com/Android/excerpt.pdf


I realise this is quite an old post so you may have already solved it, but for the benefit of people like myself who come looking for the answer, I'll share what I've found.

I have had a similar problem and have done a bit of research on the issue. This article: http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/ gives a few answers and an explanation of why this happens. It seems that the android listView only 'remembers' the view types for what it can see on the screen at any one time, in order to preserve memory. So whilst your adapter will correctly show different data as you scroll, the tick mark is treated as a different view amongst 7 others.

One solution (if you want the same rows to always be ticked) is to use the example in the article above to tell the listView to use 2 or more different row types.

If you want the ticks to be applied dynamically, then you need to update the adapter which feeds the listView, then call adapter.notifyDataSetChanged to update your list.

0

精彩评论

暂无评论...
验证码 换一张
取 消