0

I have two methods:

public View addNewLinearLayout(Context context) {
    LinearLayout linearLayout = new LinearLayout(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params.height = 120;
    linearLayout.setLayoutParams(params);
    linearLayout.setBackgroundColor(Color.RED);
    linearLayout.setGravity(Gravity.TOP);
    linearLayout.addView(buttonsGenerator(context));
    return linearLayout;
}
public View buttonsGenerator(Context context){
    ListView lst;
    lst = new ListView(context);

    Button button = new Button(context);

    return lst;
}

In addNewLinearLayout I add layout to view. In buttonsGenerator I wnat to add some buttons to list and add this list with buttons in my layout from addNewLinearLayout. How I can create list of buttons and add them to linear using method buttonsGenerator?

2
  • you can't do that. a listview is made to use a listadapter. for what you want to do, use a vertical LinearLayout Commented Feb 12, 2013 at 8:11
  • so I can't create separately method when I add buttons and after that send list or array to second method buttons whoch I want to add? I can solve that problem use second method to send to first number of button which I wnat to add, but I try add buttons by second method Commented Feb 12, 2013 at 8:14

1 Answer 1

1

For example:

public View addNewLinearLayout(Context context) {
    LinearLayout linearLayout = new LinearLayout(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params.height = 120;
    linearLayout.setLayoutParams(params);
    linearLayout.setBackgroundColor(Color.RED);
    linearLayout.setGravity(Gravity.TOP);

    List<View> components = getButtons(context);
    for(View component : components) {
       linearLayout.addView(component);
    }

    return linearLayout;
}

public List<View> getButtons(context) {
    List<View> buttons = new ArrayList<View>();
    for(int i = 0; i < 10; i++) {
       buttons.add(createButton(context));
    }
    return buttons;
}

public View createButton(Context context){
    ListView lst;
    lst = new ListView(context);

    Button button = new Button(context);

    return lst;
}

but better use a listview instead of this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.