piternb 2012-12-07 10:06 采纳率: 100%
浏览 4665
已采纳

当旋转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 2012-12-10 05: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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序