我正在使用 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;
}