Android 调用listview中的按钮单击时出错
我有一个带有数字的列表视图和一个带有每个数字的图像buton。我想按一下该行的号码打电话。我的getview方法是Android 调用listview中的按钮单击时出错,android,listview,onclicklistener,Android,Listview,Onclicklistener,我有一个带有数字的列表视图和一个带有每个数字的图像buton。我想按一下该行的号码打电话。我的getview方法是 @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View rowView = convertView; ContactStockView sv = null; if
@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;
}
我的待命日志是
那样你就找不到问题。您必须
调试您的getView
方法
通过
1) 双击外部框架左侧方法的第一行以查看一个小点,该点表示一个断点
2) 在项目上单击鼠标右键,然后单击“作为Android应用程序调试”
3) 使用按钮F6(在Eclipse中)开始移动步骤,直到找到代码生成错误的特定行
4) 现在,当你知道这条线的时候,就更容易知道发生了什么 使用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);
}
}
您应该在get view方法中声明字符串number,而不是在它之外。改变
number=currentStock.getNumber();
到
并从getview方法外部删除字符串(即,数字)logcat posted,应用程序在单击call按钮时随机选择数字请张贴日志的红色区域,这不会显示任何错误感谢回答abd,vikas rana回答完成我的工作欢迎。如果你觉得我的答案很有用,请投赞成票。谢谢:)
final String number=currentStock.getNumber();