2014-01-23 59 views
1

我想在運行時在ImageView上顯示圖像,並從Parse.com獲取圖像。我能夠用我的代碼中預定義的ImageViews數組顯示圖像。像:如何在運行時生成圖像查看

ImageView ad1,ad2,ad3,ad4,ad5,ad6; 
private ImageView[] imgs = new ImageView[5]; 
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; 
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Footer"); 
    query.orderByDescending("updatedAt"); 
    query.whereEqualTo("Status", true); 
     ob = query.find(); 
    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()); 

       } 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

XML佈局:

<HorizontalScrollView 
android:id="@+id/horizontalScrollView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<ImageView 
    android:id="@+id/ad2" 
    android:layout_width="90dp" 
    android:layout_height="wrap_content" 
    android:layout_margin="3dp" 
    tools:ignore="ContentDescription" /> 

    <ImageView 
     android:id="@+id/ad3" 
     android:layout_width="90dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="3dp" 
     tools:ignore="ContentDescription" /> 

    <ImageView 
     android:id="@+id/ad4" 
     android:layout_width="90dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="3dp" 
     tools:ignore="ContentDescription" /> 

    <ImageView 
     android:id="@+id/ad5" 
     android:layout_width="90dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="3dp" 
     tools:ignore="ContentDescription" /> 

    <ImageView 
     android:id="@+id/ad6" 
     android:layout_width="90dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="3dp" 
     tools:ignore="ContentDescription" /> 

    </LinearLayout> 
</HorizontalScrollView> 
</LinearLayout> 

有沒有什麼辦法讓我不必硬編碼ImageViews連帶與parseobjects的數量自動生成?由於

回答

1
LinearLayout container=(LinearLayout) findViewById(R.id.container); 

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Footer"); 
    query.orderByDescending("updatedAt"); 
    query.whereEqualTo("Status", true); 
     ob = query.find(); 
    for (ParseObject country : ob) { 
     ParseFile image = (ParseFile) country.get("imageFile"); 
     ImageView iv=new ImageView(this); 
     imgl.DisplayImage(image.getUrl(), iv); 
      container.addView(iv); 
     i=i+1; 
     System.out.println("the urls are"+image.getUrl()); 

       } 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

還可以設置的LinearLayout的編號,並移除所有其他ImageViews:

<HorizontalScrollView 
android:id="@+id/horizontalScrollView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:orientation="horizontal" android:id="@+id/container"> 


    </LinearLayout> 
</HorizontalScrollView> 
+0

我不想手動將ImageViews數組設置爲5或任意數字。我希望ImageViews根據數據庫中的圖像自動創建。 – addy123

+0

你的圖像數組未使用檢查循環內的代碼 –

+0

檢查我編輯的ans –

0
ImageView iv = new ImageView(context_here); 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.wrap_content,LinearLayout.LayoutParams.wrap_content); 
iv.setLayoutParams(params); 
ll_your_main_container.addView(iv); 

通話,這是循環添加ImageView對象的數量,以主佈局。

1

在你的xml文件中,刪除ImageViews。給linearLayout一些id。

<HorizontalScrollView 
android:id="@+id/horizontalScrollView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 
<LinearLayout 
android:id="@+id/linearLayout" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 
</LinearLayout> 
</HorizontalScrollView> 

在你的活動中,定義這個線性佈局。

//像這樣

private LinearLayout linearLayout; 

然後,在onCreate

linearLayout = (LinearLayout) findViewById(R.id.linearLayout); 

,然後裏面的循環(ParseQuery一個),

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.WRAP_CONTENT, 
         LinearLayout.LayoutParams.WRAP_CONTENT); 
lp.setMargins(20, 10, 20, 10); //optional you can also set more margins and do lot of stuff here 
ImageView imageView = new ImageView(context); //context is the activity context say, this 
imageView.setLayoutParams(lp); 
imgl.DisplayImage(image.getUrl(), imageView); 
linearLayout.addView(imageView); 
相關問題