我已经检索了 Spinner
中的所有第一列数据。我想在选择微调项目时检索 SQLite 数据。数据将显示在 EditText
中。但是当我运行应用程序时,EditText
中无法显示数据。有人可以给我建议吗?提前致谢。
这是我的代码
spinner_searchByEmpName = (Spinner)findViewById(R.id.searchByEmpName);
loadSerachBYProject() ;
spinner_searchByEmpName = (Spinner)findViewById(R.id.searchByEmpName);
loadSerachBYProject() ;
spinner_searchByEmpName.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
selectedEmployeeName = spinner_searchByEmpName.getSelectedItem().toString().trim();
System.out.println("selectedProjectName " + selectedEmployeeName);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
etEmpName=(EditText)findViewById(R.id.Delete_editText_StaffEmployee_Name);
etDepartment=(EditText)findViewById(R.id.Delete_editText_Department);
etDesignation=(EditText)findViewById(R.id.Delete_editText_Designation);
try {
databaseHelper = new DatabaseHelper(this);
db=databaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT staff_emp_name, department, designation FROM employee_details WHERE staff_emp_name = ?",
new String[] { "" + selectedEmployeeName });
if(cursor!=null && cursor.moveToFirst())
{
etEmpName.setText(cursor.getString(cursor
.getColumnIndex("staff_emp_name")));
etDepartment.setText(cursor.getString(cursor
.getColumnIndex("department")));
etDesignation.setText(cursor.getString(cursor
.getColumnIndex("designation")));
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
最佳答案
我建议你看一下android数据库中的教程,看看:Android SQLite database and content provider - Tutorial
永远不要对表名使用硬编码名称,列将它们存储在像这样的变量中:
public static final String EMP_TABLE = "employee_details";
public static final String EMP_COL_NAME = "staff_emp_name"
您可以创建一个代表数据库中数据的对象,如下所示:
public class Employee{
private String name;
private String department;
private String designation;
public Employee(String name,String dept,String designation){
this.name = name;
this.department = dept;
this.designation = designation;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
还要重构您的方法,如下所示:
private Employee getEmployeeByProj(String name){
try {
databaseHelper = new DatabaseHelper(this);
db=databaseHelper.getReadableDatabase();
final String where = EMP_TABLE + "=\"" + name + "\"";
Cursor cursor = db.query("employee_details",new String[] {"staff_emp_name","department","designation"},where,null,null,null,null);
if(cursor!=null && cursor.moveToFirst())
{
final String name = cursor.getString(cursor
.getColumnIndex(EMP_COL_NAME));
final String dept = cursor.getString(....
final String designation cursor.getString(.....
//do the rest of the code here
return new Employee(name,dept,designation);
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
我建议您查看我上面给您的链接
关于java - 如何从 Android 中选定的 Spinner 项目中获取 SQLite 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24444533/