【发布时间】:2015-04-21 14:29:00
【问题描述】:
我需要构建一个 DialogFragment,它将用户输入从对话框返回到活动。
对话框需要在 OnClickListener 中调用,当单击列表视图中的元素时调用该对话框。
DialogFragment的返回值(用户的输入)应该在activity的OnClickListener中直接可用。
我试图通过坚持官方文档来实现这一点:http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
我需要类似下面的东西,但它不起作用,因为我不知道如何让匿名 OnClickListener 实现 CustomNumberPicker 类的接口。
据我所知,实现接口对于将数据从 DialogFragment 获取回 Activity 是必要的。
主要活动:
public class MainAcitivity extends ActionBarActivity {
[...]
// ArrayAdapter of the Listview
private class ListViewArrayAdapter extends ArrayAdapter<Exercise> {
public ListViewArrayAdapter(Context context, ArrayList<Exercise> exercises) {
super(context, 0, exercises);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
[...]
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_workoutdetail, parent, false);
}
TextView tvSets = (TextView) convertView.findViewById(R.id.tvWorkoutExerciseSets);
tvSets.setText(sets.toString());
// OnClickListener for every element in the ListView
tvSets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This is where the Dialog should be called and
// the user input from the Dialog should be returned
DialogFragment numberpicker = new CustomNumberPicker();
numberpicker.show(MainActivity.this.getSupportFragmentManager(), "NoticeDialogFragment");
}
// Here I would like to implement the interface of CustomNumberPicker
// in order to get the user input entered in the Dialog
});
return convertView;
}
}
}
CustomNumberPicker(与文档中的基本相同):
public class CustomNumberPicker extends DialogFragment {
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Sets")
.setPositiveButton("set", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Return stuff here to the activity?
}
})
.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
【问题讨论】:
-
您是否有正在使用的自定义布局?您要返回什么?您想返回什么?
-
当对话框按钮被点击时你想返回什么?是字符串还是什么的?
-
@Droidekas 是的,它是一个显示 NumberPicker 的自定义 DialogFragment。我正在尝试返回一个整数,它是用户选择的数字。
-
可以查看this answer.
标签: java android android-listview onclicklistener android-dialogfragment