1. Download our Official Android App: Forums for Android!

Apps I have a problem that I have been stuck on for a while

Discussion in 'Android Development' started by Ben Richard, Mar 31, 2016.

  1. Ben Richard

    Ben Richard Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 22, 2016

    Mar 22, 2016
    7
    0
    15
    Male
    What I am trying to do is get user input as a editText and output it to that amount so if the user enters 9 it outputs 9 textbox

    here is the code:
    questionAndTimer class:
    public void onClick(View view) {
    //Create Intent object which moves from QuestionAndTimer class
    //to SendQuestion class
    Intent intObj = new Intent(QuestionAndTimer.this, SendQuestion.class);
    //Set the user entered question in value name which will be
    //used in GreetingActivity class

    intObj.putExtra("HOW", name2);

    //Start sendQuestion
    startActivity(intObj);

    if (howManyQuestions != null && howManyQuestions.getText().length() != 0) {//check if user enters a numeric attribute
    name2 = howManyQuestions.getText().toString();

    }
    else if(howManyQuestions.getText().toString().isEmpty()) {//check if user doesn't enter a numeric attribute
    Toast.makeText(this, "please enter how many answers you are looking for..", Toast.LENGTH_SHORT).show();
    return ;

    }

    sendQuestion class:

    String howMany = (String) intename.getSerializableExtra("HOW");
    if(howMany == "9") {

    answers.setText("" + howMany);
    answers.setText("" + howMany);
    answers.setText("" + howMany);
    answers.setText("" + howMany);
    answers.setText("" + howMany);
    answers.setText("" + howMany);
    answers.setText("" + howMany);
    answers.setText("" + howMany);
    answers.setText("" + howMany);
    }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    If this is the same question as you asked last time, have you had a look at the links I posted on how to dynamically add components to your view?

    In an app, the normal situation is to have your screen layout statically defined in the layout XML file.
    If you wish to add or remove views e.g. EditText while the application is running, then you need to understand how to add Views to an existing ViewGroup. You can get many useful links and examples on how to do this by Googling "android dynamically add view" or similar words.

    I've just noticed something else in your code. This will never do what you expect

    Code (Text):
    1.  
    2. String howMany = (String) intename.getSerializableExtra("HOW");
    3. if(howMany == "9") {
    4.  
    The 'if' statement condition 'howMany == "9"' will always evaluate to false because you are using object comparison. It's a common gotcha in Java that catches out many beginners.
    To compare two Strings for equivalence, what you need to do is use

    Code (Text):
    1.  
    2. if (howMany.equals("9")
    3.  

    But let's get back to your problem. In your SendQuestion class, have you tried to dynamically add an EditText view to the main view? What code did you write for that? Were there any specific problems you had?
     
    #2 LV426, Apr 1, 2016
    Last edited: Apr 1, 2016
    scary alien likes this.

Share This Page

Loading...