set onClick, getText, etc for created views-Collection of common programming errors

This is a logic based problem where I will need a small sample of code or an idea supplied for an answer.

I am creating a U.I. programmatically from a JSON response. This app will load in an unknown amount of questions and answers. I’m using loops and conditional statements to create the Views for the U.I. and I am using an AsyncTask for most of the heavy lifting.

Well the problem I can’t seem to figure out is: How will I give an unknown amount of views unique id’s so that I can use them.

I am providing all the fragment code so you know exactly whats going on:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_assessment, container, false);
        ab = getActivity().getActionBar();
        infoList = new ArrayList();
        new Load().execute();

        return view;
    }

    class Load extends AsyncTask {
        private ProgressDialog pDialog;
        JSONParser jParser = new JSONParser();
        JSONArray questions = null;

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading questions. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            // getting JSON string from URL
            String componentName = (String) ab.getSelectedTab().getText();
            companyName = model.getcName();
            projectName = model.getpName();
            List nameValuePairs = new ArrayList(3);
            nameValuePairs.add(new BasicNameValuePair("company", companyName));
            nameValuePairs.add(new BasicNameValuePair("project", projectName));
            nameValuePairs.add(new BasicNameValuePair("component",
                    componentName));

            JSONObject json = jParser.makeHttpRequest(url, "POST",
                    nameValuePairs);
            // Check your log cat for JSON response
            Log.d("All Questions: ", json.toString());
            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    Log.v("RESPONSE", "Success!");
                    // products found: getting Array of Questions
                    questions = json.getJSONArray(TAG_QUESTIONS);

                    // looping through All Questions
                    for (int i = 0; i < questions.length(); i++) {

                        JSONObject c = questions.getJSONObject(i);

                        // Storing each JSON item in variable
                        String name = c.getString(TAG_NAME);
                        String field = c.getString(TAG_FIELD);
                        String value = c.getString(TAG_VALUE);

                        // creating new HashMap
                        HashMap map = new HashMap();

                        // adding each child node to HashMap key => value
                        map.put(TAG_NAME, name);
                        map.put(TAG_FIELD, field);
                        map.put(TAG_VALUE, value);

                        infoList.add(map);
                    }

                } else {
                    // no products found
                    Log.v("ERROR", "No JSON for you!");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String string) {
            // dismiss the dialog
            pDialog.dismiss();

            for (int i = 0; i < infoList.size(); i++) {
                // get HashMap
                HashMap map = infoList.get(i);
                // if the answer should be a radio button, inflate it
                if (map.get(TAG_FIELD).equals(r)) {
                    Log.v("RESPONSE", "About to create a radio button");
                    // find
                    LinearLayout content = (LinearLayout) view
                            .findViewById(R.id.add);
                    // create
                    ArrayList value = new ArrayList();
                    TextView tv = new TextView(getActivity());
                    RadioGroup rg = new RadioGroup(getActivity());
                    rg.setOrientation(RadioGroup.HORIZONTAL);
                    RadioButton rb = new RadioButton(getActivity());
                    RadioButton rb2 = new RadioButton(getActivity());
                    LinearLayout ll = new LinearLayout(getActivity());
                    // set
                    rb.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    rb2.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    ll.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    String s = map.get(TAG_VALUE);
                    for (String parts : s.split("\r\n")) {
                        value.add(parts);
                    }
                    rb.setText(value.get(0));
                    rb2.setText(value.get(1));
                    tv.setText(map.get(TAG_NAME));
                    ll.setOrientation(LinearLayout.HORIZONTAL);                 
                    // add
                    rg.addView(rb);
                    rg.addView(rb2);
                    ll.addView(tv);
                    ll.addView(rg);
                    content.addView(ll);
                }
                // create an EditText field
                else if (map.get(TAG_FIELD).equals(et)) {
                    Log.v("RESPONSE", "About to create an EditText");
                    // find
                    LinearLayout content = (LinearLayout) getActivity()
                            .findViewById(R.id.add);
                    // create
                    TextView tv = new TextView(getActivity());
                    EditText et = new EditText(getActivity());
                    LinearLayout ll1 = new LinearLayout(getActivity());
                    // set
                    tv.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    et.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    ll1.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    tv.setText(map.get(TAG_NAME));
                    ll1.setOrientation(LinearLayout.HORIZONTAL);                    
                    // add
                    ll1.addView(tv);
                    ll1.addView(et);
                    content.addView(ll1);
                }
                // create CheckBox
                else if (map.get(TAG_FIELD).equals(cb)) {
                    Log.v("RESPONSE", "About to create a CheckBox");
                    // find
                    LinearLayout content = (LinearLayout) getActivity()
                            .findViewById(R.id.add);
                    // create
                    TextView tv = new TextView(getActivity());
                    CheckBox cb = new CheckBox(getActivity());
                    LinearLayout ll2 = new LinearLayout(getActivity());
                    // set
                    tv.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    cb.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    ll2.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    tv.setText(map.get(TAG_NAME));
                    ll2.setOrientation(LinearLayout.HORIZONTAL);                    
                    // add
                    ll2.addView(tv);
                    ll2.addView(cb);
                    content.addView(ll2);

                }
                // Create Spinner
                else if (map.get(TAG_FIELD).equals(dm)) {
                    Log.v("RESPONSE", "About to create a Drop Down Menu");
                    // find
                    LinearLayout content = (LinearLayout) getActivity()
                            .findViewById(R.id.add);
                    // create
                    TextView tv = new TextView(getActivity());
                    LinearLayout ll3 = new LinearLayout(getActivity());
                    ArrayList spinnerArray = new ArrayList();
                    ArrayAdapter aa = new ArrayAdapter(
                            getActivity(),
                            android.R.layout.simple_spinner_dropdown_item,
                            spinnerArray);
                    Spinner spinner = new Spinner(getActivity());
                    // set
                    tv.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    spinner.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    ll3.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    tv.setText(map.get(TAG_NAME));
                    String s = map.get(TAG_VALUE);
                    for (String parts : s.split("\r\n")) {
                        spinnerArray.add(parts);
                        System.out.println(parts);
                    }
                    spinner.setAdapter(aa);
                    ll3.setOrientation(LinearLayout.HORIZONTAL);                    
                    // add
                    ll3.addView(tv);
                    ll3.addView(spinner);
                    content.addView(ll3);
                } else if (map.get(TAG_FIELD).equals(fu)) {
                    Log.v("RESPONSE", "About to create an ImageView");
                    // find
                    LinearLayout content = (LinearLayout) getActivity()
                            .findViewById(R.id.add);
                    // create
                    TextView tv = new TextView(getActivity());
                    LinearLayout ll4 = new LinearLayout(getActivity());
                    ImageButton ib = new ImageButton(getActivity());
                    int ibd = 0;
                    ibd = R.drawable.ic_menu_camera;
                    // set
                    tv.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    ll4.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    ib.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    tv.setText(map.get(TAG_NAME));
                    ib.setImageResource(ibd);
                    ll4.setOrientation(LinearLayout.HORIZONTAL);                    
                    //add
                    ll4.addView(tv);
                    ll4.addView(ib);
                    content.addView(ll4);
                }
            }
        }
    };

I probably went about this the wrong way to begin with, however this is where I’m at. I will need to give the EditText an id to grab user input, I will need to give the ImageButton and id so it can perform events with the onClick function, and so on and so forth. You get the point I’m sure. So how would one of you tackle this problem?

  1. It’s really hard to answer this question. It really depends on what you want to do with the captured data. In any case, you don’t have to generated IDs for the elements, you can set new event listeners using Anonymous classes. Then, each of these listeners can save your data to somewhere depending on your key (name?).

Originally posted 2013-11-10 00:16:11.