1

在下面的代码中(为清楚起见而缩短),当我旋转设备时,适配器变量接收空值,而 gridView 变量接收空值。

旋转设备屏幕后,谁能帮我保留适配器和gridView?

感谢。

(抱歉英语不好)

    public class FlickrXmlFragment extends Fragment {

    private GridView gridView;
    private FlickrGridViewAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        //...
        View view = darkInflater.inflate(R.layout.fragment_flickr_xml,
                container, false);
        //...
        gridView = (GridView) view.findViewById(R.id.flickr_gridView);

        return view;
    }

    class FlickrPhotoSearchThread extends
            AsyncTask<String, Void, List<FlickrImagem>> {

        @Override
        protected void onPreExecute() { //... process...    }

        @Override
        protected List<FlickrImagem> doInBackground(String... params) { //process... }

        @Override
        protected void onPostExecute(List<FlickrImagem> result) {
            //...
            adapter = new FlickrGridViewAdapter(getActivity(), 0, result);
            gridView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            //...
        }
    }
}
4

1 回答 1

-1

处理您的活动的方向更改。

请参考:http: //developer.android.com/guide/topics/manifest/activity-element.html#config

像这样声明你的活动:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">
于 2013-02-05T13:53:12.960 回答