Kruise Kruise - 1 month ago 17
Android Question

EditText Not Reading Input in Android

The problem in EditText is, it is not returning the input value.I am using that input value to check condition.

I am giving the correct input so that i checks the if condition and go to next activity but
it always goes to else condition.

Please check the code below, i had commented the problem.

Here is the snippet

public class BuildWord extends Activity
{
String word = "word";
String finished = "Word Built";

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.buildword);

EditText get = (EditText)findViewById(R.id.dataToSend);
String getdata = get.getText().toString(); //Here i am getting Data from EditText

displayIntentData();

if (getdata.equals("word"))

//Here i am checking with "word" but it goes to else condition.I am typing "word" only
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,MainActivity.class);
intent.putExtra("key", word);
startActivity(intent);

}
});
}
else
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
}

@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
displayIntentData();
}

private void displayIntentData()
{
Intent intent = getIntent();
TextView tv = (TextView)findViewById(R.id.intentData1);
Bundle extras=intent.getExtras();

if(extras!=null)
{
tv.setText("Data received: "+extras.getString("key"));
}
else
{
tv.setText("No extradata received");
}
}


}

Answer

you are getting the value in the EditText field ("get") immediately after inflating the view. you couldn't have time to enter any text yet, so it is empty. you then check if "getData" contains "word" (it doesn't!) therefore you're hitting the ELSE.