Android 片段动态视图在旋转时消失

Android 片段动态视图在旋转时消失,android,android-fragments,android-fragmentactivity,screen-rotation,Android,Android Fragments,Android Fragmentactivity,Screen Rotation,我整晚都在寻找解决办法。。但我似乎找不到一个适合我的代码:S。我的片段在旋转时被破坏了,我知道我必须将它保存在一个包或其他东西中,但我太没时间让它工作。 请帮忙,这是我的代码: package com.remcoborst.mymovielibrary; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;

我整晚都在寻找解决办法。。但我似乎找不到一个适合我的代码:S。我的片段在旋转时被破坏了,我知道我必须将它保存在一个包或其他东西中,但我太没时间让它工作。 请帮忙,这是我的代码:

package com.remcoborst.mymovielibrary;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
    // #5781a9
    SectionsPagerAdapter mSectionsPagerAdapter;
    private static Context context;

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity.context = getApplicationContext();

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this)
            );
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            if(position == 0){
                // My Movies class
                Fragment fragment = new MyMovies(); 
                return fragment;
            }else{
                // Add Movies
                Fragment fragment = new AddMovies();
                return fragment;
            }
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return "My Movies".toUpperCase(l);
            case 1:
                return "Add".toUpperCase(l);
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class MyMovies extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Rootview is the fragment layout, fragment_main_dummy.xml in layouts
            View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);

            return rootView;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class AddMovies extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Rootview is the fragment layout, fragment_main_dummy.xml in layouts
            final View rootView = inflater.inflate(R.layout.fragment_form, container, false);
            final LinearLayout listViewData = (LinearLayout) rootView.findViewById(R.id.listViewData);

            // Create button
            Button bt = (Button) rootView.findViewById(R.id.addmovie);
            bt.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    EditText inputfield = (EditText) rootView.findViewById(R.id.imdblink);
                    String sTitle = inputfield.getText().toString().trim().replace(' ', '+');
                    if(MainActivity.hasActiveInternetConnection(getActivity())){
                        try{
                            listViewData.removeAllViews();
                            LoadMovies LoadMovies = new LoadMovies(getActivity());
                            LoadMovies.execute("http://imdbapi.org/?title="+sTitle+"&type=json&plot=simple&limit=10&lang=en-US&release=simple&mt=M");

                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }else{
                        showWarning("No working internet connection found!","Please ensure that you have a working internet connection before searching for movies.",getActivity());
                    }

                }

            });

            return rootView;
        }
    }

    public static class LoadMovies extends AsyncTask<String, Void, List<LinearLayout>> {
        private Context SubContext;
        private ProgressDialog pd;

        public LoadMovies(Context SubContext){
            this.SubContext = SubContext;
        }

        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(this.SubContext, "", "Loading. Please wait...", true);
        } 


        @Override
        protected List<LinearLayout> doInBackground(String... urls) {
            StringBuilder builder = new StringBuilder(1000000);
            List<LinearLayout> localArrayList = new ArrayList<LinearLayout>();

            for (String url : urls) {
                Log.v("URLS",url);
                if(url != "" && url != null){
                    try {                   
                        HttpClient client = new DefaultHttpClient();
                        HttpGet request = new HttpGet(url);
                        HttpResponse response = client.execute(request);

                        InputStream in = response.getEntity().getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                        String line = null;
                        while((line = reader.readLine()) != null)
                        {
                            builder.append(line);
                        }
                        in.close();

                        String sHtml = builder.toString();
                        // Change json slightly...
                        sHtml = "{ \"Movies\": "+sHtml+" }";

                        JSONObject json = new JSONObject(sHtml);
                        JSONArray jsa = json.getJSONArray("Movies");

                        for(int i = 0; i < jsa.length(); i++){
                            JSONObject c = jsa.getJSONObject(i);

                            if(c.has("poster")){
                                String sImageUrl = c.getString("poster");
                                try{
                                    ImageView imdbimg = new ImageView(this.SubContext);

                                    URL imgUrl = new URL(sImageUrl.replaceAll(" ", "%20"));
                                    Bitmap bmp = BitmapFactory.decodeStream(imgUrl.openConnection().getInputStream());
                                    imdbimg.setImageBitmap(bmp);

                                    imdbimg.setAdjustViewBounds(true);
                                    imdbimg.setMaxHeight(200);
                                    imdbimg.setMaxWidth(135);
                                    imdbimg.setPadding(0, 0, 20, 0);

                                    LinearLayout dynLL1 = new LinearLayout(this.SubContext);
                                    LinearLayout.LayoutParams LLparams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

                                    dynLL1.setOrientation(LinearLayout.VERTICAL);
                                    dynLL1.setLayoutParams(LLparams1);

                                        TextView textView1 = new TextView(this.SubContext);
                                        textView1.setTextColor(Color.parseColor("#000000"));
                                        textView1.setTextSize(18);
                                        textView1.setPadding(0, 10, 20, 10);
                                        textView1.setText(c.getString("title"));

                                        TextView textView2 = new TextView(this.SubContext);
                                        textView2.setText("Year: "+c.getString("year"));

                                        JSONArray aRuntime = c.getJSONArray("runtime");
                                        TextView textView3 = new TextView(this.SubContext);
                                        textView3.setText("Length: "+aRuntime.getString(0));

                                    dynLL1.addView(textView1);
                                    dynLL1.addView(textView2);
                                    dynLL1.addView(textView3);

                                    LinearLayout dynLL2 = new LinearLayout(this.SubContext);
                                    LinearLayout.LayoutParams LLparams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LLparams2.setMargins(20, 20, 20, 20);

                                    dynLL2.setOrientation(LinearLayout.HORIZONTAL);
                                    dynLL2.setLayoutParams(LLparams2);
                                    dynLL2.setBackgroundColor(Color.parseColor("#ffffff"));
                                    dynLL2.addView(imdbimg);
                                    dynLL2.addView(dynLL1);

                                    localArrayList.add(dynLL2);
                                }catch(Exception e){
                                    e.printStackTrace();
                                }
                            }
                        };
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            return localArrayList;
        }


        @Override
        protected void onPostExecute(List<LinearLayout> localArrayList) {
            LinearLayout listViewData = (LinearLayout) ((Activity) this.SubContext).findViewById(R.id.listViewData);
            for (final LinearLayout dynLL : localArrayList) {               
                listViewData.addView(dynLL);
            }

            pd.dismiss();
        }
    }

    public static boolean hasActiveInternetConnection(Context SubContext) {
        if (isNetworkAvailable(SubContext)) {
            try {
                HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
                urlc.setRequestProperty("User-Agent", "Test");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(1500); 
                urlc.connect();
                return (urlc.getResponseCode() == 200);
            } catch (IOException e) {
                Log.e("ERROR: ", "Error checking internet connection", e);
            }
        } else {
            Log.d("ERROR: ", "No network available!");
        }
        return false;
    }

    private static boolean isNetworkAvailable(Context SubContext) {
        ConnectivityManager connectivityManager = (ConnectivityManager) SubContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }


    public static void showWarning(String title, String mymessage,Context context)
    {
        new AlertDialog.Builder(context)
           .setMessage(mymessage)
           .setTitle(title)
           .setCancelable(true)
           .setIcon(R.drawable.warning)
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    //do things
               }
           })
           .show();
    }


}
package com.remcoborst.mymovielibrary;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URI;
导入java.net.URL;
导入java.net.URLConnection;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Locale;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.ActionBar;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.FragmentTransaction;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Color;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.LinearLayout.LayoutParams;
导入android.widget.ListView;
导入android.widget.TextView;
公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener{
//#5781a9
分段SPAGERADAPTER mSectionsPagerAdapter;
私有静态语境;
ViewPager mViewPager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainActivity.context=getApplicationContext();
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
//设置操作栏。
最终ActionBar ActionBar=getActionBar();
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//应用程序的主要部分。
mSectionsPagerAdapter=newsectionspageradapter(getSupportFragmentManager());
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//在不同区段之间滑动时,选择相应的
//我们也可以使用ActionBar.tab#select()来完成这项工作
//对选项卡的引用。
mViewPager.setOnPageChangeListener(新的ViewPager.SimpleOnPageChangeListener(){
@凌驾
已选择页面上的公共无效(内部位置){
actionBar.setSelectedNavigationItem(位置);
}
});
//对于应用程序中的每个部分,在操作栏中添加一个选项卡。
对于(int i=0;i  }else{
   //means orientation change grab the stuff you want saved to recreate the UI
   if(savedInstanceState.containsKey(EXTRA_LIST){
       mList = savedInstanceState.getParcelableArrayList(EXTRA_LIST)        
   }
   if(savedInstancestate.containsKey(EXTRA_INDEX){
       this.mDisplayedIndex = savedInstanceState.getInt(EXTRA_DISPLAYED_INDEX);
   }
}
@Override
protected void onSaveInstanceState(Bundle outState){
      outState.putInt(EXTRA_DISPLAYED_INDEX, mDisplayedIndex);
      outState.putParcelableArrayList(EXTRA_APPS_LIST, mList);
      super.onSaveInstanceState(outState);

    }