从服务器获取后如何正确显示数据?

时间:2013-03-13 05:33:15

标签: android

我正在创建一个应用程序,我从服务器检索一些数据,然后将其显示给用户。它涉及以下步骤:

  1. 在ListView中向用户显示名称列表。
  2. 用户点击任何ListView项目。
  3. 获取单击项目的NID,然后我调用AsyncTask类来获取 来自服务器的数据根据​​NID。
  4. 在AsyncTask类onPreExecute方法中,我向用户显示一个对话框,指示正在提取数据。
  5. 现在我解析所有数据并将其放在Bean Class对象中。
  6. 在onPostExecute方法中,我启动另一个Activity(nextactivity)并使用Parcelable Intent将所有数据传递给此活动。
  7. 关闭对话框。
  8. 现在在此活动的onCreate方法(nextactivity)中显示数据。
  9. 但问题是,由于数据较大,活动(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);
        }
    

0 个答案:

没有答案