I'm creating an application that uses cardview in my list. And I'm having trouble adding the menu in the cardview, as shown below:
Mycardviewclass:
classCustomAdapterextendsBaseAdapter{privateList<Palheta>palhetas;publicCustomAdapter(List<Palheta>palhetas){this.palhetas=palhetas;}@OverridepublicintgetCount(){returnpalhetas.size();}@OverridepublicObjectgetItem(inti){returnpalhetas.get(i);}@OverridepubliclonggetItemId(inti){returni;}@OverridepublicViewgetView(finalinti,Viewview,ViewGroupviewGroup){view=getLayoutInflater().inflate(R.layout.card_view_palheta,null);TextViewtv_nome=(TextView)view.findViewById(R.id.tv_nome);simpleSwitch=(Switch)view.findViewById(R.id.simpleSwitch);ImageViewimgCheck=view.findViewById(R.id.check);TextViewtextoCheck=view.findViewById(R.id.nomeCheck);if(palhetas.get(i).getSituacao().equals("COLETADA")){
simpleSwitch.setVisibility(View.INVISIBLE);
simpleSwitch.setEnabled(false );
imgCheck.setVisibility(View.VISIBLE );
textoCheck.setVisibility(View.VISIBLE);
}
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imButton);
try {
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(getApplicationContext(), view);
popup.getMenuInflater().inflate(R.menu.main_palheta, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), "Você clicou em : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
tv_nome.setText(palhetas.get(i).getCodigo());
TextView tv_endereco = (TextView) view.findViewById(R.id.tv_endereco);
tv_endereco.setText("Endereço: " + palhetas.get(i).getEndereco().getRua());
TextView tv_bairro = (TextView) view.findViewById(R.id.tv_bairro);
tv_bairro.setText("Bairro: " + palhetas.get(i).getEndereco().getBairro());
idPalheta = palhetas.get(i).getId();
simpleSwitch.setTextOn("Sim"); // displayed text of the Switch whenever it is in checked or on state
simpleSwitch.setTextOff("Não");
simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked) {
alertDialog();
} else {
Toast.makeText( getApplicationContext(), "NAO", Toast.LENGTH_LONG ).show();
}
}
});
return view;
}
}
Error giving:
java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.ImageButton
What am I missing?