如何将选定的值从字符串(微调器)保存到sqlite

时间:2018-08-29 08:29:30

标签: android

因此,我尝试实现一个新的微调器,以在代码上获得新的价值。我已经将值放在res / strings中,并且运行良好。 EditText值没有问题,但是我对放置微调器代码的位置感到困惑。因此,我将所选值转换为sqlite中的第6个值。

或者如果有任何不必要的代码,请告诉我。

这是我的代码:

public class DataPeserta extends AppCompatActivity {
protected Cursor cursor;
datpes dbHelper;
Button ton1, ton2;
EditText text1, text2, text3, text4, text5;
Spinner spNamen;

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

    dbHelper = new datpes(this);
    text1 = (EditText) findViewById(R.id.editText1);
    text2 = (EditText) findViewById(R.id.editText2);
    text3 = (EditText) findViewById(R.id.editText3);
    text4 = (EditText) findViewById(R.id.editText4);
    text5 = (EditText) findViewById(R.id.editText5);
    spNamen = (Spinner) findViewById(R.id.sp_cbng);
    ton1 = (Button) findViewById(R.id.button1);
    ton2 = (Button) findViewById(R.id.button2);


    ton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            db.execSQL("insert into biodata(no, nik, kpj, nama, nohp, cabang) values('" +
                    text1.getText().toString() + "','" +
                    text2.getText().toString() + "','" +
                    text3.getText().toString() + "','" +
                    text4.getText().toString() + "','" +
                    text5.getText().toString() +  "')");


            Toast.makeText(getApplicationContext(), "Berhasil Daftar", Toast.LENGTH_LONG).show();
            MainMenu.mm.RefreshList();
            finish();
        }
    });
    ton2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            finish();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

尝试一下

spNamen.getSelectedItem().toString();

编辑

public class DataPeserta extends AppCompatActivity {
protected Cursor cursor;
datpes dbHelper;
Button ton1, ton2;
EditText text1, text2, text3, text4, text5;
Spinner spNamen;

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

    dbHelper = new datpes(this);
    text1 = (EditText) findViewById(R.id.editText1);
    text2 = (EditText) findViewById(R.id.editText2);
    text3 = (EditText) findViewById(R.id.editText3);
    text4 = (EditText) findViewById(R.id.editText4);
    text5 = (EditText) findViewById(R.id.editText5);
    spNamen = (Spinner) findViewById(R.id.sp_cbng);
    ton1 = (Button) findViewById(R.id.button1);
    ton2 = (Button) findViewById(R.id.button2);

    String[]   cabangArray =getResources().getStringArray(R.array.Cabang_Pelayanan);           

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, cabangArray);

    spNamen.setAdapter(adapter);



    ton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            db.execSQL("insert into biodata(no, nik, kpj, nama, nohp, cabang) values('" +
                text1.getText().toString() + "','" +
                text2.getText().toString() + "','" +
                text3.getText().toString() + "','" +
                text4.getText().toString() + "','" +
                text5.getText().toString() + "','" +
                spNamen.getSelectedItem().toString() +  "')");


            Toast.makeText(getApplicationContext(), "Berhasil Daftar", Toast.LENGTH_LONG).show();
            MainMenu.mm.RefreshList();
            finish();
        }
    });
    ton2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            finish();
        }
    });
}

查询

db.execSQL("insert into biodata(no, nik, kpj, nama, nohp, cabang) values('" +
                    text1.getText().toString() + "','" +
                    text2.getText().toString() + "','" +
                    text3.getText().toString() + "','" +
                    text4.getText().toString() + "','" +
                    text5.getText().toString() + "','" +
                    spNamen.getSelectedItem().toString() +  "')");