Se si desidera aggiungere CheckBox
con l'icona per AlertDialog
è possibile personalizzato piace
public class DialogMultipleChoiceAdapter extends BaseAdapter {
LayoutInflater mLayoutInflater;
List<Item> mItemList;
public DialogMultipleChoiceAdapter(Context context, List<Item> itemList) {
mLayoutInflater = LayoutInflater.from(context);
mItemList = itemList;
}
@Override
public int getCount() {
return mItemList.size();
}
@Override
public Item getItem(int i) {
return mItemList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
public List<Item> getCheckedItem() {
List<Item> checkedItemList = new ArrayList<>();
for (Item item : mItemList) {
if (item.isChecked()) {
checkedItemList.add(item);
}
}
return checkedItemList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Item item = getItem(position);
holder.tvTitle.setText(item.getTitle());
holder.ivImage.setImageResource(item.getImageRes());
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item.setChecked(!item.isChecked());
updateItemState(holder, item.isChecked());
}
});
updateItemState(holder, item.isChecked());
return convertView;
}
private void updateItemState(ViewHolder holder, boolean checked) {
holder.root.setAlpha(checked ? 1 : 0.8f);
holder.checkbox.setChecked(checked);
}
private static class ViewHolder {
View root;
TextView tvTitle;
ImageView ivImage;
CheckBox checkbox;
ViewHolder(View view) {
root = view;
tvTitle = view.findViewById(R.id.text);
ivImage = view.findViewById(R.id.image);
checkbox = view.findViewById(R.id.checkbox);
}
}
}
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="38dp"
android:layout_height="38dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:layout_toEndOf="@id/image"
tools:text="AA"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Item
modello
class Item {
private String title;
private int imageRes;
private boolean checked;
public Item(String title, int imageRes) {
this.title = title;
this.imageRes = imageRes;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImageRes() {
return imageRes;
}
public void setImageRes(int imageRes) {
this.imageRes = imageRes;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
Infine,
List<Item> itemList = new ArrayList<>();
public void show() {
if (itemList.isEmpty()) {
itemList.add(new Item("A", R.mipmap.ic_launcher));
itemList.add(new Item("B", R.mipmap.ic_launcher));
itemList.add(new Item("C", R.mipmap.ic_launcher));
}
final DialogMultipleChoiceAdapter adapter =
new DialogMultipleChoiceAdapter(mContext, itemList);
new AlertDialog.Builder(mContext).setTitle("Select Image")
.setAdapter(adapter, null)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(mContext,
"getCheckedItem = " + adapter.getCheckedItem().size(),
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.show();
}
Demo on Github
fonte
2017-09-14 08:35:14
è necessario creare finestra di dialogo personalizzata in cui si deve legare un bel xml in cui v'è la casella di controllo – CoronaPintu
cosa l'eccezione che stai affrontando? –
Pubblica un po 'di codice e l'errore (se presente). Anche @Pritu Corna ha ragione. Usa la finestra di dialogo personalizzata. –