2013-01-14 382 views
0

我使用ActionBarSherlock和SherlockDialogFragment。 我對我的項目有一點問題,我想在sherlockdialogfragment中顯示其他佈局,一切正常,我的問題是,如果我使用imageadapter,只要我知道它應該在imageadapter中使用「this」。但是在公共靜態「這個」不能使用,它怎麼可能?我不知道如何來顯示sherlockdialogfragment..here佈局是我的問題..SherlockDialogFragment充氣佈局?

((GridView) gridView).setAdapter(new ImageAdapter(this, MOBILE_OS)); 

,這裏是我的全部代碼

public static class MyDialogFragment extends SherlockDialogFragment { 
     int mNum; 
     static MyDialogFragment newInstance(int num) { 
      MyDialogFragment f = new MyDialogFragment(); 
      Bundle args = new Bundle(); 
      args.putInt("num", num); 
      f.setArguments(args); 
      return f; 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      mNum = getArguments().getInt("num"); 
      int style = DialogFragment.STYLE_NORMAL, theme = 0; 
       style = DialogFragment.STYLE_NO_FRAME; 
       theme = android.R.style.Theme; 

      setStyle(style, theme); 

     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
        final String[] MOBILE_OS = new String[] { "Android", "iOS", 
        "Windows", "Blackberry" }; 

      View v = inflater.inflate(R.layout.pen_content, container, false); 
    View gridView = v.findViewById(R.id.gridView1); 
    ((GridView) gridView).setAdapter(new ImageAdapter(this, MOBILE_OS)); 
      return v; 
     } 

    } 

我應該怎麼做才能解決mycode的... 謝謝...

編輯....我的問題解決了..但我問了一個更多....我應該怎麼做,如果我想要從gridview事件結果值單擊??我添加setOnClickListener,但我得到的錯誤...

 @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
        final String[] MOBILE_OS = new String[] { "Android", "iOS", 
        "Windows", "Blackberry" }; 

       View v = inflater.inflate(R.layout.pen_content, container, false); 
      View gridView = v.findViewById(R.id.gridView1); 
      ((GridView) gridView).setAdapter(new ImageAdapter(getSherlockActivity(), MOBILE_OS)); 
      gridView.setOnClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 
        Toast.makeText(getSherlockActivity(),((TextView)v.findViewById(R.id.grid_item_label)).getText(), Toast.LENGTH_SHORT).show(); 
        } 
        }); 
        return v; 
      } 

回答

1

變化

((GridView) gridView).setAdapter(new ImageAdapter(this, MOBILE_OS)); 

所以這是

((GridView) gridView).setAdapter(new ImageAdapter(getSherlockActivity(),MOBILE_OS)); 

你似乎有一個類型不匹配,一個SherlockFragment不延長Context,你的ImageAdapter可能需要它。

嘗試回答您的問題編輯:

((GridView)gridView).setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 
        Toast.makeText(getSherlockActivity(),((TextView)v.findViewById(R.id.grid_item_label)).getText(), Toast.LENGTH_SHORT).show(); 
        } 
        }); 

你仍然需要設置你的聽衆時使用的GridView類。你應該從一開始就投,像這樣:

GridView gridView = (GridView) v.findViewById(R.id.gridView1); 
+0

我的問題是很容易的,我新受關於Java ...:d Ok..it的work..thanks ... – ltvie

+0

@ltvie沒問題,你應該看一下java [tutorials](http://docs.oracle.com/javase/tutorial/),以便更好地掌握OOP。 –

+0

我想問更多..(見編輯) – ltvie