3

我正在执行 HTTP 请求并获得结果。然后我想滚动浏览这些结果并为每个结果创建以下内容:(RL = 相对布局)

在此处输入图像描述

一个相对布局(图中的 RL),里面有一个 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);

我本质上想以编程方式创建一个容器,在其中放入两件东西,然后将其发送出去以进行进一步处理。

(这可能吗,或者有更好的方法吗?)*已编辑

在此先感谢您的帮助,我已经花了很多时间


*我已经尝试过了,它在尝试传递我认为的布局列表时会引发异常


只是要在这里添加更多:

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)
4

1 回答 1

2

好的,我们开始吧。请记住,这是在 HTTP 请求之后完成的,所以它正在实现一个异步任务,我只是省略了那一点,因为它与我的问题无关。

首先,创建 3 个事物列表。1 个布局列表(像容器一样的外部 div)、1 个文本视图列表和 1 个图像视图列表。对于我的示例,我遍历了我自己的名为 business 的类的列表:

 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 方法为例...忽略其他值,仅 Containers。


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 将被更新。如果我不清楚任何事情,请告诉我

于 2013-03-29T18:04:45.443 回答