2 piternb piternb 于 2012.12.07 18:06 提问

当旋转Android屏幕时App自动加载数据

我打开我的应用时数据会自动加载,
我的问题是:当屏幕是垂直的时候应用会从服务器上加载下数据,但当屏幕旋转的时候,它又重新加载数据,在垂直状态又重新加载,旋转也是,一直如此。我应该怎么做才能让它不重新加载数据,希望大家能帮我看看,应该如何处理。

程序代码如下:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nhabep); 
    new LoadData().execute();
}


 // Class load data
private class LoadData extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressDialog;
    private JSONArray jArray;
    private String result = null;
    private InputStream is = null;
    private StringBuilder sb = null;

    @Override
    protected void onPreExecute() {
        this.progressDialog = ProgressDialog.show(Nhabep.this, "",
                " Loading...");
    }

    @Override
    protected void onPostExecute(final Void unused) {
        this.progressDialog.dismiss();
        try {
            if (flag == false)
            {
                Toast.makeText(Nhabep.this, "欢迎使用!!", Toast.LENGTH_SHORT).show();
            }
            else
            {
                // listview 
            listview = (ListView) findViewById(R.id.listView1);
            this.progressDialog.dismiss();
            listview.setAdapter(new DataAdapter(Nhabep.this, soban
                    .toArray(new String[soban.size()]), thoigian
                    .toArray(new String[thoigian.size()])));
            listview.setOnItemClickListener(new OnItemClickListener() {

                // listview 
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    String soban2 = soban.get(position);
                    String cafesua2 = cafesua.get(position);
                    String cafeda2 = cafeda.get(position);
                    String cafeden2 = cafeden.get(position);
                    String duatuoi2 = duatuoi.get(position);
                    String nuocngot2 = nuocngot.get(position);
                    String cavienchien2 = cavienchien.get(position);
                    String goiga2 = goiga.get(position);
                    String bokho2 = bokho.get(position);
                    String bunbo2 = bunbo.get(position);
                    Intent i = new Intent(Nhabep.this, Show.class);
                    i.putExtra("soban", soban2);
                    i.putExtra("cafesua", cafesua2);
                    i.putExtra("cafeda", cafeda2);
                    i.putExtra("cafeden", cafeden2);
                    i.putExtra("duatuoi", duatuoi2);
                    i.putExtra("nuocngot", nuocngot2);
                    i.putExtra("cavienchien", cavienchien2);
                    i.putExtra("goiga", goiga2);
                    i.putExtra("bokho", bokho2);
                    i.putExtra("bunbo", bunbo2);
                    startActivity(i);
                }
            });
            Toast.makeText(Nhabep.this, "欢迎使用!!", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {

            Toast.makeText(getApplicationContext(), e.toString(),
                    Toast.LENGTH_LONG).show();
        }

    }

    // server 
    @Override
    protected Void doInBackground(Void... params) {
        try {

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://longvansolution.tk/loadthongtin.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 80);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

            if (result.toString().equalsIgnoreCase("null\n"))
            {
                flag = false;
                soban.clear();
                thoigian.clear();
                cafesua.clear();
                cafeda.clear();
                cafeden.clear();
                duatuoi.clear();
                nuocngot.clear();
                cavienchien.clear();
                goiga.clear();
                bokho.clear();
                bunbo.clear();

            } else
            {
                // tin theo Json
                jArray = new JSONArray(result);
                if (jArray != null && jArray.length() > 0)
                {
                    JSONObject json_data = null;
                    soban.clear();
                    thoigian.clear();
                    cafesua.clear();
                    cafeda.clear();
                    cafeden.clear();
                    duatuoi.clear();
                    nuocngot.clear();
                    cavienchien.clear();
                    goiga.clear();
                    bokho.clear();
                    bunbo.clear();
                    for (int i = 0; i < jArray.length(); i++) {
                        json_data = jArray.getJSONObject(i);
                        thoigian1 = json_data.getString("date");
                        soban1 = json_data.getString("ban");
                        cafesua1 = json_data.getString("cafesua");
                        cafeda1 = json_data.getString("cafeda");
                        cafeden1 = json_data.getString("cafeden");
                        duatuoi1 = json_data.getString("duatuoi");
                        nuocngot1 = json_data.getString("nuocngot");
                        cavienchien1 = json_data.getString("cavienchien");
                        goiga1 = json_data.getString("goiga");
                        bokho1 = json_data.getString("bokho");
                        bunbo1 = json_data.getString("bunbo");
                        thoigian.add(thoigian1);
                        soban.add(soban1);
                        cafesua.add(cafesua1);
                        cafeden.add(cafeden1);
                        cafeda.add(cafeda1);
                        duatuoi.add(duatuoi1);
                        nuocngot.add(nuocngot1);
                        cavienchien.add(cavienchien1);
                        goiga.add(goiga1);
                        bokho.add(bokho1);
                        bunbo.add(bunbo1);
                    }
                }
                flag = true;
            }
        } catch (Exception e) {
            // Log.e("log_tag", "Error in http connection" + e.toString());
            Toast.makeText(getApplicationContext(), e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        return null;

    }
}

2个回答

JonSkeet
JonSkeet   2012.12.10 13:55
已采纳

如果要处理应用中的屏幕旋转,需要添加一个属性。
android:configChanges内的活动标签如下:

<activity android:name=".Your_Activity"
    android:configChanges="orientation|keyboardHidden" >

并且在代码部分重写onConfigurationChanged,为了避免再次运行AsyncTask,需要用一个布尔值标记屏幕是否已经旋转。

public class Your_Activity extends ListActivity {
    public static boolean onrotaion=true; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(onrotaion){
        new LoadData().execute();
          }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
          //Change flag  here 
          onrotaion=false;
    }
}
hvk687
hvk687   2012.12.09 14:24

在AnrdoidManifest中,添加对onconfigchange的支持,让屏幕旋转后不会被杀掉

suannai0314
suannai0314 详细的回答更能得到用户的青睐,而且更容易被采纳哦
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片