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

Apps Require help with form code please

Discussion in 'Android Development' started by Arhodes, Jul 22, 2011.

  1. Arhodes

    Arhodes Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Jul 21, 2011

    Jul 21, 2011
    7
    0
    5
    hi guys,

    could you do me a favour and take a look at the following code and see what i'm missing.. been staring at it for a while now and can't figure out why it's not working (note coding knowledge is only 4 days of java+xml.. so i'm sure i'm missing something rather obvious)

    Basically the code is supposed to save the txt from the input field and then output it back to that field if you leave/come back to the screen! buuut nada!

    thanks for any help and advice!

    ----------

    Code (Text):
    1.  
    2. import android.app.Activity;
    3. import android.content.Context;
    4. import android.content.Intent;
    5. import android.content.SharedPreferences;
    6. import android.content.SharedPreferences.Editor;
    7. import android.media.MediaPlayer;
    8. import android.os.Bundle;
    9. import android.util.Log;
    10. import android.view.View;
    11. import android.view.Window;
    12. import android.view.WindowManager;
    13. import android.widget.Button;
    14. import android.widget.EditText;
    15.  
    16. public class review_report extends Activity {
    17.     public static final String CarDetails= "";
    18.     public static final String CarDetails_vehi_model = "";
    19.     public static final String CarDetails_veh_year = "";
    20.     public static final String CarDetails_veh_make = "";
    21.     public static final String CarDetails_veh_licnum = "";
    22.     public static final String CarDetails_veh_col = "";
    23.     public static final String DEBUG_TAG = "";
    24.  
    25.     @Override
    26.     protected void onCreate(Bundle savedInstanceState) {
    27.         // TODO Auto-generated method stub
    28.  
    29.         super.onCreate(savedInstanceState);
    30.         requestWindowFeature(Window.FEATURE_NO_TITLE);
    31.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    32.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
    33.        
    34.         setContentView(R.layout.fill_report2);
    35.  
    36.         // setting up back button + sound
    37.        
    38.         final MediaPlayer buttonSound = MediaPlayer.create(review_report.this,
    39.                 R.raw.clicknoise);
    40.         buttonSound.start();
    41.         Button backbutton = (Button) findViewById(R.id.backbutton);
    42.         backbutton.setOnClickListener(new View.OnClickListener() {
    43.             @Override
    44.             public void onClick(View v) {
    45.                 // TODO Auto-generated method stub
    46.                 buttonSound.start();
    47.                 startActivity(new Intent(
    48.                         "com.belairdirect.belairdirectaccidenthelper.MAIN"));
    49.             }
    50.         });
    51.        
    52.         //end setting up back button
    53.        
    54.          //starting input data gathering
    55.         final EditText modelText = (EditText) findViewById(R.id.TEXT_model);
    56.         String vehi_model = modelText.getText().toString();
    57.      
    58.         // saving input data
    59.         SharedPreferences CarDetails;
    60.         CarDetails = getSharedPreferences(CarDetails_vehi_model, Context.MODE_PRIVATE);
    61.        
    62.         //no idea
    63.         Editor editor = CarDetails.edit();
    64.         editor.putString(CarDetails_vehi_model, vehi_model);
    65.         editor.commit();
    66.        
    67.         //supposed to display the field if string has txt in it
    68.         final EditText ModelText =
    69.                 (EditText) findViewById(R.id.TEXT_model);
    70.                 if (CarDetails.contains(CarDetails_vehi_model)) {
    71.                     ModelText.setText(CarDetails.getString(
    72.                 CarDetails_vehi_model, ""));
    73.                 }
    74.        
    75.        
    76.         //save all data on page destroy
    77.                 onDestroy();
    78.         {
    79.                     Log.d(DEBUG_TAG, "Car Model is:"
    80.                             + CarDetails.getString(CarDetails_vehi_model, "Not set"));
    81.             super.onDestroy();
    82.         }
    83.     }
    84. }
    85.  
    86.  
     

    Advertisement

  2. Arhodes

    Arhodes Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Jul 21, 2011

    Jul 21, 2011
    7
    0
    5
    fiddled about all day with it.. got it down to this although still not working.. any ideas? :)

    just to recap, basically I want it to store the data from the EditText field, and re-show it in the EditText field (if user has put their info in!)
     
  3. amlothi

    amlothi Android Expert
    Rank:
    None
    Points:
    143
    Posts:
    1,185
    Joined:
    Jul 23, 2010

    Jul 23, 2010
    1,185
    201
    143
    One thing you are missing is the App Development forum:
    Application Development - Android Forums

    You posted in the general forum for discussion of Android Applications. Many casual Android users come here to discuss or find applications that are already on the market. Many of them (including myself) don't know a thing about writing an application.

    The probability of you getting a helpful response would be higher if you posted in the forum I linked.
     
  4. Xyro

    Xyro 4 8 15 16 23 42
    Moderator
    Rank:
     #19
    Points:
    1,433
    Posts:
    13,004
    Joined:
    Dec 1, 2009

    Dec 1, 2009
    13,004
    9,625
    1,433
    UK
    Thread moved to the Application Development section.
     
  5. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    At a quick glance, I see one problem here:

    Thes variables should not be empty strings, you are telling the SharedPreferences to save some data under the name "" <- (empty string)

    Code (Text):
    1. public class review_report extends Activity
    2. {    
    3. public static final String CarDetails= "";    
    4. public static final String CarDetails_vehi_model = "";    
    5. public static final String CarDetails_veh_year = "";    
    6. public static final String CarDetails_veh_make = "";    
    7. public static final String CarDetails_veh_licnum = "";    
    8. public static final String CarDetails_veh_col = "";    
    9. public static final String DEBUG_TAG = "";
    I'm surprised the SharedPrefs class didn't leap out of your monitor and slap you silly with error messages ;)

    Instead use something like this:

    Code (Text):
    1. public class ReviewReport extends Activity
    2. {
    3. //debug
    4. public static final String TAG = "ReviewReport";
    5.    
    6. //pref names  
    7. public static final String CAR_DETAILS= "CarDetailsPref";    
    8. public static final String MODEL = "ModelPref";    
    9.  
    10. //.... etc
    11.  
    12.  
    Notice I changed some of the capitalizations -- this is to show you to proper convention most (like 98%) Java coders use.

    The basics are:

    - constants (ie static & final) are all caps with underscores like this:
    static final string HELLO_CONSTANT = "HelloWorldConstant";

    - class names are TitleCase like this:

    ClassName extends ...

    - methods and varibles are camelCase like this:

    myBackButton;
    startActivity();


    (lower case 1st letter, uppercase for first letter of next word, no underscores)
     

Share This Page

Loading...