2016-09-09 34 views
1

即時嘗試在GridView項目上創建一個對話框單擊一個擴展片段的類我已經完成了一些帖子,但那是描述該類的擴展FragmentDialog我不明白如何在下面實現是我的課在擴展片段的類中顯示DialogBox

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的項目,請點擊聽衆就是我想要顯示的對話框

+0

這是什麼問題?您可以使用alertdialog。 – Sandeep

回答

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); 
+1

感謝男人:) :) –

+0

@hatibabrar一個upvode會很好。 :) –

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

在R.String.ok_text上給出錯誤m在字符串文件中創建字符串 –

+0

@hatibabrar在'string.xml'文件中定義像這樣的字符串 OK'就是這樣。 –

+0

@hatibabrar你有沒有得到它的工作或仍然面臨一個問題?讓我知道。 –

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

,如果想單OK按鈕然後按照@jaydroider的回答。

+0

給出java.lang.NullPointerException: –

+1

可能是上下文有問題。用戶上下文con = getApplicationContext();並請在開始開發之前閱讀一些基礎知識。主要問題在於基礎知識。 – Sandeep

相關問題