当我运行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, "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;
}
}
答案 0 :(得分:3)
要在您的应用程序中处理屏幕旋转,您应该在activity标记内添加属性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 :(得分:0)
您可以尝试将数据缓存到磁盘。然后,当您启动任务时,检查数据的年龄。如果它太旧,那么实际上是对服务器的调用。否则,只需使用缓存数据。
另外,我建议您使用loaders而不是AsyncTask。它们更可靠,并且通常会使您的应用更加稳定。
答案 2 :(得分:0)
<activity android:name=".MainActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait">
</activity>
将此设置为您的清单。
答案 3 :(得分:0)
实际上旋转屏幕会导致应用程序关闭并重新打开,这就是您的代码再次运行的原因。您可能需要检查自上次加载数据以来的时间长度,并且只在需要数据时再次加载数据。
答案 4 :(得分:0)
将以下代码写入Androidmanifest.xml文件的Activity标记。
android:configChanges="orientation|keyboardHidden"
更改后,您的活动代码将如下所示。
<activity android:name=".Main_Activity"
android:configChanges="orientation|keyboardHidden" >
请参阅以下链接以获取更多信息。
它将解决您的问题。
答案 5 :(得分:0)
这将帮助您并让您考虑妥善处理事情。 [最佳做法]
答案 6 :(得分:0)
您可以根据自己的情况执行以下操作 1-如果您不想更改应用程序的方向,即使您更改了设备的方向,也要在清单文件集屏幕方向中更改
机器人:screenOrientation = “纵向”
或
机器人:screenOrientation = “风景”
2-如果您想根据设备的方向更改应用程序方向,但不想在更改方向上加载数据,那么请在活动中使用清单文件中
机器人:configChanges = “取向”
3-如果要按特定时间间隔加载数据,请将旧数据与新数据进行比较并执行任务 你可以使用Asynk任务