2

I am adding a Static and Dynamic text view simultaneously in android.but there is something wrong in the code..it is only adding the last element of array..Can You please help me to sort it out?

dynamicInput = (String[]) data;
        runOnUiThread(new Runnable() {
            public void run() {

                String[] string = { "Customer Mobile No :", "Nick Name :",
                        "Amount :", "Due date :" };
                LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputbiller);
                TextView dynamicText = new TextView(PayBiller_Activity.this);
                TextView staticText = new TextView(PayBiller_Activity.this);
                for (int i = 0; i < string.length; i++) {

                    staticText.setText(string[i]);

                    staticText.setTextColor(Color.BLACK);
                    staticText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);

                    dynamicText.setText(dynamicInput[i]);

                    dynamicText.setTextColor(Color.GRAY);
                    dynamicText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                }
                findViewById.addView(staticText);
                findViewById.addView(dynamicText);

            }
        });
4

4 回答 4

0

Try this:

dynamicInput = (String[]) data;
        runOnUiThread(new Runnable() {
            public void run() {

                String[] string = { "Customer Mobile No :", "Nick Name :",
                        "Amount :", "Due date :" };
                LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputbiller);

                for (int i = 0; i < string.length; i++) {
                    TextView dynamicText = new TextView(PayBiller_Activity.this);
                    TextView staticText = new TextView(PayBiller_Activity.this);
                    staticText.setText(string[i]);

                    staticText.setTextColor(Color.BLACK);
                    staticText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);

                   dynamicText.setText(dynamicInput[i]);

                    dynamicText.setTextColor(Color.GRAY);
                    dynamicText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);

                    findViewById.addView(staticText);
                    findViewById.addView(dynamicText);
                }


            }
        });
于 2013-08-20T06:13:57.023 回答
0

put this inside your for loop.

findViewById.addView(staticText);
findViewById.addView(dynamicText);
于 2013-08-20T06:15:08.753 回答
0

if you want to set all the array elements to your textViews then you must append it on each iteration. right now you are not doing it. hence you are getting only the last array element.

Note: also make sure your string Array dynamicInput and string array for staticText have same number of elements so that you wont get index out of bound exception or you might miss some elements while assigning it to TextView.

I have given some other name to your "string" array. its "staticStringArray."

dynamicInput = (String[]) data;
    runOnUiThread(new Runnable() {
        public void run() {

            String[] staticStringArray= { "Customer Mobile No :", "Nick Name :","Amount :", "Due date :" };
            LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputbiller);
            TextView dynamicText = new TextView(PayBiller_Activity.this);
            TextView staticText = new TextView(PayBiller_Activity.this);
            for (int i = 0; i < staticStringArray.length; i++) {

                //append each element of the array on each iteration. 
                staticText.setText(staticText.getText().toString() + staticStringArray[i] + "\n");

                staticText.setTextColor(Color.BLACK);
                staticText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);

                //append each element of the array on each iteration. 
                dynamicText.setText(dynamicText.getText().toString() + dynamicInput[i] + "\n");

                dynamicText.setTextColor(Color.GRAY);
                dynamicText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            }
            findViewById.addView(staticText);
            findViewById.addView(dynamicText);

        }
    });
于 2013-08-20T06:29:02.020 回答
0

Actually the problem is something different. What you are doing is that you are adding the elemnt of a array in a single TextView. And as you are doing it in a loop it is only taking the last element and fetching it in Text view. Do one thing take the ID of parent layout (Linear/relative).

Within the loop

create a Textview and add a value to it(the way you are doing like setText, setTextSize etc). And within the loop add it to its parent layout.

This will might help you.

于 2013-08-20T06:33:13.033 回答