アプリを実行すると、アプリはデータを自動ロードします。問題は、画面の垂直方向のアプリがサーバーからデータをロードする場合ですが、画面を水平方向に回転させると、データが再度ロードされます。ローテーション画面アプリが再びデータをロードするときにそうすること。助けてくれませんか。
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, "Không có bàn nào được chọn!!", Toast.LENGTH_SHORT).show();
}
else
{
//Hiển thị thông tin các món ăn lên 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() {
//xử lý khi chọn các item trên 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, "Thông tin bàn được tải thành công!!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
}
//kết nối đến sererver và nhân thông tin trả về từ 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();
//Kiểm tra thông tin nhận được từ server
//nếu null sẽ clear các textview
//nếu có thông tin thì đọc thông tin theo Json
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
{
//Lấy thông 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;
}
}