我有一个对话框片段,我通过setmultiplechoiceitems获取用户的选择(我保留所选项目+通过接口将它们传递给主机活动)。
我现在正在尝试使用alertdialog的自定义布局,我想知道使用我的复选框布局而不是默认布局的最佳方法是什么(因为我已经花了太多时间来获取第一个代码工作,我真的不想重新开始每个复选框,sharefprefs和东西的监听器...)我想知道我是否可以使用我的实际代码我的自定义复选框。
正如您在屏幕截图中看到的那样,我已经使用.setView(inflater.inflate(R.layout.custom_settings,null))
添加了我的自定义布局(底部清单),但所选项目并不持久。
这是我的代码:
public class TimelineSettings extends DialogFragment {
ArrayList<Integer> selected_categories = new ArrayList<Integer>();
boolean[] itemsChecked = {false, false, false, false, false, false};
public interface dialoglistener {
public void onOkay(ArrayList<Integer> selected);
public void onCancel();
}
dialoglistener mlistener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// ensure that the host activity implements the callback interface
try {
// Instantiate the dialogListener so we can send events to the host
mlistener = (dialoglistener) activity;
} catch (ClassCastException e) {
// if activity doesn't implement the interface, throw an exception
throw new ClassCastException(activity.toString()
+ " must implement dialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
for(int i=0;i<itemsChecked.length;i++){
if(selected_categories.contains((String)String.valueOf(i)))
itemsChecked[i]=true;
}
LayoutInflater inflater = getActivity().getLayoutInflater();
// Set the dialog title
builder.setTitle("Choisissez vos paramètres")
.setMultiChoiceItems(R.array.categories, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexselected,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
if(!selected_categories.contains(indexselected)){
selected_categories.add(indexselected);
itemsChecked[indexselected]=true;
}
} else if (selected_categories.contains(indexselected)) {
// Else, if the item is already in the array, remove it
selected_categories.remove(indexselected);
itemsChecked[indexselected]=false;
}
}
})
.setView(inflater.inflate(R.layout.custom_settings,null))
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mlistener.onOkay(selected_categories);
}
})
.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mlistener.onCancel();
}
});
//this part is to customize the look of the alertdialog :
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
Typeface roboto = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Roboto-Thin.ttf");
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.main_green));
TextView tv = (TextView) d.findViewById(textViewId);
tv.setTextColor(getResources().getColor(R.color.main_green));
tv.setTypeface(roboto);
return d; //was : return builder.create();
}
}
答案 0 :(得分:0)
您的selected_categories
是一个Integer对象的ArrayList,但是当您使用selected_categories.contains((String)String.valueOf(i))
检查要检查字符串的内容时
您不会在Integer ArrayList中找到任何字符串,因此该语句将始终返回false。
尝试:
for(int i=0;i<itemsChecked.length;i++){
if(selected_categories.contains(Integer.valueOf(i)) {
itemsChecked[i]=true;
}
}