在Class中显示扩展Fragment的DialogBox

时间:2016-09-09 07:04:01

标签: android fragment

我试图在GridView项目上创建一个对话框点击一个扩展片段的类我已经彻底了一些帖子但是那个描述该类扩展FragmentDialog我不明白如何实现这个是我的类

public class Favourite extends Fragment {

GridView gv;
Context context;
SQLiteDatabase db;
String[] title_array,id_array;
Bitmap[] img_array;
Cursor c,c1;


@Nullable
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     db = getActivity().openOrCreateDatabase("MyDb",android.content.Context.MODE_PRIVATE ,null);

}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View InputFragmentView = inflater.inflate(R.layout.favourite, container, false);

    Cursor c=db.rawQuery("select DISTINCT thumb from image",null);
    Cursor c1=db.rawQuery("select DISTINCT title,id from vdetail",null);

    title_array = new String[c1.getCount()];
    id_array = new String[c1.getCount()];
    img_array=new Bitmap[c.getCount()];

    int i = 0;

    if (c.moveToFirst()&&c1.moveToFirst()) {
        do {
            byte[] imagess = c.getBlob(0);
            Bitmap bm = BitmapFactory.decodeByteArray(imagess, 0, imagess.length);
            String title = c1.getString(0);
            String id = c1.getString(1);
            title_array[i] = title;
            id_array[i] = id;
            img_array[i] = bm;
            i++;

            Log.d("getting data", "on retrieving: ");
        } while (c.moveToNext()&&c1.moveToNext());
    }
    gv = (GridView) InputFragmentView.findViewById(R.id.fav_gv);
    gv.setAdapter(new FavourtireAdapter(getActivity(),title_array,id_array,img_array));

    gv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("OK", "onItemLongClick: ");
            return false;
        }
    });
    return InputFragmentView;

}

}

gridview项单击侦听器是我想要显示对话框的部分

3 个答案:

答案 0 :(得分:2)

  

您可以在Alert Dialog内使用on Click Listener

AlertDialog ad = new AlertDialog.Builder(getActivity())
            .create();
    ad.setCancelable(false);
    ad.setTitle(title);
    ad.setMessage(message);
    ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
ad.show();

答案 1 :(得分:1)

如果您尝试使用Dialog。使用Android的Dialog功能。无需实现或扩展类。

final Dialog dialog = new Dialog(context);
    //if not title required
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //layout of dialog
    dialog.setContentView(R.layout.alert_dialog);
    dialog.show();

如果在对话框布局中有ImageView(如果只想显示图像)。

    //for example button
    imageView = (ImageView) dialog.findViewById(R.id.your_id);
    imageView.setImageBitmap(your_image);

答案 2 :(得分:0)

试试这个方法

public void showDialog(String title, String msg) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(con);
    builder.setTitle(title);

    builder.setMessage(msg);

    builder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    //Action on yes

                }

            });
    builder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                    //action on no

                }
            });

    AlertDialog alert = builder.create();
    alert.show();
}

如果想要单个确定按钮,请按照@jaydroider的回答。