Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android container programmatically

I am performing an HTTP request and getting results. Then I want to scroll through these results and create the following for each result: (RL = Relative Layout)

enter image description here

A Relative Layout (RL in the pic) with a TextView and an ImageView on the inside. I then want to add all of these 'containers' to a List and pass them off for interpretation.

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

I essentially want to programmatically create a container, put two things in it, and send it off for further processing.

(Is this possible, or is there a better way to do this?) *EDITED

Thanks in advance for any help, I have spent a lot of time on this already


*I have tried this, it throws an exception when trying to pass the list of layouts I think


Just going to add more here:

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

And the exception

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)
like image 385
shanehoban Avatar asked Dec 18 '25 20:12

shanehoban


1 Answers

Alrighty here we go. Keep in mind, this is done after a HTTP Request, so it's implementing an Async Task, I just left out that bit as it isn't relevant to the question I had.

First, create 3 lists of things. 1 List of layouts (the outer div like container), 1 list of text views, and 1 list of imageviews. For my example, I looped through a list of my own class called 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

I sent them through to my GPS which then sent them to my Compass. To make changes to them, for example, update the text, you do the following... Taking for example, the calculateBearing method stated above... Ignoring the other values, just the 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
    }

   }
 }

Keep in mind that I call these external methods from onPostExecute, so the UI will be updated. If I wasn't clear on anything let me know

like image 64
shanehoban Avatar answered Dec 21 '25 16:12

shanehoban