views:

601

answers:

1

Hi

I'm stuck on a problem and if possible I will need help from the community. I'm not looking for a ready-made solution, but something which would help me to produce the result.

I'm looking for a way to produce a dynamic activity based on a JSONArray object. Here an example of a JSONArray object:

[ { "name": "my checkbox name",
    "type": "checkbox",
    "value": "one,two,three"
  }
  { "name": "my edit text",
    "type": "text",
    "value": ""
  }
  ...]

This JSONArray could be totally random. It could have 2 text views, 3 select menus, 1 text view and so on.

The goal is to iterate over this JSONArray and create the appropriate elements within my android code.

To produce the result I've thought of a simple switch which would render one by one my different JSONArray to an android widget.

But after that how could I have access of every property of every widget rendered?

Edit: I need as well to assign an event listener on some widget as taking the GPS coordinated...

Thank you.

Edit: this is a JSONArray not a JSONObject...

+1  A: 

When you are generating the widget programmatically, you can assign them your own id.

For example:

Button btn = new Button(this);
btn.setId(myBtnId);

And later in your code, you can reference to that button with findViewById(myBtnId).

ccheneson
Thank you for the quick answer, I will try tomorrow night. Glad to avoid to manipulate dynamics var.
ee_vin
It's working like a charm; )
ee_vin
It looks like you found a good solution, but I thought I would alert you to a project I just wrapped up that consisted of a similar challenge. I documented the approach and made the source available here: http://labs.makemachine.net/2010/04/android-form-generator/
jeremynealbrown