How do I set the value of an EditText

by Lovedumplingx » Thu, 19 Mar 2009 22:43:35 GMT


Sponsored Links
 Ok...so I've scoured and scoured and played and fiddled but I can't
figure it out.

I want to allow the user to set preferences for an application and I
want the preferences to be displayed in the EditText area if/when they
come back to change them again.

In my head I'm thinking I would be able to use setText() but no...that
takes a CharSequence and I have a string and don't know how to make a
CharSequence (which according to what I've read is supposed to be a
read-only thing anyway).

So...does anyone know how to put text into an EditText field without
relying on the XML?  I really want to do this via application
preferences.

Thanks.
--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Will » Fri, 20 Mar 2009 06:35:41 GMT


 Cast the String to a CharSequence.

String x = "foo";
EditText ET;
ET.setText((CharSequence) x);




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How do I set the value of an EditText

by Isaac Waller » Fri, 20 Mar 2009 12:28:20 GMT


 A String _is_ a CharSequence. There is no need for a cast.





--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Sat, 21 Mar 2009 04:00:59 GMT


 That's what I thought too but I crash the task every time the activity
that contains this code is started:

EditText userText = (EditText) findViewById(R.id.userText);
userText.setText(userParam);

This is the basic way I thought to have the value set but it crashes
every time.





--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Tseng » Sat, 21 Mar 2009 17:57:05 GMT


 An error message would be helpful.... Otherwise it's hard to help you,
other than "guessing" what could be the reasons for it.

Maybe you haven't set the ID correctly, using a wrong ID (which
doesn't even exist in the XML Layout file) or you're using
setContentView/setView after you have using "findViewById". This could
throw up a NullPointer Exception when you try to access (because
findViewById returns 0 if no layout has been set or the View with this
ID was not found).







--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by sm1 » Sun, 22 Mar 2009 00:28:11 GMT


 i use

  myEditText.setText("hello", TextView.BufferType.EDITABLE);

works fine
serge







--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Mon, 23 Mar 2009 22:55:06 GMT


 I get a NullPointer Exception when I use the Debug tool.

So you're saying that I haven't successfully read the View probably?
Ok...that's a great starting point.







--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Thu, 26 Mar 2009 04:22:21 GMT


 o I see the NullPointer Exception as a good starting point but I'm
still perplexed.

I created a new project just for testing. And I can't get this to
work:

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class main extends Activity {
/** Called when the activity is first created. */
EditText justFillIn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

justFillIn = (EditText) findViewById(R.id.ipText);
justFillIn.setText("charSequence is a string");

setContentView(R.layout.main);
}
}

All I want to do is set the value for that EditText and it's just not
working.

Any help?

On Mar 23, 10:55am, Lovedumplingx <lovedumpli...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Mark Murphy » Thu, 26 Mar 2009 04:34:27 GMT


 


You must call setContentView() first.

You aren't telling Android what layout to display until you call
setContentView(). Hence, until that time, findViewById() is guaranteed
not to work, because there are no views to find.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Android App Developer Books:  http://commonsware.com/books.html 

--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Lovedumplingx » Thu, 26 Mar 2009 21:41:27 GMT


 Holy Mother Dogfood!!!

I am quite possibly an idiot (that's the second time I'm saying this
today)!  Wow.  So simple and yet I was looking at it all backward.

Thanks so much.  It makes sense now.





--~--~---------~--~----~------------~-------~--~----~



How do I set the value of an EditText

by Tseng » Sun, 29 Mar 2009 00:39:41 GMT


 Hehe,
yep, that's the most common error beginers make when start with
android and i already mentioned it in my post above ^^





--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Benckmark Tools Android

mau nanya rekan semua,

tools untuk benchmarkin di android naon ya?

mohon sharing nya.

Terima kasih

-- 
-----------------------
@ZuliadyAzwin <http://twitter.com/zuliadyazwin>

"Design is not just what it looks like and feels like. Design is how it
works" - Steve Jobs

-- 
===============

2. Android Dual-Core SuperPhone Fight! Motorola Atrix 4G vs Samsung Galaxy S II vs LG Optimus 3D

Rasanya sebelum nih gadget nyampe ke indonesia, ada baiknya kita liat
pertarungan ponsel muktahir berprosesor dual core ya

http://androidheadlines.com/2011/03/android-dual-core-superphone-fight-motorola-atrix-vs-samsung-galaxy-s2-vs-lg-optimus-3d.html

-- 
===============

3. More DropDown Menu Image

4. Rom buat hero

5. Having trouble programmatically checking an item in a ListView

6. Have you implemented fragments into a pre-3.0 app?

7. [WTS] Battrey Nexian Journey dan Silicon case