androidGenX
androidGenX

Reputation: 1148

Access Dynamic text filed values android

I have created some dynamic EditText fileds and a button using foreach loop, code is below:

void callDynamicView(){
        String configText="Building Name, S, 10; No: of Floors, I, 3; Area of Plot, D, 10; Location(X), L, 15.3;  Location(Y),  L, 15.3; Photo, P, 10";

        String[] splits = configText.split(";");
        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);

        TextView tv = new TextView(this);
        tv.setText("Survey");
        tv.setTextColor(Color.rgb(200,0,0));
        ll.addView(tv);



        for(String splitSemi: splits){

            String[] splitsCommas = splitSemi.split(",");
            int len = splitsCommas[1].length();

            //Toast.makeText(getBaseContext(), len, Toast.LENGTH_SHORT).show();
            if(splitsCommas[1].equals("P")){
                Toast.makeText(getBaseContext(), "Keri", Toast.LENGTH_SHORT).show();
                Button btn = new Button(this);  
                btn.setText("Photo");
                ll.addView(btn);
                //this.setContentView(sv);
            }           
            else{

                //Toast.makeText(getBaseContext(), "else", Toast.LENGTH_SHORT).show();
                TextView txtv = new TextView(this);
                txtv.setText(splitsCommas[0]);
                ll.addView(txtv);

                EditText et = new EditText(this);
                ll.addView(et);

                }


        }

        Button btn = new Button(this);
        btn.setText("Submit");
        ll.addView(btn);
        this.setContentView(sv);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

        //  Toast.makeText(getBaseContext(), et.getText().toString(), Toast.LENGTH_SHORT).show();

            }
        });


    }

I want to access all entered values in button click. But I am unable to get the values. I have checked varios possibilities. I am new to creating UI dynamically . Can help me guys?

Upvotes: 2

Views: 682

Answers (6)

DSS
DSS

Reputation: 7259

You could do this:

  1. On each creation of the TextView or EditText, assign an id to the view.
  2. Add the TextView or EditText to a list of the same type, ie. List<EditText>
  3. On Button click, get the values of the TextView or EditText added into the list.

as like:

for(int i=0;i<arrayListOfViews.size();i++) {
     EditText editObj = arrayList.get(i);
     String val = editObj.getText().toString();
}

Hope that helps!

Upvotes: 1

Hamid Shatu
Hamid Shatu

Reputation: 9700

Set EditText's id as follows...and store those ids in a List to retrieve them later.

 List<Integer> editTextIdList = new ArrayList<Integer>();
 int id = 0;

 for(String splitSemi: splits){

        String[] splitsCommas = splitSemi.split(",");
        int len = splitsCommas[1].length();

        //Toast.makeText(getBaseContext(), len, Toast.LENGTH_SHORT).show();
        if(splitsCommas[1].equals("P")){

             .....
        }           
        else{

            //Toast.makeText(getBaseContext(), "else", Toast.LENGTH_SHORT).show();
            TextView txtv = new TextView(this);
            txtv.setText(splitsCommas[0]);
            ll.addView(txtv);

            EditText et = new EditText(this);
            et.seId(id);
            editTextIdList.add(id);
            ll.addView(et);

            id++;

        }
    }

In OnClickListener...create EditText instance using those ids and get their text as follow...

   btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            for(int i = 0; i < editTextIdList.size(); i++) {

                EditText editText = (EditText)findViewById(editTextIdList.get(i));
                String text = editText.getText().toString();

            }

           // Toast.makeText(getBaseContext(), et.getText().toString(), Toast.LENGTH_SHORT).show();

        }
    });

Upvotes: 3

praveen
praveen

Reputation: 420

setId to your editText and onClick of button do:

EditText editText =(EditText) context.findViewById(id);
String result = editText.getText().toString();

or use

ArrayList<EditText> editTextList = new ArrayList<EditText>();

in for loop add editText editTextList.add(i,new EditText(context)); for getting values use

editTextList.get(i).getText().toString();

Upvotes: 1

Kunu
Kunu

Reputation: 5134

Instead of declaring your Button, TextView and EditText in your loop , declare it above your onCreate() method. And you can initialize it anywhere you want.

public class YourClass(){
        TextView tv;
        Button btn;
        EditText edt;

        /* your onCreate() and all other codes here*/
        .....................
        .....................

        public void callDynamicView(){
           /* Initialize and create your view here */
        }
}

and you can directly access the TextView,Button and EditTextanywhere in your class

Upvotes: 1

QuokMoon
QuokMoon

Reputation: 4425

String strtextview=txt.gettext. toString() ;

String strbtnText=btn.gettext. toString();

Upvotes: 1

user2060383
user2060383

Reputation: 989

Just declare following variables at class level,

TextView tv;
TextView txtv;  
EditText et;

Upvotes: 1

Related Questions