2013-03-29 160 views
2

我正在執行HTTP請求並獲取結果。然後,我想通過這些結果中滾動,併爲每個結果如下: (RL =相對佈局)以編程方式安裝Android容器

enter image description here

甲相對佈局(RL在PIC)在內側一個TextView和ImageView的。然後,我想將所有這些「容器」添加到列表中,並將它們傳遞給解釋。

//inside onPostExecute - given a list 

List<RelativeLayout> containers = new ArrayList<RelativeLayout>(); 

for(i=0; i<alist.size();i++){ 
    RelativeLayout newLayout = new RelativeLayout(getApplicationContext()); 
    TextView tv = new TextView(getApplicationContext()); 
    ImageView iv = new ImageView(getApplicationContext()); 
    newLayout.addView(tv); 
    newLayout.addView(iv); 

    containers.add(newLayout); 
} 

otherClass.send(containers); 

我基本上想要以編程方式創建一個容器,在其中放入兩件東西,並將其發送給進一步處理。

(這是可能的,或者是有沒有更好的方式來做到這一點?)* EDITED

在此先感謝您的幫助,我已經花了很多時間在這已經


*我曾經嘗試這樣做,它會拋出試圖通過佈局的名單,我認爲


只是要增加更多的在這裏當一個例外:

public void send(List<RelativeLayout> containers) { 

     for(int i=0; i<containers.size(); i++){ 
      RelativeLayout rl = new RelativeLayout(mContext); 
      rl = containers.get(i); 
      icons_row.addView(rl); 
     } 
    } 

和異常

03-29 10:20:32.290: E/AndroidRuntime(29782): FATAL EXCEPTION: main 
03-29 10:20:32.290: E/AndroidRuntime(29782): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
03-29 10:20:32.290: E/AndroidRuntime(29782): at android.view.ViewGroup.addViewInner(ViewGroup.java:3618) 
03-29 10:20:32.290: E/AndroidRuntime(29782): at android.view.ViewGroup.addView(ViewGroup.java:3489) 
03-29 10:20:32.290: E/AndroidRuntime(29782): at android.view.ViewGroup.addView(ViewGroup.java:3434) 
03-29 10:20:32.290: E/AndroidRuntime(29782): at android.view.ViewGroup.addView(ViewGroup.java:3410) 

回答

1

Alrighty在這裏我們去。請記住,這是在HTTP請求之後完成的,所以它實現了一個異步任務,我只是忽略了這一點,因爲它與我的問題無關。

首先,創建3個事物列表。 1佈局列表(外部div像容器),1個文本視圖列表和1個圖像列表。在我的例子,我通過自己的所謂的商務類的列表循環:

private List<RelativeLayout> layoutContainers = new ArrayList<RelativeLayout>(); 
private List<TextView> textContainers = new ArrayList<TextView>(); 
private List<ImageView> imageContainers = new ArrayList<ImageView>(); 

.... 

@Override 
protected void onPostExecute(List<Business> blist){ 

    for(int i=0;i<blist.size();i++){ 

    RelativeLayout newLayout = new RelativeLayout(mContext); 

    TextView tv = new TextView(mContext); 
    ImageView iv = new ImageView(mContext); //// or getApplicationContext() 

    tv.setId(1); iv.setId(2); 

    newLayout.addView(tv); 
    newLayout.addView(iv); 
    icons_row.addView(newLayout); // this is another relative layout created via XML as standard 


    RelativeLayout.LayoutParams layoutParms = (RelativeLayout.LayoutParams)newLayout.getLayoutParams(); 
    RelativeLayout.LayoutParams textParms = (RelativeLayout.LayoutParams)tv.getLayoutParams(); 
    RelativeLayout.LayoutParams imageParms = (RelativeLayout.LayoutParams)iv.getLayoutParams(); 

    layoutParms.height = RelativeLayout.LayoutParams.MATCH_PARENT; 
    layoutParms.width = 175; 
    newLayout.setGravity(Gravity.CENTER_VERTICAL); 

    // Text Styling   
    textParms.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
    textParms.width = RelativeLayout.LayoutParams.MATCH_PARENT; 
    tv.setBackgroundColor(R.drawable.my_border); 
    tv.setPadding(1, 5, 1, 2); 
    tv.setTextColor(Color.GREEN); 
    tv.setGravity(Gravity.CENTER_HORIZONTAL); // center text 

    // Image Styling - background set deeper inside   
    imageParms.addRule(RelativeLayout.BELOW, tv.getId()); 
    imageParms.setMargins(35, 0, 0, 0); 
    imageParms.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
    imageParms.width = RelativeLayout.LayoutParams.WRAP_CONTENT; 

    // set the params 
    tv.setLayoutParams(textParms); 
    iv.setLayoutParams(imageParms); 
    newLayout.setLayoutParams(layoutParms); 

    // add them to the lists (created above) 
    textContainers.add(tv); 
    imageContainers.add(iv); 
    layoutContainers.add(newLayout); 
      } 

     bearingTextView = (TextView)findViewById(R.id.bearingTextView); 
     gps.calculateBearing(compass, bearingTextView ,blist, textContainers, imageContainers,layoutContainers); 

} // end on Post Execute 

我送他們通過我的GPS,然後把他們送到我的指南針。例如,要更改它們,更新文本,請執行以下操作...例如,上面介紹的calculateBearing方法...忽略其他值,只是容器。


public class Gps{ 
    private List<TextView> textContainers; 
    private List<ImageView> imageContainers; 
    private List<RelativeLayout> layoutContainers; 

... 

public void calculateBearing(Compass compass, TextView bearingText, List<Business> blist,     List<TextView> textContainers,List<ImageView> imageContainers,List<RelativeLayout> layoutContainers){ 
    this.textContainers = textContainers; 
    this.imageContainers = imageContainers; 
    this.layoutContainers = layoutContainers; 

    //eg. change the text 
     for(int i=0; i<textContainers.size(); i++){ 
     textContainers.get(i).setText("This is text field: " + Integer.toString(i)); // will return, 1,2,3 etc depending on whatever textField is there 
    } 

    } 
} 

請記住,我叫從onPostExecute這些外部方法,所以用戶界面將被更新。如果我不清楚任何事情,請讓我知道