我在Viewpager中使用AsyncTask,到目前为止一切正常。当我运行我的应用程序时,一切正常,并通过从一列滑动到另一列来显示数据。但是当您启动应用程序时,第一列中的数据不会出现。我已尝试在onCreate中插入以下代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url = "1";
layout = R.id.lista;
empty = R.id.empty;
DownloadJSON newTask = new DownloadJSON(url,layout,empty);
newTask.execute();
...
它不起作用。该应用已关闭。如何在应用启动时加载此数据?
我的代码:
public class MainActivity extends Activity {
ViewPager vp;
private vpAdapter myAdapter;
ListViewAdapter adapter;
ListView listview;
String url;
int layout;
int empty;
ArrayList<HashMap<String, String>> arraylist;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.pager);
myAdapter = new vpAdapter();
vp.setAdapter(myAdapter);
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
//CASE 0 = FIRST PAGE
switch (i) {
case 0:
url = "1";
layout = R.id.lista;
empty = R.id.empty;
DownloadJSON newTask = new DownloadJSON(url,layout,empty);
newTask.execute();
break;
default:
break;
case 1:
url = "2";
layout = R.id.lista2;
empty = R.id.empty2;
DownloadJSON newTask2 = new DownloadJSON(url,layout,empty);
newTask2.execute();
break;
case 2:
url = "3";
layout = R.id.lista3;
empty = R.id.empty3;
DownloadJSON newTask3 = new DownloadJSON(url,layout,empty);
newTask3.execute();
break;
case 3:
url = "4";
layout = R.id.lista4;
empty = R.id.empty4;
DownloadJSON newTask4 = new DownloadJSON(url,layout,empty);
newTask4.execute();
break;
case 4:
url = "5";
layout = R.id.lista5;
empty = R.id.empty5;
DownloadJSON newTask5 = new DownloadJSON(url,layout,empty);
newTask5.execute();
break;
case 5:
url = "6";
layout = R.id.lista6;
empty = R.id.empty6;
DownloadJSON newTask6 = new DownloadJSON(url,layout,empty);
newTask6.execute();
break;
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
String givemeurl;
int givemelayout;
int givemeempty;
public DownloadJSON(String url, int layout, int empty) {
this.givemeurl = url;
this.givemelayout = layout;
this.givemeempty = empty;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ListView listview = (ListView) findViewById(layout);
if(listview.getCount()==0){
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Cargando...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}else{
cancel(true);
Log.e("CANCELADO","CANCELADO");
}
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("URL");
if(jsonobject != null){
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("productos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("imagen", jsonobject.getString("imagen"));
map.put("nombre", jsonobject.getString("nombre"));
map.put("total", jsonobject.getString("total"));
map.put("empresa", jsonobject.getString("empresa"));
map.put("unidades", jsonobject.getString("unidades"));
map.put("precionuevo", jsonobject.getString("precionuevo")+" €");
map.put("precioantiguo", jsonobject.getString("precioantiguo")+" €");
map.put("descripcion", jsonobject.getString("descripcion"));
map.put("direccion", jsonobject.getString("direccion"));
map.put("telefono", jsonobject.getString("telefono"));
map.put("latitud", jsonobject.getString("latitud"));
map.put("longitud", jsonobject.getString("longitud"));
map.put("codeqr", jsonobject.getString("codeqr"));
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
//e.printStackTrace();
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
}else{
//Log.e("Response","No data");
}
return null;
}
@Override
protected void onPostExecute(Void args) {
listview = (ListView) findViewById(givemelayout);
listview.setEmptyView(findViewById(givemeempty));
adapter = new ListViewAdapter(MainActivity.this, arraylist);
listview.setAdapter(adapter);
mProgressDialog.dismiss();
//Log.e("",""+listview.getCount());
}
}
答案 0 :(得分:2)
为了获得更好的性能,您应该在显示视图寻呼机之前下载所有数据。目前,只要用户滑动视图寻呼机,您就会一次又一次地下载相同的数据。并且最初看不到第一页上的任何数据是预期的结果,因为您只在onPageChangeListener中下载数据。我的建议是:
onCreate
方法中,使用AsyncTask
下载您的应用所需的所有必要数据。onPostExecute
的{{1}}方法开始您的主要目的。您可以将获得的数据传递给主要意图: