我创建了一个 自定义适配器 来管理每个 ListView 项目中的一些按钮。如果我向下滚动列表,然后单击按钮,则适配器的位置会错误。
这是我的适配器:
public DoubleListAdapter(Context context, ArrayList<DoubleListItems> items,
String Tag) {
super(context, 0, items);
this.Tag = Tag;
this.context = context;
this.items = items;
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final int Position = position;
if (convertView == null) {
convertView = vi.inflate(R.layout.double_list_item, null);
holder = new ViewHolder();
final DoubleListItems i = (DoubleListItems) items.get(position);
holder.item = i;
holder.Icon = (ImageView) convertView
.findViewById(R.id.IconaDoubleList);
holder.Title = (TextView) convertView
.findViewById(R.id.TitoloDoubleList);
holder.SecondLineDesc = (TextView) convertView
.findViewById(R.id.SecondaLineaDesc);
holder.SecondLineValue = (TextView) convertView
.findViewById(R.id.SecondaLineaValue);
holder.ThirdLineDesc = (TextView) convertView
.findViewById(R.id.TerzaLineaDesc);
holder.ThirdLineValue = (TextView) convertView
.findViewById(R.id.TerzaLineaValue);
holder.Delete = (Button) convertView
.findViewById(R.id.ButtonDeleteItem);
holder.Set = (Button) convertView.findViewById(R.id.ButtonSetiItem);
holder.Delete.setTag(holder);
holder.Delete.setOnClickListener(new OnClickListener() {
holder.Set.setTag(holder);
holder.Set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Log.e("DOUBLELIST ADAPTER ", "Position: " + Position);
String Code = i.getKey();
// String Code = holder.item.getKey();
Intent intent;
Costanti.curLocale = Costanti.LocaliLavorati.Item("L"
+ Code);
intent = new Intent(context, IncassiTab.class);
if (Costanti.curLocale != null) {
context.startActivity(intent);
} else {
Toast.makeText(context, "Locale nullo",
Toast.LENGTH_SHORT);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final DoubleListItems i = holder.item;
if (i != null) {
holder.Set.setVisibility(View.GONE);
holder.Delete.setVisibility(View.GONE);
holder.Icon.setImageResource(i.getIcon());
holder.Title.setText(i.getTitle());
holder.SecondLineDesc.setText(i.getSecondLineDesc());
if (i.getType().equals("N")) {
holder.SecondLineDesc.setTextColor(context.getResources()
.getColor(R.color.grigio));
}
if (!i.getSecondLineValue().equals("")) {
holder.SecondLineValue.setVisibility(View.VISIBLE);
holder.SecondLineDesc.setTextColor(context.getResources()
.getColor(R.color.orange));
holder.Icon.setImageResource(i.getIcon());
holder.Title.setText(i.getTitle());
holder.SecondLineDesc.setText(i.getSecondLineDesc());
holder.SecondLineValue.setText(i.getSecondLineValue());
if (!i.getThirdLineDesc().equals("")) {
holder.Set.setVisibility(View.VISIBLE);
holder.Delete.setVisibility(View.VISIBLE);
holder.ThirdLineDesc.setVisibility(View.VISIBLE);
holder.ThirdLineValue.setVisibility(View.VISIBLE);
holder.ThirdLineDesc.setText(i.getThirdLineDesc());
holder.ThirdLineValue.setText(i.getThirdLineValue());
holder.Delete.setVisibility(View.VISIBLE);
holder.Set.setVisibility(View.VISIBLE);
} else {
holder.ThirdLineDesc.setVisibility(View.GONE);
holder.ThirdLineDesc.setVisibility(View.GONE);
}
} else {
holder.SecondLineValue.setVisibility(View.GONE);
holder.ThirdLineDesc.setVisibility(View.GONE);
holder.ThirdLineValue.setVisibility(View.GONE);
holder.Set.setVisibility(View.GONE);
holder.Delete.setVisibility(View.GONE);
}
}
return convertView;
}
private class ViewHolder {
public ViewHolder() {
}
protected DoubleListItems item;
protected Button Delete;
protected Button Set;
protected ImageView Icon;
protected TextView Title;
protected TextView SecondLineDesc;
protected TextView SecondLineValue;
protected TextView ThirdLineDesc;
protected TextView ThirdLineValue;
}
这是解决方案:
if (convertView == null) {
Your code
}else{
Your code
}
holder.Set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Your code
}
}