如何从spinner android中选择值然后插入到mysql?

时间:2013-09-28 05:19:43

标签: java android mysql

如何从微调器中选择值然后插入到php mysql中? 我有两个java文件。

1.spinner

2.将数据插入mysql

如何合并两个文件??

这是我的java代码,任何机构帮帮我吗?

  1. spinner.java

    public class spinner extends Activity
    implements AdapterView.OnItemSelectedListener {
    TextView selection;
    String[] items={"Aceh", "Sumatera Utara","Papua"};
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        selection=(TextView)findViewById(R.id.selection);
    
        Spinner spin=(Spinner)findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);
    
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,items);
    
        aa.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
    }
    
    public void onItemSelected(AdapterView<?> parent,
                                                                View v, int position, long id) {
        selection.setText(items[position]);
    }
    
    public void onNothingSelected(AdapterView<?> parent) {
        selection.setText("");
    }
    

    }

  2. 此文件tambah_user.java

  3. 这是我的文件tambah_user.java代码发送mysql amy正文帮助我吗?

    public class pasang extends Activity {
    EditText nomor_reg, nama, ttl, jam, peruntukan, keterangan, notelp;
    TextView status, myLatitude, myLongitude;
    Button btnShowLocation, simpan, keluar;
    GPSTracker gps;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pasang);
        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
        // show location button click event
        myLatitude = (TextView) findViewById(R.id.mylatitude);
        myLongitude = (TextView) findViewById(R.id.mylongitude);
        nomor_reg = (EditText) findViewById(R.id.txtnomor_reg);
        nama = (EditText) findViewById(R.id.txtnama);
        ttl = (EditText) findViewById(R.id.txtttl);
        jam = (EditText) findViewById(R.id.txtjam);
        peruntukan = (EditText) findViewById(R.id.txtperuntukan);
        keterangan = (EditText) findViewById(R.id.txtketerangan);
        notelp = (EditText) findViewById(R.id.txtnotelp);
        simpan = (Button) findViewById(R.id.btnsimpan);
        keluar = (Button) findViewById(R.id.btnexit);
        status = (TextView) findViewById(R.id.txtstatus);
        simpan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("nomor_reg",
                        nomor_reg.getText().toString()));
                postParameters.add(new BasicNameValuePair("nama", nama
                        .getText().toString()));
                postParameters.add(new BasicNameValuePair("ttl", ttl.getText()
                        .toString()));
                postParameters.add(new BasicNameValuePair("jam", jam.getText()
                        .toString()));
                postParameters.add(new BasicNameValuePair("peruntukan",
                        peruntukan.getText().toString()));
                postParameters.add(new BasicNameValuePair("keterangan",
                        keterangan.getText().toString()));
                postParameters.add(new BasicNameValuePair("notelp", notelp
                        .getText().toString()));
                postParameters.add(new BasicNameValuePair("lat", myLatitude
                        .getText().toString()));
                postParameters.add(new BasicNameValuePair("long", myLongitude
                        .getText().toString()));
    
                /* String valid = "1"; */
    
                String response = null;
    
                try {
    
                    response = koneksigangguan.executeHttpPost(
                            "http://plnskh.zz.mu/android/lapor/pasang.php",
                            postParameters);
    
                    String res = response.toString();
    
                    res = res.trim();
    
                    res = res.replaceAll("\\s+", "");
    
                    status.setText(res);
    
                    if (res.equals("1"))
                        status.setText("Data tidak Tersimpan Ke server");
    
                    else
                        status.setText("Data berhasil disimpan ke server");
    
                }
    
                catch (Exception e) {
    
                    nomor_reg.setText(e.toString());
    
                }
    
            }
    
        });
    }
    
    public void keluar(View theButton) {
        Intent a = new Intent(this, Mainmenu.class);
        startActivity(a);
        finish();
    }
    

    }

2 个答案:

答案 0 :(得分:0)

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
postParameters.add(new BasicNameValuePair("YourParameter",mySpinner.getSelectedItem().toString());

答案 1 :(得分:0)

 Spinner spinner2 = (Spinner) findViewById(R.id.selectstate);
    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            Object item = parent.getItemAtPosition(pos);
        String classval = item.toString();

        }

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

        }
    });