• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Require help with form code please

Arhodes

Lurker
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:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

public class review_report extends Activity {
	public static final String CarDetails= "";
	public static final String CarDetails_vehi_model = "";
	public static final String CarDetails_veh_year = "";
	public static final String CarDetails_veh_make = "";
	public static final String CarDetails_veh_licnum = "";
	public static final String CarDetails_veh_col = "";
	public static final String DEBUG_TAG = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.fill_report2);

		// setting up back button + sound
		
		final MediaPlayer buttonSound = MediaPlayer.create(review_report.this,
				R.raw.clicknoise);
		buttonSound.start();
		Button backbutton = (Button) findViewById(R.id.backbutton);
		backbutton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				buttonSound.start();
				startActivity(new Intent(
						"com.belairdirect.belairdirectaccidenthelper.MAIN"));
			}
		});
		
		//end setting up back button
		
		 //starting input data gathering
		final EditText modelText = (EditText) findViewById(R.id.TEXT_model);
		String vehi_model = modelText.getText().toString();
      
		// saving input data
		SharedPreferences CarDetails;
		CarDetails = getSharedPreferences(CarDetails_vehi_model, Context.MODE_PRIVATE);
        
		//no idea
		Editor editor = CarDetails.edit();
		editor.putString(CarDetails_vehi_model, vehi_model);
		editor.commit();
		
		//supposed to display the field if string has txt in it
		final EditText ModelText =
				(EditText) findViewById(R.id.TEXT_model);
				if (CarDetails.contains(CarDetails_vehi_model)) {
					ModelText.setText(CarDetails.getString(
				CarDetails_vehi_model, ""));
				}
		
		
		//save all data on page destroy
				onDestroy();
		{
					Log.d(DEBUG_TAG, "Car Model is:"
							+ CarDetails.getString(CarDetails_vehi_model, "Not set"));
			super.onDestroy();
		}
	}
}
 
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!)
 
so i'm sure i'm missing something rather obvious

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.
 
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:
public class review_report extends Activity 
{     
public static final String CarDetails= "";     
public static final String CarDetails_vehi_model = "";    
public static final String CarDetails_veh_year = "";     
public static final String CarDetails_veh_make = "";     
public static final String CarDetails_veh_licnum = "";     
public static final String CarDetails_veh_col = "";     
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:
public class ReviewReport extends Activity 
{
//debug
public static final String TAG = "ReviewReport";
   
//pref names  
public static final String CAR_DETAILS= "CarDetailsPref";     
public static final String MODEL = "ModelPref";    

//.... etc
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)
 
Back
Top Bottom