我正面临这个问题并且已经阅读了很多问题/答案,但无法解决。
我有一个带有微调器的ListView(用户必须为该行中的产品选择一个数量编号),但是当有多个项目而不是视图并且在微调器中选择一个数字后滚动ListView时,这个数字会消失,并且默认再次显示。
我在适配器内为ListView本身创建了一个适配器:
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(context, R.layout.layout_spinner_quanitdade, quantities) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(18);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setGravity(Gravity.END);
return v;
}
};
holder.spinnerQuantidade.setAdapter(spinnerAdapter);
holder.spinnerQuantidade.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int spinnerPosition, long id) {
String newQuantity = quantities[spinnerPosition];
item.setQuantidade(newQuantity);
Spinner theSpinner = (Spinner) parent;
theSpinner.setSelection(spinnerPosition);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
在尝试在Spinner中选择一个值之前(查看带有IMEI一词的行):
在微调器中为行选择一个值:
向上滚动然后向下滚动(值更改回1,而不是我选择的10):
更新:更改为尝试保留并设置微调器选择,但不起作用:
public View getView(final int modelPosition, View view, ViewGroup parent) {
ViewHolder holder = null;
final Produto item = getItem(modelPosition);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = mInflater.inflate(R.layout.item_view_produto, parent, false);
holder = new ViewHolder();
holder.textViewCodigo = (TextView) view.findViewById(R.id.textViewCodigo);
holder.textViewNome = (TextView) view.findViewById(R.id.textViewNome);
holder.spinnerQuantidade = (Spinner) view.findViewById(R.id.spinnerQuantidade);
holder.spinnerQuantidade.setTag(modelPosition);
CheckBox chk = (CheckBox) view.findViewById(R.id.checkBox);
holder.checkBox = chk;
holder.checkBox.setTag(modelPosition);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (modelPosition % 2 == 0) {
view.setBackgroundResource(R.drawable.cor_zebra2);
} else {
view.setBackgroundResource(R.drawable.cor_zebra1);
}
holder.textViewCodigo.setText(item.getCodigo());
holder.textViewNome.setText(item.getDescricao().trim());
// checking and settion spinner selection - DID NOT WORK
if (mapRowSpinnerPos.containsKey(modelPosition)) {
holder.spinnerQuantidade.setSelection(mapRowSpinnerPos.get(modelPosition));
}
if (this.showChecks) {
holder.spinnerQuantidade.setVisibility(View.GONE);
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(item.isChecked());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.setChecked(isChecked);
if (isChecked) {
selectedList.add(item);
} else {
selectedList.remove(item);
}
}
});
} else {
// situation 2: show spinners
holder.checkBox.setVisibility(View.GONE);
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(context, R.layout.layout_spinner_quanitdade, quantities) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(18);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setGravity(Gravity.END);
return v;
}
};
holder.spinnerQuantidade.setAdapter(spinnerAdapter);
holder.spinnerQuantidade.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int spinnerPosition, long id) {
// int thePosition = (Integer) parent.getTag();
mapRowSpinnerPos.put(modelPosition, spinnerPosition);
String newQuantity = quantities[spinnerPosition];
item.setQuantidade(newQuantity);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
return view;
}
答案 0 :(得分:7)
设置适配器后移动以下代码。设置适配器会将其设置为第一个位置。
<div class="map-ctn init"></div>
<div class="list-ctn init"></div>
答案 1 :(得分:1)
ListView
重复使用其观看次数,因此只会填充当前显示的观看次数。
因此,当列表项重新进入屏幕时,它会重新填充。
为了保持每个微调器的状态,你的适配器应该有一个字段来保存每个项目的选定值,这个值将从微调器选择中更新,然后,在getView方法中,你将检查来自适配器的选择并设置微调器到选定的项目。