Mikołaj Mularczyk
Mikołaj Mularczyk

Reputation: 969

Android Retrieve data from EditText

I am new to android programming, so please be forgiving. This is my first app, its purpose is to find strings typed in the search widget by the user in txt placed on sd card. I almost finished it, but my last goal was to add a feature that would allow the user to decide how many answers he wants to get after searching. So I added an EditText field where i can type a number. And that is my problem: I can't retrieve data that I typed in EditText field, and I don't know why. This is my onCreate.

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

   EditText editText = (EditText) findViewById(R.id.enter_a_number); // I think this is the firs part of my problem...

    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        int number = Integer.valueOf(editText.getText().toString()); // ...and that is the second
        do_my_search(query, number);
    }
}

and do_my_search method:

   public void do_my_search(String query, int number) {
      //Find the directory for the SD Card using the API
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"fragment.txt");

    try {

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "windows-1250"),8192);
        String line;
        String[] text = new String[5];
        int i = 0;
        TextView textView1 = (TextView)findViewById(R.id.textView1);
        textView1.setMovementMethod(new ScrollingMovementMethod());

        while ((line = br.readLine()) != null) {
            if(line.toLowerCase().contains(query.toLowerCase()) == true && i < number){
                text[i] = i + 1 + "." + line + "\n";
                i++;
            }
        }
        for(i = 0; i < number ; i ++){
            if(text[i] == null)
                text[i] = "";
        }
        StringBuilder builder = new StringBuilder();
        for (String value : text) {
            builder.append(value);
        }
        String final_string = builder.toString();
        Spannable wordtoSpan = new SpannableString(final_string);
        for(i = 0;; ){
            int k = final_string.indexOf(query,i);
            if (k == -1)
                break;
            wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), k, k + query.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            i = k + query.length();
        }

        textView1.setText(wordtoSpan);
        br.close();
    }
    catch (IOException e) {
        //You'll need to add proper error handling here
    }
}

Everything but the text field works fine. I have browsed some similiar threads and I think my problem is that the variable number gets the value before I type anything in the field, it always picks the default text, even if it is showing something different. I know that probably there will be some errors in my code, but right now I'm interested in solving this particular issue: what should I do to make my variable number pick the value that i type in my edittext field? And is it possible without adding buttons or TextWatcher?

Upvotes: 0

Views: 9949

Answers (5)

pavithra
pavithra

Reputation: 105

EditText edtemail;
String email;

... then inside the oncreate method

edtemail=(EditText)findViewById(R.id.text_email);
email=edtemail.getText.toString();

Upvotes: -1

Omar Faroque Anik
Omar Faroque Anik

Reputation: 2609

From this line,

 EditText editText = (EditText) findViewById(R.id.enter_a_number);

You just got a view, EditText view. Now you need to get the data, user has entered. To do this, use the following line after that

 String eText=editText.getText().toString();

Upvotes: 1

Rishabh Srivastava
Rishabh Srivastava

Reputation: 3745

Use edittext.getText().toString() to get data from EditText. It returns a string which you can store in a string variable.

Upvotes: 0

Mena
Mena

Reputation: 48444

You need to add a listener to your EditText, in order to retrieve its value once changed.

Your "first problem" should not be an actual problem (retrieving the EditText by id as a child View of the main Layout), as long as the EditText is declared in your main Layout xml, and has that id assigned.

The simplest way to solve your issue is to declare a String variable as instance property of your main Activity. Let's call it theText and assign it an empty String.

Note that you don't really need an instance variable, you might want to use the value of the text change directly - it really boils down to whether you need to re-use that after processing it with your search method or not. In the example below, I assume you have an instance String variable theText declared and assigned in your Activity.

Add the listener to your EditText as such:

editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        // we assign "theText" a value here
        theText = s.toString();
        // we test it here (assuming your main Activity is called "MainActivity"
        Toast.makeText(MainActivity.this, theText, Toast.LENGTH_LONG).show();
        // you can do whatever with the value here directly (like call "do_search"), 
        // or launch a background Thread to do it from here
    }
});

Now once the text has been changed by the user, the value of theText will be updated to that text and you can use it elsewhere in your code.

Upvotes: 1

Jony-Y
Jony-Y

Reputation: 1579

I cant find where you get the text entered.... to receive text from the edittext you need

String myText=edittext.getText().toString()

well actually you just need edittext.getText().toString() but you get the point... i trust you know where to put it once you get it.

and then use it to whatever purpose you want... then you can also get it as an int with Integer.parseInt(myText) if you need a Int from the string.

Upvotes: 1

Related Questions