程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-當旋轉Android屏幕時App自動加載數據

android-當旋轉Android屏幕時App自動加載數據

編輯:編程綜合問答
當旋轉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;

    }
}

最佳回答:


如果要處理應用中的屏幕旋轉,需要添加一個屬性。
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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved