我制作了一个包含我的应用列表的活动。列表中的每个项目都有一个复选框。
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);
}
当我检查列表中的一个项目时,会自动检查其他元素。我有什么要添加到我的代码中才能发生这种情况? 感谢。
答案 0 :(得分:0)
您应该将RecyclerView用于此类列表,这是在MultiChoice模式下使用RecyclerView的一个很好的示例。https://www.bignerdranch.com/blog/recyclerview-part-2-choice-modes/