Eclipse,如何将微调器存储到数据库

时间:2013-10-23 01:56:17

标签: android sql eclipse

我知道如何将spinner值存储到SQLite数据库的字符串,但数据库通过http的方法不同。我想知道我在代码中缺少什么,不知道我写的行是否正确。感谢任何帮助,谢谢。

这是活动

public class RegisterUser extends Activity implements OnClickListener, AsyncTaskComplete, OnItemSelectedListener{

    private User user = null;
    private Spinner mGender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);

        Intent i = getIntent();
        user = (User)i.getSerializableExtra("User");

        if (user != null) {
            ((EditText)findViewById(R.id.eemail)).setText(user.getEmail());
            ((EditText)findViewById(R.id.repass)).setText(user.getPassword());
            ((EditText)findViewById(R.id.efname)).setText(user.getFirstname());
            ((EditText)findViewById(R.id.elname)).setText(user.getLastname());
            ((EditText)findViewById(R.id.edob)).setText(user.getDob());
            //((Spinner)findViewById(R.id.spinner1).setText(user.getGender());

            mGender = (Spinner)findViewById(R.id.spinner1);
            // Spinner method to read the on selected value
            ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
                android.R.layout.simple_spinner_item, new State[] { 
                    new State("Male"), 
                    new State("Female")
                }
            );
            mGender.setAdapter(spinnerArrayAdapter);
            mGender.setOnItemSelectedListener(this);
        }

        // set onclick listeners
        findViewById(R.id.buttonNext).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.buttonNext:
            PostUser();
            break;
        }
    }

    @SuppressWarnings("unchecked")
    private void PostUser() {
        // Initialize HttpGet
        CloudDBPost httppost = new CloudDBPost(this, this);

        ArrayList<NameValuePair> lst = new ArrayList<NameValuePair>();

        // add url part
        lst.add(new BasicNameValuePair("url", "http://www.stepsaway.vrgnetwork.com/users/add"));


        lst.add(new BasicNameValuePair("email", ((EditText)findViewById(R.id.eemail)).getText().toString().trim()));
        lst.add(new BasicNameValuePair("pass", ((EditText)findViewById(R.id.repass)).getText().toString().trim()));
        lst.add(new BasicNameValuePair("firstname", ((EditText)findViewById(R.id.efname)).getText().toString().trim()));
        lst.add(new BasicNameValuePair("lastname", ((EditText)findViewById(R.id.elname)).getText().toString().trim()));
        lst.add(new BasicNameValuePair("dob", ((EditText)findViewById(R.id.edob)).getText().toString().trim()));
        lst.add(new BasicNameValuePair("gender", mGender.getSelectedItem().toString().trim()));


        // add file part
        //lst.add(new BasicNameValuePair("file", "/storage/sdcard/Pictures/icon1.png"));

        httppost.execute(lst);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onAsyncTaskComplete (String result) {   
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
        Log.d("SaveUser", result);

        if (result.trim().toString().equals("OK")) {
            // start view contacts activity
            Intent contacts = new Intent(getApplicationContext(), GameActivity.class);
            contacts.putExtra("userid", user.getUserid());
            startActivity(contacts);
        } else {
            AlertDialog.Builder adb = new AlertDialog.Builder(RegisterUser.this);
            adb.setTitle("Save User");
            adb.setMessage("Unable to save User successfully");
            adb.setPositiveButton("Ok", null);
            adb.show();   
        }
    }

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // Get the currently selected State object from the spinner
        State st = (State)mGender.getSelectedItem();

        // Show it via a toast
        toastState( "onItemSelected", st );
    } 

    public void toastState(String name, State st) {
        if ( st != null ) {
            //gender = st.name;
            //Toast.makeText(getBaseContext(), Gen, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
}

这些是字符串:

public class User implements Serializable {
    private String userid;
    private String email;
    private String pass;
    private String fname;
    private String lname;
    private String dob;
    private String gender;
    private String locationid;
    private String latitude;
    private String longitude;
    static final long serialVersionUID = 99L;

    public User() {
        // TODO Auto-generated constructor stub
        userid = "0";
        locationid = "0";
        latitude = "0";
        longitude = "0";
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getPassword() {
        return pass;
    }

    public void setPassword(String pass) {
        this.pass = pass;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getFirstname() {
        return fname;
    }

    public void setFirstname(String fname) {
        this.fname = fname;
    }

    public String getLastname() {
        return lname;
    }

    public void setLastname(String lname) {
        this.lname = lname;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getLocationid() {
        return locationid;
    }

    public void setLocationid(String locationid) {
        this.locationid = locationid;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要做的就是存储微调器当前设置的索引。您可以使用getSelectedItemPosition()获取当前索引。重建视图时,如果要将微调器恢复为正确值,请将存储的索引传递到setSelection()