我有一个listview,其中包含两个textview,一个图像和一个按钮,我想在按钮点击时使用当前listview项目执行某些功能.但我在启动我的适配器时使用他的代码关闭我的应用程序
@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
}
};
Run Code Online (Sandbox Code Playgroud)
呼叫按钮代码
@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();
}
Run Code Online (Sandbox Code Playgroud)
我的adapeter课是
public ContactAdapter(Activity activity, List<ContactStock> objects) {
super(activity, R.layout.row, objects);
this.activity = activity;
this.stocks = objects;
}
Run Code Online (Sandbox Code Playgroud)
改变这个
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
Run Code Online (Sandbox Code Playgroud)
至
ImageButton btn=(ImageButton)rootView.findViewById(R.id.btn_call);
Run Code Online (Sandbox Code Playgroud)
因为你有
rowView = inflater.inflate(R.layout.row, null);
Run Code Online (Sandbox Code Playgroud)
findViewById在当前膨胀的布局中查找视图.您使用视图对象初始化视图.
编辑:
context.startActivity(phoneCallIntent);
Run Code Online (Sandbox Code Playgroud)
startActivity()是一种活动类的方法.您需要将上下文传递给适配器类的构造函数初始化它,然后像上面的语句一样使用它
通过
new CustomAdapter(MainActivtiy.this,..other params);
Run Code Online (Sandbox Code Playgroud)
然后
上下文背景; public CustomAdapter(Context contex,.. params){this.context = context; }
| 归档时间: |
|
| 查看次数: |
269 次 |
| 最近记录: |