How to prevent a Checkbox from receiving click events?

Asked

Viewed 54 times

1

I want to create a list where each item has a purely informative checkbox, so that when the user accesses this item, the checkbox will be marked. But after a test implementation, I noticed that when I use the checkbox, the system does not run the OnItemClick. If I remove the checkbox, the system executes the OnItemClick. I need to do something?

My List:

public class ListaActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

private ListView listView;

private AdapterListView adapterListView;

private ArrayList<ItemListView> Itens;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista);

    listView = (ListView) findViewById(R.id.lstLista);

    listView.setOnItemClickListener(this);

    CriarLista();
}

// Adiciona item por item da lista e o que se quer nela
private void CriarLista(){

    Itens = new ArrayList<ItemListView>();

    ItemListView Item1 = new ItemListView("Frase 1", 0);
    ItemListView Item2 = new ItemListView("Frase 2", 0);
    ItemListView Item3 = new ItemListView("Frase 3", 0);
    ItemListView Item4 = new ItemListView("Frase 4", 0);
    ItemListView Item5 = new ItemListView("Frase 5!", 0);

    Itens.add(Item1);
    Itens.add(Item2);
    Itens.add(Item3);
    Itens.add(Item4);
    Itens.add(Item5);

    adapterListView = new AdapterListView(this, Itens);

    listView.setAdapter(adapterListView);

    listView.setCacheColorHint(Color.TRANSPARENT);

}

// O que deve acontecer quando clicar em um item
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
    ItemListView item = adapterListView.getItem(arg2);
    Toast.makeText(this, "Item Selecionado: " + item.getTexto(), Toast.LENGTH_LONG).show();
}

My itemListView:

public class ItemListView {
private String Texto;
private int Check; //0- Desabilitado 1-Habilitado

public ItemListView(String texto, int check){
    this.Texto = texto;
    this.Check = check;
}

public String getTexto(){
    return Texto;
}

public void setTexto(String texto){
    Texto = texto;
}

public int getCheck(){
    return Check;
}

public void setCheck(int check){
    Check = check;
}

And the Adapterlistview:

public class AdapterListView extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<ItemListView> Itens;

public AdapterListView(Context context, ArrayList<ItemListView> Itens){
    this.Itens = Itens;
    mInflater = LayoutInflater.from(context);
}

public int getCount(){
    return Itens.size();
}

public ItemListView getItem(int position){
    return Itens.get(position);
}

public long getItemId(int position){
    return position;
}

public View getView(int position, View view, ViewGroup parent){
    ItemListView item = Itens.get(position);
    view = mInflater.inflate(R.layout.item_listview, null);

    ((TextView) view.findViewById(R.id.txtItem)).setText(item.getTexto());
    ((CheckBox) view.findViewById(R.id.chkItem)).setChecked(false);
    return view;
}

Note that it has been implemented so that if the user selects an item from the list, the system presents a Toast indicating which item was selected. However, the system is not performing this action

1 answer

1


It is possible to solve this by putting

android:focusable="false"
android:focusableInTouchMode="false" 

in the checkbox that will only be informative by XML.

If you ever need the checkbox to have a click event, then you have to use the Viewholder pattern. I also recommend a read now and migrate to Recyclerview with Viewholder, will solve many of the problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.