我正在创建一个自定义AlertDialog来获取这样的项目列表......
对话片段:
public class MultiListDialog extends DialogFragment {
private ArrayList<Integer> selectedItems = new ArrayList<>();
public MultiListDialog newInstance(Bundle args) {
MultiListDialog d = new MultiListDialog();
d.setArguments(args);
return d;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
selectedItems = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getArguments().getString("title", ""))
.setMultiChoiceItems(getArguments().getCharSequenceArray("list"), null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
selectedItems.remove(Integer.valueOf(which));
}
}
})
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
}
})
.setNegativeButton(R.string.cancel_only, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
}
});
return builder.create();
}
public ArrayList<String> getSelectedItems() {
ArrayList<String> ret = new ArrayList<>();
for (int i=0; i<selectedItems.size(); i++)
ret.add(getArguments().getCharSequenceArray("list")[i].toString());
return ret;
}
}
的活动:
Bundle args = new Bundle();
args.putString("title", "My Title");
args.putCharSequenceArray("list", arrayList.toArray(new CharSequence[arrayList.size()]));
DialogFragment fragment = new MultiListDialog().newInstance(args);
fragment.show(getFragmentManager(), "my_frag");
如何在OK点击中检索所选项目?我尝试添加接口和抽象函数,但无法使其正常工作。
答案 0 :(得分:0)
可以使用SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(longitude), Double.parseDouble(latitude))));
}
方法将其强制转换为监听界面。您还可以将活动作为侦听器传递给对话框片段,并在DialogFragment#getActivity()