android: chaotic selection in ListView

2019-09-11 03:43发布

In my application I have a ListView and ArrayAdapter. When I select some one element in ListView I can see also few more selected elements.

Furthermore, when I scroll elements in ListView (after selection) then my selection disappears and elements are selected randomly.

Code of adapter:

final ListAdapter adapter = new ArrayAdapter<BinLocationsComponent>(activity, R.layout.simple_list_item_multiple_choice, locationComponents) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if ( convertView == null ) {
            convertView = layoutInflater.inflate(layout.simple_test_layout_with_one_text_field, null);
        }
        return convertView;
    }
};

Looks like some parameters have to be set.

Could you please help me to avoid this problem?


UPD1

final ListAdapter adapter = new ArrayAdapter<BinLocationsComponent>(activity, R.layout.simple_list_item_multiple_choice, locationComponents) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LocationsViewHolder viewHolder;
        if ( convertView == null ) {
            convertView = layoutInflater.inflate(layout.rp_bin_locations_block_element, parent, false);
            viewHolder = new LocationsViewHolder();
            viewHolder.init(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (LocationsViewHolder) convertView.getTag();
        }
        final BinLocation binLocation = locationComponents.get(position).getBinLocation();
        if ( binLocation != null ) {
            viewHolder.provideValues(binLocation);
        }
        return convertView;
    }

    class LocationsViewHolder {
        TextView stationName;
        TextView rackName;
        TextView binName;
        TextView volume;
        NumberPicker numberOfItems;

        public void init(View view) {
            stationName = (TextView) view.findViewById(id.ro_station_name);
            rackName = (TextView) view.findViewById(id.ro_rack_name);
            binName = (TextView) view.findViewById(id.ro_bin_name);
            volume = (TextView) view.findViewById(id.ro_volume_value);
            numberOfItems = (NumberPicker) view.findViewById(id.ro_number_of_items);
        }

        public void provideValues(BinLocation location) {
            stationName.setText(location.getStationName());
            rackName.setText(location.getRackName());
            binName.setText(location.getBinName());
            volume.setText(valueOf(location.getVolume()));
        }

    }
};

and simple_list_item_multiple_choice it's android sdk's file:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
/>

1条回答
Anthone
2楼-- · 2019-09-11 04:19

Wow, its the first hurdle you face in your first ListView. Don't worry. First of all understand how ListView works, its important. Otherwise you can't master ListView. In simplest word: ListView creates few row Views and utilizes them for every row. So if you have have 50 elements in your ListView then may be only 10 times getView() was called with convertView being null. So in short if you don't update the state of a view inside getView() method for every position, it will really be a chaos. Also please use View Holder Pattern in your ListView. It will drastically increase scrolling and will utilize less resources. More on View Holder Pattern http://www.jmanzano.es/blog/?p=166

UPDATE:

I said

So in short if you don't update the state of a view inside getView() method for every position, it will really be a chaos.

 private SparseBooleanArray checkedPositions = new SparseBooleanArray();// class variable


listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
             if(checkedPositions.get(position)){
                 checkedPositions.put(position, false);
             }else{
                 checkedPositions.put(position, true);
             }
        }

    });

now in your getView()

((CheckedTextView)convertView.findViewById(R.id.checkedTextView)).setChecked(checkedPositions.get(position));

adjust this code according to your need. OR you can have a boolean variable in your BinLocation to maintain checked positions. But remember and understand the point of maintaining state inside getView().

查看更多
登录 后发表回答