在AsyncTask中动态添加视图

时间:2012-07-31 12:27:29

标签: views android-asynctask add

我在循环中创建一些文本和图像视图,从另一个类接收一些数据,如何创建那些文本和图像视图并在AsyncTask中使用addview方法?

我在doInBackground方法中使用addview,这不起作用.. 另外,我不知道这是否是动态添加视图的最佳方式,欢迎提出建议!

class syncTask extends AsyncTask<Void, Void, Void> {
     private ProgressDialog pd;
     int increment=0;
     RelativeLayout layout;
     public syncTask(RelativeLayout layout) {
        // TODO Auto-generated constructor stub
         this.layout = layout;
    }

    @Override
     protected void onPreExecute() {
        pd = new ProgressDialog(main.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setProgress(0);
        pd.setMax(0);
        pd.setMessage("Loading friends");
        pd.show();
     }

    @Override
     protected Void doInBackground(Void... v) {
        try
        {
            Set<User> users = LFMRestConnection.getFriendsNowListening(user, 1440);
            for (User u : users) {
                pd.setMax(max);
                //album picture
                    album_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                    resized_a_p = android.graphics.Bitmap.createScaledBitmap(album_pic, (width/3), (width/3), true);
                    iv_album = new ImageView(main.this);
                    iv_album.setImageBitmap(resized_a_p);
                    iv_album.layout(0, 0, 0, 0);
                    iv_album.setPadding(width-resized_a_p.getWidth()-5, t_img, 5, 0);
                //profile picture
                    user_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                    resized_u_p = android.graphics.Bitmap.createScaledBitmap(user_pic, ((width/4)-10)/2, ((width/4)-10)/2, true);
                    iv_user = new ImageView(main.this);
                    iv_user.setImageBitmap(resized_u_p);
                    iv_user.setLayoutParams(params_2);
                    iv_user.layout(0, 0, 0, 0);
                    iv_user.setPadding(0, (t_img+resized_a_p.getHeight())-resized_u_p.getHeight(), resized_a_p.getWidth()+10, 0);
                //shapes
                    //shapes = new shapes(this, (t_img+resized_a_p.getHeight()), resized_a_p.getWidth()+10);
                //username text position
                    tv_user = new TextView(main.this);
                    tv_user.layout(0, 0, 0, 0);
                    tv_user.setTextSize(12);
                    tv_user.setPadding(0, (t_img+resized_a_p.getHeight())-resized_u_p.getHeight()+14, (resized_a_p.getWidth()+resized_u_p.getWidth())+15, 0);
                    tv_user.setGravity(Gravity.RIGHT);
                    tv_user.setLayoutParams(params_1);
                //artist text position
                    tv_artist = new TextView(main.this);
                    tv_artist.layout(0, 0, 0, 0);
                    tv_artist.setTextSize(20);
                    tv_artist.setPadding(5, t_img-10, 0, 0);
                    tv_artist.setGravity(Gravity.LEFT);
                //track text position
                    tv_info = new TextView(main.this);
                    tv_info.setTextSize(12);
                    tv_info.layout(0, 0, 0, 0);
                    tv_info.setPadding(5, t_img+12, 0, 0);
                    tv_info.setGravity(Gravity.LEFT);

                //artist
                    tv_artist.setTypeface(null, Typeface.BOLD);
                    tv_artist.setText(u.getLastTrack().getArtistName());
                //username 
                    tv_user.setTypeface(null, Typeface.NORMAL);
                    tv_user.setText(u.getUserName()+"\n");
                    tv_user.append(u.getRealName());
                //track
                    tv_info.setTypeface(null, Typeface.NORMAL);
                    tv_info.append("\n" + u.getLastTrack().getTrackName());
                    tv_info.append("\n" + u.getLastTrack().getAlbumName());

                //move position
                    t_img+=(width/3)+20;

                increment++;
                pd.incrementProgressBy(increment);
                //views
                layout.addView(iv_album);
                layout.addView(iv_user);
                layout.addView(tv_info);
                layout.addView(tv_user);
                layout.addView(tv_artist);
            }
        }
        catch(Exception e)
        {
            //startActivity(i);
            System.out.println("Error");
            //finish();     
        }

        return null;

     }
     @Override
        protected void onPostExecute(Void result) {

         pd.dismiss();
     }

0 个答案:

没有答案