I have created a list with checkbox disabled, where the purpose is that when the user clicks on an item in the list, a new screen opens and the check is checked, but I can not make the check automatically marked.
Class ListActivity
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();
}
private void CriarLista(){
Itens = new ArrayList<ItemListView>();
ItemListView Item1 = new ItemListView("Frase 1", 0, false);
ItemListView Item2 = new ItemListView("Frase 2", 1, false);
ItemListView Item3 = new ItemListView("Frase 3", 2, false);
ItemListView Item4 = new ItemListView("Frase 4", 3, false);
ItemListView Item5 = new ItemListView("Frase 5", 4, false);
ItemListView Item6 = new ItemListView("Frase 6", 5, false);
ItemListView Item7 = new ItemListView("Frase 7", 6, false);
ItemListView Item8 = new ItemListView("Frase 8", 7, false);
ItemListView Item9 = new ItemListView("Frase 9", 8, false);
ItemListView Item10 = new ItemListView("Frase 10", 9, false);
Itens.add(Item1);
Itens.add(Item2);
Itens.add(Item3);
Itens.add(Item4);
Itens.add(Item5);
Itens.add(Item6);
Itens.add(Item7);
Itens.add(Item8);
Itens.add(Item9);
Itens.add(Item10);
adapterListView = new AdapterListView(this, Itens);
listView.setAdapter(adapterListView);
listView.setCacheColorHint(Color.TRANSPARENT);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
ItemListView item = adapterListView.getItem(arg2);
item.setCheck(true);
Intent EnviarDados = new Intent(ListaActivity.this, FraseActivity.class);
int id = item.getId();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
EnviarDados.putExtras(bundle);
startActivity(EnviarDados);
}
}
Class ItemListView
public class ItemListView {
private String Texto;
private int Id;
private boolean Check;
public ItemListView(String texto, int id, boolean check){
this.Texto = texto;
this.Id = id;
this.Check = check;
}
public String getTexto(){
return Texto;
}
public void setTexto(String texto){
Texto = texto;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public boolean getCheck() {
return Check;
}
public void setCheck(boolean check) {
Check = check;
}
}
Class 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(item.getCheck());
return view;
}
}