Sherlock DialogFragment膨胀布局?

时间:2013-01-14 00:27:07

标签: android android-layout actionbarsherlock

我正在使用ActionBarSherlock和SherlockDialogFragment。 我的项目有点问题,我想在sherlockdialogfragment中显示其他布局,一切运作良好,我的问题是,如果我使用imageadapter,只要我知道它应该在imageadapter中使用“this”但是在公共场合静态“这”不能用,怎么会??我不明白如何在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;
        }

    }

我该怎么做来修复我的代码...... 感谢...

编辑....我的问题解决了..但我再问一个......我该怎么办,如果我想从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 个答案:

答案 0 :(得分: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);