Teeranai.P
Teeranai.P

Reputation: 119

How to reference object when add view by programatically?

I have JSON data and would like to add them to EditText

JSON

{
   "Data":[
       {
         "Name":"benz",
         "Display":"slk200",
         "Value":"1"
      },
      {
         "Name":"bmw",
         "Display":"z4",
         "Value":"2"
      },
      {
         "Name":"toyota",
         "Display":"supra",
         "Value":"3"
      },
      {
         "Name":"honda",
         "Display":"civic",
         "Value":"4"
      }
   ]
}

Java

LinearLayout placeHolder = (LinearLayout) findViewById(R.id.place_holder);
for (Data f : dataList) {
    EditText ed = new EditText(context);
    ed.setHint(f.display);
    placeHolder.addView(ed);
}

If I would like to reference the EditText object after click some button What should I do?

Upvotes: 0

Views: 98

Answers (3)

anubhavashok
anubhavashok

Reputation: 532

for the part on the button, you can implement an onClickListener. If you have already created a button in your layout.
Reference it with:

Button b1 = (Button)findViewById(R.id.<buttonid>);

Then you can create an onClickListener for the button, which listens for a click.

b1.setOnClickListener(new OnClickListener(){
    public void onClick(View v)
    {
            EditText e1 = (EditText) findViewById(R.id.<edittextid>);
            //get records
            e1.setText(<next record>);
    }

});

Upvotes: -1

Tigger
Tigger

Reputation: 9120

EditText inherits from TextView, which inherits from View. This means, you can use the view.setId(int) to assign an id to the dynamic EditText view.

You can then call findViewById(EditText id you gave it) to get the EditText view.

For example, assigning an id:

EditText ed = new EditText(context);
ed.setId(99);

And to reference the new EditText view:

EditText tmpEd = (EditText) findViewById(99);
tmpEd.setText("a test string");

Upvotes: 1

Digital Alchemist
Digital Alchemist

Reputation: 2332

Parsing Json request,

String jsonString;
JSONObject reader = new JSONObject(jsonString);

JSONObject dataJSON  = reader.getJSONObject("Data");
displayText = dataJSON.getString("Display");

EditText ed = new EditText(context);
ed.setHint(displayText );

Following links might b useful to u

1- TutorailzPoint-JsonParsing

2- AndroidHive-Json

Upvotes: 1

Related Questions