hatib abrar
hatib abrar

Reputation: 123

Show DialogBox in Class that extends Fragment

i m trying to create a dialogue box on GridView items click in a class that extends fragment i have gone thorough some post but that's describing that class extends FragmentDialog i don't understand how to implement this below is my Class

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 items click listener is the part where i want to show the dialog box

Upvotes: 0

Views: 86

Answers (3)

user6798817
user6798817

Reputation:

If you are trying to use Dialog. Use Dialog feature of Android. No need to implement or extend classes.

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();

If you have ImageView in the dialog layout(If you want to show only image).

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

Upvotes: 1

Sandeep
Sandeep

Reputation: 2593

Try this method

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();
}

and if want single ok button then follow the @jaydroider' answer.

Upvotes: 0

Jay Rathod
Jay Rathod

Reputation: 11245

You can use Alert Dialog inside 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();

Upvotes: 2

Related Questions