2013-03-29 3 views
2

HTTP 요청을 수행하고 결과를 가져 오는 중입니다. (RL = 상대 레이아웃)프로그래밍 방식으로 안드로이드 컨테이너

enter image description here

합니다 (그림에서 RL) 상대 레이아웃 내부에 텍스트 뷰와 이미지 뷰와 : 그럼 이러한 결과를 스크롤하고, 각 결과에 대해 다음을 만들려면 . 그런 다음이 모든 '컨테이너'를 목록에 추가하고 해석을 위해 전달하려고합니다.

//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); 

본질적으로 컨테이너를 프로그래밍 방식으로 만들고 두 가지를 넣은 다음 추가 처리를 위해 보내려합니다.

(이 가능하거나,이 작업을 수행 할 수있는 더 좋은 방법?있다) * 어떤 도움을, 나는이 이미


에 많은 시간을 보냈습니다 미리

감사 편집 됨

:이 시도 *, 내가

그냥 여기에 더 추가하려고 생각 레이아웃의 목록을 통과하려고 예외가 발생합니다
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 요청이 끝나면 비동기 작업을 구현하므로이 질문은 내가 갖고있는 질문과 관련이 없으므로 그만 둡니다.

먼저 세 가지 목록을 만듭니다. 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에서 이러한 외부 메소드를 호출 명심하십시오, 그래서 UI가 업데이트됩니다. 아무 것도 알려주지 않았다면

관련 문제