我有一个奇怪的问题让我发疯。在我的 android 应用程序中,我定制了我自己的从 ArrayAdapter 扩展的适配器。我添加适配器的 ListView 的项目可以是标记文本(不可编辑)、可编辑文本或微调器。疯狂的是:当我滚动 ListView 时,有两个问题:
(1)微调器项目中显示的(选定的)值有时会发生变化,尽管我只滚动了!!当我单击微调器时,仍然显示旧的选定值(应该由微调器显示的那个)(2)当我滚动时,ListViewItems的顺序会发生变化!
=>但是适配器中的数据没有改变(数据本身和顺序都没有) - 所以它一定是视图本身的问题?!也许android在后台缓存并且没有尽快刷新ListViewItems或类似的东西?!
有人可以帮我吗?
多谢!
好的,我找到了一个不是很好的解决方案,但它确实有效。我只是不再使用 convertView 了,尽管这在内存和性能方面不是最理想的。在我的情况下应该没问题,因为我的 ListView 的最大项目数量是 15。这是我的适配器类:
public class FinAdapter extends ArrayAdapter<Param>{
public Param[] params;
private boolean merkzettel;
protected EditText pv;
public FinAdapter(Context context, int textViewResourceId, Param[] items, boolean merkzettel) {
super(context, textViewResourceId, items);
this.params = items;
this.merkzettel = merkzettel;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Param p = params[position];
if(p != null){
if(merkzettel){
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.merk_det_item, null);
}
TextView tvl = (TextView) convertView.findViewById(R.id.paramM);
TextView edl = (TextView) convertView.findViewById(R.id.param_valueM);
TextView pal = (TextView) convertView.findViewById(R.id.param_unitM);
if (tvl != null) {
tvl.setText(p.getName());
}
if(pal != null){
pal.setText(p.getUnit());
}
if(edl != null){
edl.setText(p.getDefData());
}
}
else{
if(p.isSelect()){
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.fin_prod_list_item_select, null);
}
TextView tvs = (TextView) convertView.findViewById(R.id.paramS);
Spinner sp = (Spinner) convertView.findViewById(R.id.spinner_kalk);
TextView paU = (TextView) convertView.findViewById(R.id.param_unitS);
if (tvs != null) {
tvs.setText(p.getName());
}
if(paU != null){
paU.setText(p.getUnit());
}
if(sp != null){
String[] values = new String[p.getData().size()];
for(int i=0; i<values.length; i++){
values[i] = p.getData().get(i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setSelection(p.getData().indexOf(p.getDefData()));
sp.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent,
View convertView, int pos, long id) {
p.setDefData(p.getData().get(pos));
p.setChanged(true);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
else if(p.isEdit()){
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.fin_prod_list_item_edit, null);
}
TextView pa = (TextView) convertView.findViewById(R.id.param);
pv = (EditText) convertView.findViewById(R.id.param_value);
TextView paE = (TextView) convertView.findViewById(R.id.param_unit);
if (pa != null) {
pa.setText(p.getName());
}
if(paE != null){
paE.setText(p.getUnit());
}
if(pv != null){
pv.setText(p.getDefData());
pv.setOnEditorActionListener(new OnEditorActionListener(){
public boolean onEditorAction(TextView convertView, int actionId,
KeyEvent event) {
// TODO Auto-generated method stub
p.setDefData(pv.getText().toString());
p.setChanged(true);
return false;
}
});
}
}
else if(p.isLabel()){
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.fin_prod_list_item_label, null);
}
TextView tvl = (TextView) convertView.findViewById(R.id.paramL);
TextView edl = (TextView) convertView.findViewById(R.id.param_valueL);
TextView pal = (TextView) convertView.findViewById(R.id.param_unitL);
if (tvl != null) {
tvl.setText(p.getName());
}
if(pal != null){
pal.setText(p.getUnit());
}
if(edl != null){
edl.setText(p.getDefData());
}
}}
}
return convertView;
}
}