DialogFragment(ListViewを使った複数選択)
DialogFragmentとListViewを使って複数選択ダイアログを使う 重要なことは下記の通り
- android.R.layout.simple_list_item_multiple_choiceを使う
- ListViewで”multipleChoice”を設定する。
public class MultiSelectDialog extends DialogFragment {
private CustomAdapter adapter;
public static MultiSelectDialog newInstance(int title) {
MultiSelectDialog frag = new MultiSelectDialog();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.hoge , null);
ArrayList<string> itemList = new ArrayList<string>();
adapter = new CustomAdapter(getActivity(), 0, itemList);
ListView listView = (ListView)view.findViewById(R.id.list);
listView.setAdapter(adapter);
return new AlertDialog.Builder(getActivity())
.setTitle("一覧")
.setView(view)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
}
// ListViewで使用するadapter
public class CustomAdapter extends ArrayAdapter<parseobject> {
private LayoutInflater inflater;
public CustomAdapter(Context context, int resource, List<strings> objects) {
super(context, resource, objects);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View v, final ViewGroup parent) {
String item = getItem(position);
if (null == v) {
//Androidデフォルトのリソースを利用する。
v = inflater.inflate(android.R.layout.simple_list_item_multiple_choice, null);
}
TextView intTextView = (TextView) v.findViewById(android.R.id.text1);
intTextView.setText(item);
return v;
}
}
}
レイアウトファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/skill_name_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:fastScrollEnabled="true" />
</LinearLayout>