所以...我在使用CustomAdapter时遇到了一个奇怪的问题,这是我的代码:
public class QuilometragemCustomAdapter extends BaseAdapter {
Context context;
List<Quilometragem> quilometragens;
Quilometragem km;
public QuilometragemCustomAdapter(Context context, List<Quilometragem> list) {
this.context = context;
this.quilometragens = list;
}
@Override
public int getCount() {
return quilometragens.size();
}
@Override
public Quilometragem getItem(int position) {
return quilometragens.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
int color = Color.argb(255,41,36,33);
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.veiculoTxt = (TextView) convertView.findViewById(R.id.veiculoTxt);
holder.destinoTxt = (TextView) convertView.findViewById(R.id.dmTxt);
holder.imageB = (ImageButton) convertView.findViewById(R.id.editButton);
holder.imageB2 = (ImageButton) convertView.findViewById(R.id.deleteButton);
TextView txtV = (TextView) convertView.findViewById(R.id.text2);
TextView txtD = (TextView) convertView.findViewById(R.id.text3);
txtV.setText("\nVeiculo:\n");
txtD.setText("\nDestino:\n");
holder.imageB.setBackgroundResource(R.drawable.ic_action_edit);
holder.imageB2.setBackgroundResource(R.drawable.ic_action_cancel);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.nkm =(TextView) convertView.findViewById(R.id.text1);
holder.nkm.setText("\n" + quilometragens.get(position).getId()+ "\n");
String veiculoTxt = quilometragens.get(position).getVeiculo();
holder.veiculoTxt.setText("\n\n" + veiculoTxt + " \n");
holder.veiculoTxt.setTextColor(color);
String destinoTxt = quilometragens.get(position).getDestino();
holder.destinoTxt.setText("\n" + destinoTxt + "\n");
holder.destinoTxt.setTextColor(color);
return convertView;
}
private class ViewHolder {
private TextView veiculoTxt;
private TextView destinoTxt;
private ImageButton imageB;
private ImageButton imageB2;
private TextView nkm;
}
}
我没有使用列表,我只为行创建了一个布局,所以我可以添加它,因为我在sqlite数据库中插入数据,但它仍然存在同样的问题。
问题是,我的布局只打印最后一个位置,我调试了一千次,我的“位置”始终为0,并且随着列表中的项目不断增加。
如果列表中有3个项目,例如:ABC 123 QWE 321 IOP 456
它总是打印“IOP 456”它就像其他2个不在那里,我尝试用一个但是结果更糟,我尝试用do()同时,它变得更糟。我认为问题出在View上,但我不确定。
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个......
{
static class ViewHolder {
private TextView veiculoTxt;
private TextView destinoTxt;
private ImageButton imageB;
private ImageButton imageB2;
private TextView nkm;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
int color = Color.argb(255,41,36,33);
ViewHolder holder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row, null);
}
holder = new ViewHolder();
holder.veiculoTxt = (TextView) convertView.findViewById(R.id.veiculoTxt);
holder.destinoTxt = (TextView) convertView.findViewById(R.id.dmTxt);
holder.imageB = (ImageButton) convertView.findViewById(R.id.editButton);
holder.imageB2 = (ImageButton) convertView.findViewById(R.id.deleteButton);
TextView txtV = (TextView) convertView.findViewById(R.id.text2);
TextView txtD = (TextView) convertView.findViewById(R.id.text3);
txtV.setText("\nVeiculo:\n");
txtD.setText("\nDestino:\n");
holder.imageB.setBackgroundResource(R.drawable.ic_action_edit);
holder.imageB2.setBackgroundResource(R.drawable.ic_action_cancel);
holder.nkm =(TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
holder.nkm.setText("\n" + quilometragens.get(position).getId()+ "\n");
String veiculoTxt = quilometragens.get(position).getVeiculo();
holder.veiculoTxt.setText("\n\n" + veiculoTxt + " \n");
holder.veiculoTxt.setTextColor(color);
String destinoTxt = quilometragens.get(position).getDestino();
holder.destinoTxt.setText("\n" + destinoTxt + "\n");
holder.destinoTxt.setTextColor(color);
return convertView;
}
}