我有一个列表视图,其中包含两个文本视图,一个图像和一个按钮,我希望在按钮单击时使用当前列表视图项执行某些功能。但我在启动我的适配器时使用他的代码我的应用程序关闭
@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.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.textrow1);
sv.number = (TextView) rowView.findViewById(R.id.textrow2);
//ImageButton buy=(ImageButton)convertView.findViewById(R.id.btn_call);
// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sv);
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText( activity, "abc", Toast.LENGTH_SHORT).show();
}
});
} 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());
sv.number.setText(currentStock.getNumber());
// TODO Auto-generated method stub
return rowView;
}
protected static class ContactStockView {
protected TextView name;
protected TextView number;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
stocks.clear();
if (charText.length() == 0) {
stocks.addAll(arraylist);
} else {
for (ContactStock cs : arraylist) {
if (cs.getName().contains(charText)) {
stocks.add(cs);
}
}
}
notifyDataSetChanged();
}
private OnClickListener callClickListner=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
通话按钮代码
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneCallUri = "tel:" + phonenumber;
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
//Toast.makeText( activity, "abc", Toast.LENGTH_SHORT).show();
}
我的adapeter课程是
public ContactAdapter(Activity activity, List<ContactStock> objects) {
super(activity, R.layout.row, objects);
this.activity = activity;
this.stocks = objects;
}
答案 0 :(得分:5)
更改此
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
到
ImageButton btn=(ImageButton)rootView.findViewById(R.id.btn_call);
因为你有
rowView = inflater.inflate(R.layout.row, null);
findViewById
在当前膨胀的布局中查找视图。您可以使用视图对象初始化视图。
编辑:
context.startActivity(phoneCallIntent);
startActivity()
是一种活动类方法。您需要将上下文传递给适配器类的构造函数初始化它,然后像上面的语句一样使用它
传递
new CustomAdapter(MainActivtiy.this,..other params);
然后
上下文上下文; public CustomAdapter(Context contex,.. params) { this.context = context; }
答案 1 :(得分:2)
你应该替换这个
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
使用
ImageButton btn=(ImageButton)rowView.findViewById(R.id.btn_call);
在这个位置becoz你的convertView==null
答案 2 :(得分:2)
还可以使用rowView
代替convertView
将ImageButton初始化为:
ImageButton btn=(ImageButton)rowView.findViewById(R.id.btn_call);
答案 3 :(得分:1)
更改此行.....
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
到这一行..
ImageButton btn=(ImageButton)rowView.findViewById(R.id.btn_call);
在getview中