64442

How can I display values from EditText user input?

Question:

I have a SharedPreferences that saves EditText input from one activity and displays the String value in another activity.

When I enter an input into the EditText fields and press (a button I created) "Save" (which commits the EditText to editor), the file is stored successfully. However, the display activity (which displays the stored String values in SharedPreferences) doesn't display the values. I am guessing it is because it is the onCreate, so the screen is "created" when it runs. How can I get it to update the EditText values when the user commits (presses save) immediately?

CustomStoreEditActivity - Just storing the user inputs (EditText entries):

final Button saveButton = (Button) findViewById(R.id.saveButton); saveButton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { if (saveButton.isClickable()) { SharedPreferences prefs = getSharedPreferences( "customtime", 0); // prefs.registerOnSharedPreferenceChangeListener(this); final SharedPreferences.Editor edit = prefs.edit(); EditText shopName = (EditText) findViewById(R.id.shopName); EditText open1 = (EditText) findViewById(R.id.open1); EditText close1 = (EditText) findViewById(R.id.close1); EditText open2 = (EditText) findViewById(R.id.open2); EditText close2 = (EditText) findViewById(R.id.close2); EditText open3 = (EditText) findViewById(R.id.open3); EditText close3 = (EditText) findViewById(R.id.close3); EditText open4 = (EditText) findViewById(R.id.open4); EditText close4 = (EditText) findViewById(R.id.close4); EditText open5 = (EditText) findViewById(R.id.open5); EditText close5 = (EditText) findViewById(R.id.close5); EditText open6 = (EditText) findViewById(R.id.open6); EditText close6 = (EditText) findViewById(R.id.close6); EditText open7 = (EditText) findViewById(R.id.open7); EditText close7 = (EditText) findViewById(R.id.close7); EditText comments = (EditText) findViewById(R.id.comments); edit.putString("shopName", shopName.getText().toString()); edit.putString("Monday1", open1.getText().toString()); edit.putString("Monday2", close1.getText().toString()); edit.putString("Tuesday1", open2.getText().toString()); edit.putString("Tuesday2", close2.getText().toString()); edit.putString("Wednesday1", open3.getText().toString()); edit.putString("Wednesday2", close3.getText().toString()); edit.putString("Thursday1", open4.getText().toString()); edit.putString("Thursday2", close4.getText().toString()); edit.putString("Friday1", open5.getText().toString()); edit.putString("Friday2", close5.getText().toString()); edit.putString("Saturday1", open6.getText().toString()); edit.putString("Saturday2", close6.getText().toString()); edit.putString("Sunday1", open7.getText().toString()); edit.putString("Sunday2", close7.getText().toString()); edit.putString("comments", comments.getText().toString()); edit.commit(); Intent myIntent = new Intent(getBaseContext(), CustomStoreActivity.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(myIntent); Toast.makeText(getBaseContext(), "Opening Hours Saved!", Toast.LENGTH_SHORT).show(); } } }); }

CustomStoreActivity - where I believe the problem lies:

private void displayPreferences(){ SharedPreferences prefs = getSharedPreferences("customtime", 0); String shopName = prefs.getString("shopName", "Empty"); String shopTime1 = prefs.getString("Monday1", " "); String shopTime2 = prefs.getString("Monday2", " "); String shopComments = prefs.getString("comments", ""); TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs); displayPrefs.setText(shopName + shopTime1 + shopTime2 + shopComments); }

Thank you for your ample time.

Answer1:

I think part of what you're looking for is to put your CustomStoreActivity code in onResume instead of onCreate. Whatever code you move into onResume will occur whenever the CustomStoreActivity is brought to the front (including when it is first created).

Another alternative, assuming that CustomStoreActivity is used to launch the Activity that contains the EditTexts, is to use startActivityForResult and pass the data back in the result Intent instead of using the SharedPreferences. <a href="http://www.dev-articles.com/article/Android-Activity-startActivityForResult-387002" rel="nofollow">Here</a> is a simple example (though note that the setResult call in this example is passed null where you would want to pass in an Intent, as documented <a href="http://developer.android.com/reference/android/app/Activity.html#StartingActivities" rel="nofollow">here</a> in the Android docs).

It also seems like you're trying to use your second Activity like a dialog box with editable fields. If that's the case, yet another alternative is to actually use a variant of the Dialog class within your CustomStoreActivity class instead of creating another Activity to act like one. See the Android doc for <a href="http://developer.android.com/guide/topics/ui/dialogs.html" rel="nofollow">dialogs</a>.

Answer2:

I'm not sure I really understand your question but are you trying to update the TextView of a different Activity from the one you are in? In which case I don't think this can be done and you should use Intents to pass the data to the activity

Recommend

  • Permission denied running a method from a Ruby gem
  • Any way to return a ResultSet in Java?
  • Jquery accordion activate from external links
  • Ruby can't find the method capture2e from open3 module
  • Run a python script in perl
  • Error when retrieving value from database and applying to anchor tag.
  • how to open modal from another modal in ui bootstrap?
  • Import 3rd party module in SublimeREPL
  • Why does Open3.popen3 return wrong error when executable is missing?
  • How to get data from database and echo on php page?
  • Perl select returning undef on sysread when using Windows, IPC::Open3, and IO::Socket->socketpair
  • Laravel: PDO Class not found error
  • C++: constant reference to temporary
  • Add and display a new FormPage to a FormEditor using a command
  • Read tab delimited text file into MySQL table with PHP
  • How to open Theme editor in android studio
  • Question mark icons showing up for quotation marks when there's a UTF-8 character encoding
  • Find a Google Apps Script to terminate it
  • How to add source files of library jars?
  • How to implement xsl
  • Form with a MainMenu shrinks every time the project is run
  • Creating a EditorTemplate MVC3
  • How to specify columns in Swagger
  • Complicated COUNT query in MySQL
  • Eclipse code fomatting
  • How do you specify multiple AppleScript child elements of the same class?
  • i can't add #region to .ashx in visual studio 2010
  • JavaScript Intellisense is not working in Visual Studio 2015
  • Encoding: everything is UTF-8 but the DB output is displayed wrong. Any Ideas?
  • How do I embed a custom user-control in MS Word?
  • yii2 sorting in related model
  • gulp.watch running same task multiple times when saving many files
  • CKeditor stripping font tags instead of converting to span
  • How can I add div content to a new page in jspdf?
  • Android full screen on only one activity?
  • Django: Count of Group Elements
  • Fetching methods from BroadcastReceiver to update UI
  • Symfony2: How to get request parameter
  • GridView Sorting works once only
  • WPF Applying a trigger on binding failure