我正在创建一个应用程序,我从服务器检索一些数据,然后将其显示给用户。它涉及以下步骤:
但问题是,由于数据较大,活动(nextactivity)需要更多时间加载,ListView活动会向用户显示2-3秒,然后向用户显示nextActivty。
我在这里也给你了我的代码:
这是我调用AsyncTask类来获取数据的方法:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position).getNid(),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
if(rowItems.get(position).getNid()!=null){
String[] params=new String[1];
params[0]=rowItems.get(position).getNid();
new RepresentativeDataAsynTask(RespresentativeList.this).execute(params);
}
这是我获取所有数据的AsyncTask类:
@Override
protected void onPreExecute() {
proDialog = new ProgressDialog(mContext);
proDialog.setTitle("Support Manager");
proDialog.setMessage("Fetching Details ...");
proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
proDialog.setIcon(R.drawable.ic_launcher);
proDialog.show();
super.onPreExecute();
}
@Override
protected RepresentativeData doInBackground(String... params) {
JSONObject result = null;
RepresentativeData representativeDetails = new RepresentativeData();
try {
result = client.reprentativeDetailNode(mContext.getString(R.string.Server),params[0]);
//fetching all data by parsing result
} catch (ClientProtocolException e) {
error=true;
e.printStackTrace();
} catch (IOException e) {
error=true;
e.printStackTrace();
}
return representativeDetails;
}
@Override
protected void onPostExecute(RepresentativeData result) {
Intent yourRepresentative = new Intent(mContext,YourRepresentative.class);
yourRepresentative.putExtra("representative detail", result);
proDialog.dismiss();
mContext.startActivity(yourRepresentative);
}
super.onPostExecute(result);
}