I have a listview which contains two textviews, an image and a button I want to do some function on button click with the current listview item. But I i use his code my app close while starting my adapter is
@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
}
};
call button code
@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();
}
my adapeter class is
public ContactAdapter(Activity activity, List<ContactStock> objects) {
super(activity, R.layout.row, objects);
this.activity = activity;
this.stocks = objects;
}
Change this
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
to
ImageButton btn=(ImageButton)rootView.findViewById(R.id.btn_call);
coz you have
rowView = inflater.inflate(R.layout.row, null);
findViewById
looks for a view in the current inflated layout. You use the view object to initialize view.
Edit:
context.startActivity(phoneCallIntent);
startActivity()
is a method of activity class. You need to pass the context to the constructor of adapter class initialize it and then use it like above statement
To pass
new CustomAdapter(MainActivtiy.this,..other params);
Then
Context context; public CustomAdapter(Context contex,..params) { this.context =context; }
You should replace this
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
With
ImageButton btn=(ImageButton)rowView.findViewById(R.id.btn_call);
Becoz your convertView==null
at this position
还可以使用rowView
而不是convertView
将ImageButton初始化为:
ImageButton btn=(ImageButton)rowView.findViewById(R.id.btn_call);
change this line.....
ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
to this line..
ImageButton btn=(ImageButton)rowView.findViewById(R.id.btn_call);
in getview..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.