dans mon application j'utilise le bouton d'image dans la vue liste. J'écris l'action pour chaque bouton d'image. mais je reçois java.lang.NoSuchMethodException: exception onClick. Je ne connais pas la raison de l'erreur s'il vous plaît aidez-moi. comment écrire une action pour les boutons d'image. Merci d'avance.java.lang.NoSuchMethodException: onClick dans la vue de liste sur android
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);
}
}
en activité InventoryAdapter:
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;
}
}
}
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" />
pas 100% sûr, mais je pense que la méthode onClick() est recherchée dans l'activité, pas dans l'adaptateur – Gryphius