在我的应用程序中,我有一个ListView
and ArrayAdapter
。当我在其中选择某个元素时,ListView
我还可以看到更多选定的元素。
此外,当我在(选择后)滚动元素时,ListView
我的选择消失并且元素被随机选择。
适配器代码:
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;
}
};
看起来需要设置一些参数。
你能帮我避免这个问题吗?
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()));
}
}
};
和 simple_list_item_multiple_choice 它是 android sdk 的文件:
<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"
/>