1
I’m creating an application that I use cardview in my list. And I’m having difficulty adding the menu in cardview, as image below:
My class of cardview:
class CustomAdapter extends BaseAdapter{
private List<Palheta> palhetas;
public CustomAdapter(List<Palheta> palhetas) {
this.palhetas = palhetas;
}
@Override
public int getCount() {
return palhetas.size();
}
@Override
public Object getItem(int i) {
return palhetas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.card_view_palheta, null);
TextView tv_nome = (TextView) view.findViewById(R.id.tv_nome);
simpleSwitch = (Switch) view.findViewById(R.id.simpleSwitch);
ImageView imgCheck = view.findViewById(R.id.check);
TextView textoCheck = 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;
}
}
Mistake you’re making:
java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.ImageButton
What am I missing?
The view
imButton
must be the typeImageView
, otherwise you will not be able to convert from one type to another. Or you can replaceImageButton imageButton = (ImageButton) view.findViewById(R.id.imButton);
forAppCompatButton imageButton = (AppCompatButton) view.findViewById(R.id.imButton);
– Valdeir Psr
Now I get the error: Process: com.diego.agentesendemias, PID: 12020 android.view.Inflateexception: Binary XML file line #0: Failed to resolve attribute at index 6: Typedvalue{t=0x1c/d=0xff009688 a=1 r=0x10600d3} at android.view.Layoutinflater.inflate(Layoutinflater.java:543) at android.view.Layoutinflater.inflate(Layoutinflater.java:427)
– Carlos Diego