带有 ViewHolder 模式的 setOnClickListener 内部适配器失败
setOnClickListener Inside Adapter with ViewHolder Pattern fail
我对 OnClickListener()
事件有疑问。
每个按钮视图中每个项目列表的数据或变量给出了不匹配的值。
此外,我通过打印一些日志来测试给出的位置。
根据项目行显示位置不匹配(可能是因为视图回收)
所以,下面是我的适配器实现 class,请帮我识别它。
请给我一个指导和适当的方法来解决这个问题。
MyAdapter.Class
private List<Result> results;
public ManifestAdapter(Context context, int view, List<Result> results) {
super(context, view, results);
this.results = results;
this.context = context;
sSave = context.getResources().getString(R.string.complete);
sCancel = context.getResources().getString(R.string.cancel);
sEditData = context.getResources().getString(R.string.edit_data);
colorShipping = context.getResources().getColor(R.color.status_shipping);
colorShippingComplete = context.getResources().getColor(R.color.status_shipping_complete);
colorShippingIncomplete = context.getResources().getColor(R.color.status_shipping_incomplete);
}
@Override
public Result getItem(int position) {
return results.get(position);
}
public View getView(final int position, View convertView, ViewGroup parent) {
int pos = position;
ViewHolder holder = null;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem_manifest, null);
holder = new ViewHolder();
holder.front = (RelativeLayout) convertView.findViewById(R.id.front);
holder.qrcode = (TextView) convertView.findViewById(R.id.qrcode);
holder.amount = (TextView) convertView.findViewById(R.id.amount);
holder.customer = (TextView) convertView.findViewById(R.id.customer);
holder.address = (TextView) convertView.findViewById(R.id.receiverAddress);
holder.btnTel = (Button) convertView.findViewById(R.id.receiverTel);
holder.btnMap = (Button) convertView.findViewById(R.id.district);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Result result = getItem(position);
.....
.....
.....
// setup call
if (result.getReceiverTel() != null) {
if (!result.getReceiverTel().equals("")) {
holder.btnTel.setText(result.getReceiverTel().trim());
holder.btnTel.setTag(position);
holder.btnTel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
String uri = "tel:" + getItem(pos).getReceiverTel().trim();
Intent iCall = new Intent(Intent.ACTION_DIAL);
iCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
iCall.setData(Uri.parse(uri));
context.startActivity(iCall);
}
});
}
}
return convertView;
}
private static class ViewHolder{
RelativeLayout front;
TextView qrcode;
TextView amount;
TextView customer;
TextView address;
Button btnTel;
Button btnMap;
int position;
}
请更改此代码。
// setup call
if (result.getReceiverTel() != null) {
if (!result.getReceiverTel().equals("")) {
holder.btnTel.setText(result.getReceiverTel().trim());
holder.btnTel.setTag(position);
holder.btnTel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
String uri = "tel:" + getItem(pos).getReceiverTel().trim();
Intent iCall = new Intent(Intent.ACTION_DIAL);
iCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
iCall.setData(Uri.parse(uri));
context.startActivity(iCall);
}
});
}
}
至
// setup call
holder.btnTel.setTag(position);
if (result.getReceiverTel() != null) {
if (!result.getReceiverTel().equals("")) {
holder.btnTel.setText(result.getReceiverTel().trim());
holder.btnTel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
String uri = "tel:" + getItem(pos).getReceiverTel().trim();
Intent iCall = new Intent(Intent.ACTION_DIAL);
iCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
iCall.setData(Uri.parse(uri));
context.startActivity(iCall);
}
});
}
}
如有任何疑问,请告知。
我对 OnClickListener()
事件有疑问。
每个按钮视图中每个项目列表的数据或变量给出了不匹配的值。
此外,我通过打印一些日志来测试给出的位置。
根据项目行显示位置不匹配(可能是因为视图回收)
所以,下面是我的适配器实现 class,请帮我识别它。
请给我一个指导和适当的方法来解决这个问题。
MyAdapter.Class
private List<Result> results;
public ManifestAdapter(Context context, int view, List<Result> results) {
super(context, view, results);
this.results = results;
this.context = context;
sSave = context.getResources().getString(R.string.complete);
sCancel = context.getResources().getString(R.string.cancel);
sEditData = context.getResources().getString(R.string.edit_data);
colorShipping = context.getResources().getColor(R.color.status_shipping);
colorShippingComplete = context.getResources().getColor(R.color.status_shipping_complete);
colorShippingIncomplete = context.getResources().getColor(R.color.status_shipping_incomplete);
}
@Override
public Result getItem(int position) {
return results.get(position);
}
public View getView(final int position, View convertView, ViewGroup parent) {
int pos = position;
ViewHolder holder = null;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem_manifest, null);
holder = new ViewHolder();
holder.front = (RelativeLayout) convertView.findViewById(R.id.front);
holder.qrcode = (TextView) convertView.findViewById(R.id.qrcode);
holder.amount = (TextView) convertView.findViewById(R.id.amount);
holder.customer = (TextView) convertView.findViewById(R.id.customer);
holder.address = (TextView) convertView.findViewById(R.id.receiverAddress);
holder.btnTel = (Button) convertView.findViewById(R.id.receiverTel);
holder.btnMap = (Button) convertView.findViewById(R.id.district);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Result result = getItem(position);
.....
.....
.....
// setup call
if (result.getReceiverTel() != null) {
if (!result.getReceiverTel().equals("")) {
holder.btnTel.setText(result.getReceiverTel().trim());
holder.btnTel.setTag(position);
holder.btnTel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
String uri = "tel:" + getItem(pos).getReceiverTel().trim();
Intent iCall = new Intent(Intent.ACTION_DIAL);
iCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
iCall.setData(Uri.parse(uri));
context.startActivity(iCall);
}
});
}
}
return convertView;
}
private static class ViewHolder{
RelativeLayout front;
TextView qrcode;
TextView amount;
TextView customer;
TextView address;
Button btnTel;
Button btnMap;
int position;
}
请更改此代码。
// setup call
if (result.getReceiverTel() != null) {
if (!result.getReceiverTel().equals("")) {
holder.btnTel.setText(result.getReceiverTel().trim());
holder.btnTel.setTag(position);
holder.btnTel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
String uri = "tel:" + getItem(pos).getReceiverTel().trim();
Intent iCall = new Intent(Intent.ACTION_DIAL);
iCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
iCall.setData(Uri.parse(uri));
context.startActivity(iCall);
}
});
}
}
至
// setup call
holder.btnTel.setTag(position);
if (result.getReceiverTel() != null) {
if (!result.getReceiverTel().equals("")) {
holder.btnTel.setText(result.getReceiverTel().trim());
holder.btnTel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
String uri = "tel:" + getItem(pos).getReceiverTel().trim();
Intent iCall = new Intent(Intent.ACTION_DIAL);
iCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
iCall.setData(Uri.parse(uri));
context.startActivity(iCall);
}
});
}
}
如有任何疑问,请告知。