如何在ImageViews中加载多个图像(parse.com)

时间:2014-01-20 07:17:02

标签: android parse-platform

我在parse.com上将5张图片保存到班级。我可以在imageView中基于objectid下载一个图像。我需要将所有5个图像下载到我的5个ImageViews。我怎样才能做到这一点。任何帮助,将不胜感激。感谢

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Footer");
            // Locate the objectId from the class
    query.getInBackground("tNp607WyQD", new GetCallback<ParseObject>() {
                public void done(ParseObject object,ParseException e) {
                    // TODO Auto-generated method stub
                    // Locate the column named "ImageName" and set
                    // the string
                    ParseFile fileObject = (ParseFile) object.get("imageFile");
                    fileObject.getDataInBackground(new GetDataCallback() {
                                public void done(byte[] data,ParseException e) {
                                    if (e == null) {
                                        Log.d("test","We've got data in data.");
                                        // Decode the Byte[] into
                                        // Bitmap
                                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,data.length);
                                        // Get the ImageView from main.xml
                                        //ImageView image = (ImageView) findViewById(R.id.ad1);
                                        ImageView ad1=(ImageView) findViewById(R.id.ad1);
                                        // Set the Bitmap into the
                                        // ImageView
                                        ad1.setImageBitmap(bmp);
                                        // Close progress dialog
                                        progressDialog.dismiss();
                                    } else {
                                        Log.d("test",
                                                "There was a problem downloading the data.");
                                    }
                                }
                            });
                }
            });
}}

3 个答案:

答案 0 :(得分:3)

我可以借助imageloader类来解决这个问题:

public class Login extends Activity {
public ImageLoader imgl;
EditText fullname, mobilenumber, occupation;
 Button save;
 ImageView ad1,ad2,ad3,ad4,ad5,ad6;  
 List<ParseObject> ob;

 private ImageView[] imgs = new ImageView[5];
 ProgressDialog progressDialog;
 int i=0;
 HorizontalScrollView horizontalScrollView1;        

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.userdata);
     imgl=new ImageLoader(getApplicationContext());
     fullname = (EditText) findViewById(R.id.fullname) ;
     mobilenumber = (EditText) findViewById(R.id.mobile) ;
    occupation = (EditText) findViewById(R.id.occupation) ;
     save=(Button) findViewById(R.id.btnSave);      
    horizontalScrollView1=(HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
    ad1=(ImageView) findViewById(R.id.ad1);
    ad2=(ImageView) findViewById(R.id.ad2);
    ad3=(ImageView) findViewById(R.id.ad3);
    ad4=(ImageView) findViewById(R.id.ad4);
    ad5=(ImageView) findViewById(R.id.ad5);
    ad6=(ImageView) findViewById(R.id.ad6);
     imgs[0] = ad2; 
     imgs[1] = ad3; 
     imgs[2] = ad4; 
     imgs[3] = ad5; 
     imgs[4] = ad6;

     progressDialog= ProgressDialog.show(Login.this, "","Downloading Image...", true);
                // Locate the class table named "Footer" in Parse.com
     ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
             "Footer");
     query.orderByDescending("updatedAt");
     try {
        ob = query.find();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     for (ParseObject country : ob) {
         ParseFile image = (ParseFile) country.get("imageFile");
         imgl.DisplayImage(image.getUrl(), imgs[i]);
        i=i+1;
         System.out.println("the urls are"+image.getUrl());
     progressDialog.dismiss();
     }
 }
}

答案 1 :(得分:1)

这个怎么样? (也许这是一种非常粗暴的方式)

由于您有5个(已定义的)图像视图,因此请创建一个图像视图数组。

private ImageView iv1, iv2, iv3, iv4, iv5;
private ImageView[] imgs;

在onCreate(),

imgs = new ImageView[5];
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);

等等

然后像这样设置imageview数组

imgs[0] = iv1;
imgs[1] = iv2;
imgs[2] = iv3;

//等等

在你的ParseQuery中,假设你得到了所需的ParseObjects列表(5,我相信), 在GetCallBack的完成方法中,

public void done(List<ParseObject> objects,ParseException e) {
for(int i =0 ; i < objects.size(); i++){

           ParseObject object = objects.get(i);
           ParseFile fileObject = (ParseFile) object.get("imageFile");
           fileObject.getDataInBackground(new GetDataCallback() {
                                public void done(byte[] data,ParseException e) {
                                    if (e == null) {

                                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,data.length);
                                        imgs[i].setImageBitmap(bmp);

                                    } else {
                                        Log.d("test",
                                                "There was a problem downloading the data.");
                                    }
                                }
                            });
                }
progressDialog.dismiss();
}

在ParseQuery中,要按照createdAt的升序获取图像,请使用

query.orderByAscending("createdAt");

如果你在ParseQuery中遇到变量'i'的任何问题,那就好了(特别是你可能不允许在done()里面使用i并且会要求你宣布它为final),让我知道!

答案 2 :(得分:0)

您需要以可以创建下载任务的自定义实例的方式重构代码。

例如,创建一个包装ParseQuery的FooterQuery类。构造对象时,传入parseId和应该加载到的ImageView的资源ID。

如果ImageView由AdapterView控制(列出列表或网格视图),则会更复杂。