0

我正在使用 ActionBarSherlock 和 SherlockDialogFragment。我的项目有一个小问题,我想在 sherlockdialogfragment 中显示其他布局,一切正常,我的问题是,如果我使用 imageadapter,只要我知道它应该在 imageadapter 中使用“this”..但在公共场合静态“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;
            }
4

1 回答 1

1

改变

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

所以它是

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

您似乎有类型不匹配, a SherlockFragmentdoes not extend 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);
于 2013-01-14T00:30:35.990 回答