[英]Insert spinner value into database
我已经从 SQL 数据库填充了微调器值,但我希望在选择该值后它应该再次保存到数据库中。 但是当我点击保存按钮时( androidx.appcompat.widget.AppCompatSpinner{eadaafd VFED..CL. ........ 19,38-505,171 #7f0700bc app:id/spn_flight}
)这一行被保存到数据库不是实际值。
package com.example.timetracker.ui;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.timetracker.R;
import com.example.timetracker.jdbc_connection.ConnectionHelper;
import com.example.timetracker.manager.SharedPrefManager;
import com.example.timetracker.util.Const;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
public class FlightSelection extends AppCompatActivity {
Spinner spn_flight;
Button btn_submit;
TextView date;
ProgressDialog progressDialog;
SharedPrefManager sharedPrefManager = new SharedPrefManager();
String ip, db, un, passwords;
Connection connect;
PreparedStatement stmt;
ResultSet rs;
Connection conn;
boolean isSucess = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight_selection);
//create a date string.
String date_n = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault()).format(new Date());
//get hold of textview.
date = (TextView) findViewById(R.id.dateText);
//set it as current date.
date.setText(date_n);
ip = "SECRET";
un = "SECRET";
passwords = "SECRET";
db = "SECRET";
spn_flight = (Spinner) findViewById(R.id.spn_flight);
btn_submit = (Button) findViewById(R.id.btn_submit);
connect = CONN(un, passwords, db, ip);
String query = "select FlightNumber from flight";
try {
connect = CONN(un, passwords, db, ip);
stmt = connect.prepareStatement(query);
rs = stmt.executeQuery();
ArrayList<String> data = new ArrayList<String>();
while (rs.next()) {
String id = rs.getString("FlightNumber");
data.add(id);
}
String[] array = data.toArray(new String[0]);
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, data);
spn_flight.setAdapter(NoCoreAdapter);
} catch (SQLException e) {
e.printStackTrace();
}
spn_flight.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String name = spn_flight.getSelectedItem().toString();
Toast.makeText(FlightSelection.this, name, Toast.LENGTH_SHORT)
.show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadtable uploadtable = new uploadtable();
uploadtable.execute();
}
});
}
@SuppressLint("NewApi")
private Connection CONN(String _user, String _pass, String _DB,
String _server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
+ "databaseName=" + _DB + ";user=" + _user + ";password="
+ _pass + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return conn;
}
private class uploadtable extends AsyncTask<Void, Void, Void> {
/* @Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(FlightSelection.this);
progressDialog.setTitle("Creating account please wait!");
progressDialog.show();
super.onPreExecute();
}*/
@Override
protected Void doInBackground(Void... voids) {
try {
ConnectionHelper con = new ConnectionHelper();
conn = con.connectionclasss(Const.DBUserNameStr, Const.DBPasswordStr, Const.db, Const.ip);
String queryStmt = "Insert into flight" +
" (FlightNumber) values "
+ "('"
+ spn_flight
+ "')";
Statement stmt = conn.createStatement();
isSucess = stmt.execute(queryStmt);
isSucess = true;
conn.close();
// return data;
} catch (Exception e) {
isSucess = false;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (isSucess) {
Toast.makeText(FlightSelection.this, "product added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(FlightSelection.this, "prduct not added", Toast.LENGTH_SHORT).show();
}
super.onPostExecute(aVoid);
}
}
}
(androidx.appcompat.widget.AppCompatSpinner{eadaafd VFED..CL. ........ 19,38-505,171 #7f0700bc app:id/spn_flight})
此行保存到数据库中,而不是实际值。
在query
中,您直接传递了spinner
的实例,它保存了相同的实例名称。 而您应该将spinner
的selectedItem
传递给query
。
String queryStmt = "Insert into flight" +
" (FlightNumber) values "
+ "('"
+ spn_flight.getSelectedItem()
+ "')";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.