I made an activity that has a list with my apps. Each item of the list has a checkbox.
private PackageManager gestorPacotes;
private List<Aplicacao> listaAplicacoes;
private ListView lista;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_aplicacoes);
carregaAplicacoes();
carregaListaAplicacoes();
}
private void carregaAplicacoes(){
gestorPacotes = getPackageManager();
listaAplicacoes = new ArrayList<Aplicacao>();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = gestorPacotes.queryIntentActivities(i, 0);
for(ResolveInfo ri:availableActivities) {
Aplicacao aplicacao = new Aplicacao();
aplicacao.nome = ri.loadLabel(gestorPacotes);
aplicacao.pacote = ri.activityInfo.packageName;
aplicacao.icon = ri.activityInfo.loadIcon(gestorPacotes);
listaAplicacoes.add(aplicacao);
}
}
private void carregaListaAplicacoes(){
lista = (ListView)findViewById(R.id.listaAplicacoes);
ArrayAdapter<Aplicacao> adapter = new ArrayAdapter<Aplicacao>(this, R.layout.list_item, listaAplicacoes) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}
ImageView appIcon = (ImageView) convertView.findViewById(R.id.item_app_icon);
appIcon.setImageDrawable(listaAplicacoes.get(position).icon);
final TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
appLabel.setText(listaAplicacoes.get(position).nome);
final TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
appName.setText(listaAplicacoes.get(position).pacote);
final CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.chk_box);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Toast.makeText(getApplicationContext(),appLabel.getText(),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
};
lista.setAdapter(adapter);
}
When I check one item of the list other elements are being checked automatically. What do I have to add to my code to this stop happening? Thanks.
You should probably use a RecyclerView for that kind of list, here's a good example on using RecyclerView in MultiChoice mode. https://www.bignerdranch.com/blog/recyclerview-part-2-choice-modes/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.