Search code examples
androidandroid-edittextandroid-spinner

How to save string written in EditText to string - android studio


I'm developing an application that allows user to add his own word, it's category. I start with a spinner that has the words available before in sqlite db and Add new word. When he clicks on Add new word, the spinner changes into EditText box that allows him to enter a new word. Whenever I try to save the text written in Edittext to string it shows that string is defined as "Add new word" and not the value entered by the user. What should I do?

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(parent.getId()==R.id.spinword)
    {
        editword=parent.getItemAtPosition(position).toString();
        if(editword.equals("Add new word"))
        {
            spinwrd.setVisibility(spinwrd.INVISIBLE);
            edtwrd.setVisibility(edtwrd.VISIBLE);
            flag = 1;
        }
        if(flag == 1)
        {
            editword.replace(editword,edtcat.getText().toString());
        }

    }

    if(parent.getId() == R.id.spincat) {
        editcategory = parent.getItemAtPosition(position).toString();

        if (editcategory.equals("Add new category")) {
            spincat.setVisibility(spincat.INVISIBLE);
            edtcat.setVisibility(edtcat.VISIBLE);
            editcategory = edtcat.getText().toString();
        }
    }
}

I tried equals method and replace method but in vain.


Solution

  • The answer to this question is as follows: First, replacing, clearing, or any change depends on the position of the line meaning that: In the code above, I was trying to capture the text written by the user but at that point of execution when user chooses "Add new word" the text becomes it. The solution to this problem is to place the line of code editword.replace(editword,edtcat.getText().toString()); when this function is finished to be sure that the user has entered some text.