如何显示进度微调在Android中,在执行doInBackground() [英] How to show a progress spinner in android, when doInBackground() is being executed

查看:153
本文介绍了如何显示进度微调在Android中,在执行doInBackground()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里我使用的AsyncTask来从一个服务器上的数据活动类:

This is my Activity class where i use AsyncTask to get data from a server:

public class UserProfileActivity extends Activity {

    private ImageView userImage;
    private TextView userName;
    private TextView userLocation;
    private TextView editInfo;
    private TextView chnageImage;
    private TextView userScore;
    private ListView friendsList;
    public ArrayAdapter<String> adapter;
    public int score;
    public int level;
    public String image;
    public String fname;
    public String lname;
    public String city;
    public int id;
    public String email;
    protected Activity activity = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.user_profile);

        userImage = (ImageView) findViewById(R.id.profileImage);
        userName = (TextView) findViewById(R.id.userName_profile);
        userLocation = (TextView) findViewById(R.id.userLocation_profile);
        editInfo = (TextView) findViewById(R.id.edit_profile);
        chnageImage = (TextView) findViewById(R.id.changeImage_profile);
        userScore = (TextView) findViewById(R.id.userScore_profile);
        friendsList = (ListView) findViewById(R.id.friendsList);

        new LongOperation().execute("");

    }

    private class LongOperation extends AsyncTask<String, Void, String> {

        private InputStream is;
        private StringBuilder sb;
        private String result;

        @Override
        protected String doInBackground(String... params) {

            try {
                HttpPost httppost = new HttpPost(
                        "http://www.xxxxxxxxx.com/mobile/getProfileInfo");
                HttpResponse response = SignUpActivity.httpclient
                        .execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "iso-8859-1"), 8);
                    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();
                } catch (Exception e) {
                }
                try {
                    JSONObject jObj = new JSONObject(result);
                    String status = jObj.getString("status");
                    score = jObj.getInt("credits");
                    level = jObj.getInt("level");
                    image = jObj.getString("image");
                    fname = jObj.getString("fname");
                    lname = jObj.getString("lname");
                    city = jObj.getString("city");
                    id = jObj.getInt("user_id");
                    email = jObj.getString("email");

                    JSONArray friendsJsonArray = jObj.getJSONArray("friends");
                    int size = friendsJsonArray.length();

                    ArrayList<String> friendsNames = new ArrayList<String>();
                    String[] friendsIds = new String[size];
                    for (int i = 0; i < size; i++) {

                        friendsNames.add(friendsJsonArray.getJSONObject(i)
                                .getString("name"));
                    }
                    adapter = new ArrayAdapter<String>(getApplicationContext(),
                            R.layout.simple_listview_item, friendsNames);

                } catch (Exception e) {
                }
            } catch (Exception e) {
            }

            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {

            friendsList.setAdapter(adapter);
            userScore.setText(score + " points" + "   level " + level);
            userName.setText(fname + "  " + lname);
            userLocation.setText(city);
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory
                        .decodeStream((InputStream) new URL(image).getContent());
            } catch (MalformedURLException e1) {

                e1.printStackTrace();
            } catch (IOException e2) {

                e2.printStackTrace();
            }
            userImage.setImageBitmap(bitmap);

        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }
}

在此活动中加载它显示了所有默认值和图像,然后当后台code执行被竞争(如除外)的变化,但是这需要2-3秒哪些用户将看到默认值,我不想。那么,如何可以保持微调是这样的:

when this activity is loaded it shows all the default values and images and then changes when background code execution is competed(as excepted), but this takes 2-3 secs for which user will be seeing default values, which i dont want to. So how can i keep a spinner like this:

2〜3秒,然后当微调消失的活动必须显示实际值。

for 2-3 secs and then when the spinner disappears the activity must show the actual values.

感谢您

推荐答案

请参考下面的code

Refer the below code

private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

    /** progress dialog to show user that the backup is processing. */
    /** application context. */
    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();
    }

    @Override
    protected Boolean doInBackground(final String... args) {
        try {

            Utilities.arrayRSS = objRSSFeed
                    .FetchRSSFeeds(Constants.Feed_URL);
            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        // Setting data to list adapter
        setListData();
    }

这篇关于如何显示进度微调在Android中,在执行doInBackground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆