How to prevent a Checkbox from receiving click events?

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 is marked. But after a test implementation, I noticed that when I use the checkbox, the system does not run OnItemClick . If I remove the checkbox, the system runs OnItemClick . Do I need to do anything?

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 was implemented so that if the user selects an item from the list, the system displays a Toast indicating which item was selected. However, the system is not performing this action.

    
asked by anonymous 23.10.2017 / 04:01

1 answer

1

You can solve this by putting

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

in the checkbox that will be informative only by XML.

If you need someday that the checkbox has a click event too, then you have to use the ViewHolder pattern. I also recommend a read already and migrate to RecyclerView with ViewHolder, will solve many of the problems.

    
23.10.2017 / 13:41