Home > Net >  Get the text from an EditText created dynamically by a function
Get the text from an EditText created dynamically by a function

Time:04-12

I am working on an Android application using Java. I'm using a spinner where it offers different options for a user to select and then I try to update the UI according to the selection, adding more user input for additional information or removing some of it. Everything works until I call the saveInfo() function which is supposed to print the text inside this new text input field that has been created with the addInput() function. Every time I print it, it returns null ("java.lang.NullPointerException"). Would anyone have an idea on how I can use a function to create a new EditText input and at the same time be able to retrieve information from it? If not, is there another solution someone would recommend to dynamically add multiple EditTexts to the screen? Thank you!

Here is my code:

public class UserInfo extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private String[] bankNames={"Teacher","Student","Parent","Worker","Another"};
    //Commum variables:
    private EditText nameInput;
    //Teacher:
    private EditText inSchoolTitleInput;
    //Parent:
    //...

    private EditText neutralInput;
    private String neutralInputText;

    //String from spinner:
    private String selectedName;

    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        //Getting the instance of Spinner and applying OnItemSelectedListener on it
        Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
        layout = findViewById(R.id.linearLayout);
        spin.setOnItemSelectedListener(this);

        //Creating the ArrayAdapter instance having the bank name list
        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spin.setAdapter(aa);
    }

     @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        selectedName = bankNames[position];
        addFields();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {}

    //Checks what was selected from spinner and updates the screen
    public void addFields() {
        addCommonFields();
        switch (selectedName) {
            case "Teacher": addTeacherFields();
                            break;
            //...
        }
    }

    public void addCommonFields() {
        layout.removeAllViewsInLayout();
        addInput(nameInput, "Lucas Slepetys");
    }

    private void addTeacherFields() {
        addInput(inSchoolTitleInput, "Math");
    }

    public void addInput(EditText field, String hint) {
        neutralInput = field;
        neutralInput = new EditText(this);
        neutralInput.setHint(hint);
        layout.addView(neutralInput);
    }
    
    public void saveInfo(View v) {
        switch (selectedName) {
            case "Teacher":
                System.out.println(neutralInput.getText().toString());
                break;
            //...
        }
     }
}

Thank you!

CodePudding user response:

Better you create a HashMap with key value like

HashMap map = new HashMap<String,EditText>(); map.put("edit_text_type",editText Object Reference)

so whenever you create a new dynamic edit text add also inside this hash map and when you want fo retrieve the value of particular HashMap same key you need to get from map like

map.get("edit_text_type").gettext().toString();

Below is the reference example

public void addInput(EditText field, String hint) {

    neutralInput = new EditText(this);
    neutralInput.setHint(hint);
    layout.addView(neutralInput);

   **map.put(hint,field);**
}
  • Related