第一次:我不需要有关如何创建和显示Dialog的课程,请阅读我写的所有内容。
我遇到了问题,这是我的第一个问题。我有一个带有主要活动的应用程序,左侧有一个抽屉,当我点击某个项目时,它会在主要内容中显示 Fragment 。 One Fragment让我在SQLite数据库中添加一个星球。然后另一个片段显示了一个列表,其中包含我在数据库中插入的行星。单击其中一个,将出现 FragmentDialog 并询问我是否要删除行星:如果是,请删除它,否则不执行任何操作。我认为,方法 onYes ()似乎没有被调用。有人知道为什么吗? 这是 FragmentDialog 的类:
public class FragmentDialog extends DialogFragment
{
private IFragmentDialog mListener;
public static FragmentDialog getInstance()
{
return new FragmentDialog();
}
public interface IFragmentDialog
{
public void onYes();
public void onNo();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder vDialogBuilder = new AlertDialog.Builder(getActivity());
vDialogBuilder.setTitle("Attenzione");
vDialogBuilder.setMessage("Vuoi cancellare questo elemento?");
vDialogBuilder.setPositiveButton("Sì", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if (mListener != null)
{
mListener.onYes();
}
}
});
vDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if (mListener != null)
{
mListener.onNo();
}
}
});
return vDialogBuilder.create();
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
if (activity instanceof IFragmentDialog)
{
mListener = (IFragmentDialog) activity;
}
}
}
这是打开FragmentDialog的 Fragment 的 Class :
public class FragmentPlanetList extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, IFragmentDialog
{
ListView mListView;
PlanetCursorAdapter mCursorAdapter;
private ILoadFragment mListener;
public static Uri uri = null;
private static final int PLANET_LOADER_ID = 0;
public static FragmentPlanetList getInstance()
{
return new FragmentPlanetList();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View vView = inflater.inflate(R.layout.fragment_planet_list, container, false);
getActivity().setTitle(R.string.fragment_planet_list);
mListView = (ListView) vView.findViewById(R.id.planet_list);
mCursorAdapter = new PlanetCursorAdapter(getActivity().getApplicationContext(), null, 0);
mListView.setAdapter(mCursorAdapter);
getLoaderManager().initLoader(PLANET_LOADER_ID, null, this);
mListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
uri = Uri.parse(PlanetProvider.PLANETS_URI + "/" + id);
// getActivity()。getContentResolver()。delete(uri,null,null); Toast.makeText(getActivity(),“Cancellato ID”+ id,Toast.LENGTH_SHORT).show();
showDialog();
}
});
return vView;
}
protected void showDialog()
{
FragmentDialog vDialog = FragmentDialog.getInstance();
vDialog.show(getFragmentManager(), "SHOW_DIALOG");
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
if (activity instanceof ILoadFragment)
{
mListener = (ILoadFragment) activity;
}
}
@Override
public void onYes()
{
// not called
Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG).show();
// getActivity()。getContentResolver()。delete(uri,null,null); }
@Override
public void onNo()
{
}
}
编辑:我认为问题是(如OrhanC1所述)if onAttach ()方法。我是否也在我的Activity中实现了FragmentDialog接口?如果是这样,我如何传递列表项的位置和要删除的URI?
答案 0 :(得分:0)
创建DialogHanlder
public class DialogHandler {
public Runnable ans_true = null;
public Runnable ans_false = null;
// Dialog. --------------------------------------------------------------
public boolean Confirm(Activity act, String Title, String ConfirmText,
String CancelBtn, String OkBtn, Runnable aProcedure, Runnable bProcedure) {
ans_true = aProcedure;
ans_false= bProcedure;
AlertDialog dialog = new AlertDialog.Builder(act).create();
dialog.setTitle(Title);
dialog.setMessage(ConfirmText);
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, OkBtn,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
ans_true.run();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, CancelBtn,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
ans_false.run();
}
});
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.show();
return true;
}
}
将其称为另一个活动的方法
public class YourActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(myclick);
}
public final Button.OnClickListener myclick = new Button.OnClickListener() {
@Override
public void onClick(View v) {
doclick();
}
};
public void doclick() {
DialogHandler appdialog = new DialogHandler();
boolean dlg = appdialog.Confirm(this, "Message title", "Message content",
"Cancel", "OK", aproc(), bproc());
}
public Runnable aproc(){
return new Runnable() {
public void run() {
Log.d("Test", "This from A proc");
}
};
}
public Runnable bproc(){
return new Runnable() {
public void run() {
Log.d("Test", "This from B proc");
}
};
}
}
答案 1 :(得分:0)
DialogFragment实际上只是一个包装对话框的片段。您可以通过在DialogFragment的onCreateDialog()方法中创建和返回对话框来在其中放置任何类型的对话框。
使用onCreateDialog()中的任意数量的按钮创建自定义对话框; 在给定的代码中,ok是禁用,您可以通过将代码复制为setNegativeButton Do来启用它。
用新的DialogInterface blah blah替换null。如果你喜欢
下面是一个示例DialogFragment:
class MyDialogFragment extends DialogFragment{
Context mContext;
public MyDialogFragment() {
mContext = getActivity();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Really?");
alertDialogBuilder.setMessage("Are you sure?");
//null should be your on click listener
alertDialogBuilder.setPositiveButton("OK", null);
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
}
创建对话框调用:
new MyDialogFragment().show(getFragmentManager(), "MyDialog");
从某处解雇对话:
((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();
所有这些代码都可以与支持库完美配合,只需更改导入即可使用支持库类。