我有一个带有数字的列表视图和一个带有每个数字的图像按钮。我想点击按钮点击行中的号码。我的getview方法是
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView = convertView;
ContactStockView sv = null;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(
R.layout.activity_row, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sv = new ContactStockView();
sv.name = (TextView) rowView.findViewById(R.id.textacti_row1);
sv.number = (TextView) rowView.findViewById(R.id.textacti_row2);
sv.btncall=(ImageButton)rowView.findViewById(R.id.imgbtn_call);
//sv.btncall.setOnClickListener((OnClickListener) activity);
rowView.setTag(sv);
//ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
/* */
} else {
sv = (ContactStockView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
ContactStock currentStock = (ContactStock) stocks.get(position);
sv.name.setText(currentStock.getName());
number=currentStock.getNumber();
sv.number.setText(number);
//ImageButton btn=(ImageButton)rowView.findViewById(R.id.imgbtn_call);
sv.btncall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText( activity, number, Toast.LENGTH_SHORT).show();
String phoneCallUri = "tel:"+number;
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
activity.startActivity(phoneCallIntent);
}
});
/* btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText( activity, "abc", Toast.LENGTH_SHORT).show();
/*}
});*/
// TODO Auto-generated method stub
return rowView;
}
protected static class ContactStockView {
protected TextView name;
protected TextView number;
protected ImageButton btncall;
}
我的日志猫叫声
答案 0 :(得分:1)
你应该在get view方法中声明字符串数字而不是在它之外。变化
number=currentStock.getNumber();
到
final String number=currentStock.getNumber();
并从外部getview方法
中删除字符串(即数字)答案 1 :(得分:0)
你无法通过这种方式找到问题。您必须 debug
getView
方法。
通过
做到这一点 1)双击外框左侧方法的第一行,看一个小点代表breakpoint
。
2)右键单击该项目,然后单击Debug as Android application。
3)使用按钮F6(在Eclipse中)开始移动步骤,直到找到您编码的特定行产生错误。
4)现在当你知道这条线时,更容易找出发生了什么。
答案 2 :(得分:0)
使用setTag将数字附加到视图,然后在单击侦听器中检索它。你也不需要每次都有一个新的监听器,在函数外定义一次......
sv.btncall.setTag(number);
然后在onclick听众......
new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = (String) v.getTag();
String phoneCallUri = "tel:"+number;
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
activity.startActivity(phoneCallIntent);
}
}