in my app i use image button in list view. i write action for each image button. but i get java.lang.NoSuchMethodException: onClick exception. i do not know the reason for error please help me. how to write action for the image buttons . thanks in advance.
public class InventoryListActivity extends ListActivity {
private InventoryAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory_list);
adapter = new InventoryAdapter(this);
setListAdapter(adapter);
}
}
in InventoryAdapter activity:
public class InventoryAdapter extends BaseAdapter implements Observer,OnClickListener {
public InventoryAdapter(Context ctx) {
context = ctx;
inventory = IAPManager.shared().getInventory();
inventory.addObserver(this);
inventory.load();
}
public void update(Observable o, Object arg) {
switch ( ((Inventory)arg).getStatus() ) {
case LOADED:
this.notifyDataSetChanged();
break;
default:
break;
}
}
@Override
public int getCount() {
return inventory.size(Inventory.FilterType.ALL);
}
@Override
public Object getItem(int position) {
return inventory.getProducts(Inventory.FilterType.ALL).get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Product product = (Product) getItem(position);
View view;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.inventory_list_item, null);
}
else {
view = convertView;
}
play = (ImageButton)findViewById(R.id.imageButton2);
detail = (ImageButton)findViewById(R.id.imageButton3);
buy = (ImageButton)findViewById(R.id.imageButton2);
play.setTag(position);
detail.setTag(position);
buy.setTag(position);
titleView = (TextView) view.findViewById(R.id.product_title);
titleView.setText(product.getTitle());
iconView = (ImageView) view.findViewById(R.id.product_icon);
String iconURL = product.getIconURLString();
if(AsyncImageLoader.contains(iconURL)) {
iconView.setImageDrawable(AsyncImageLoader.get(iconURL));
}
else {
iconView.setImageDrawable(null);
new AsyncImageLoader(product.getIconURLString(), new AsyncImageLoader.Delegate(){
public void imageLoaded(String urlString, Drawable imageDrawable) {
notifyDataSetChanged();
}
});
}
descriptionView = (TextView) view.findViewById(R.id.product_description);
descriptionView.setText(product.getDescription());
return view;
}
private ImageButton findViewById(int imagebutton2) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onClick(View v) {
Log.e("onclick","onclick");
Integer position = (Integer) v.getTag();
switch(v.getId()){
case R.id.imageButton1:
Log.e("Buy","buy position"+position);
break;
case R.id.imageButton2:
Log.e("play","play position"+position);
break;
case R.id.imageButton3:
Log.e("detail","detail position"+position);
break;
}
}
}
in xml:
<ImageButton android:background="@drawable/play_btn" android:focusable="false" android:onClick="onClick"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/imageButton2" android:layout_weight="0.5" />
I think the problem is, that you’re trying to wire up the onClick listener via xml but implement it in your adapter. I think android simply doesn’t recognize the onClick listener or doesn’t know that is should be used for the ImageButton. Try to implement and add the OnClickListener dynamically in the getView() method.
UPDATE
Check the first if statement of the getView() method. There the OnClickListener will be set for the 3 buttons. As your adapter implements the OnClickListener interface you pass the adpater to the method. You also have to remove the
android:onClick="onClick"attribute from your ImageButtons in the layout xml file.