如何从Android中选定的Spinner项获取SQLite数据

时间:2014-06-27 05:27:53

标签: java android android-sqlite

我检索了Spinner中的所有第一列数据。我想在选择微调项目时检索SQLite数据。数据将显示在EditText s中。但是当我运行应用程序时,数据无法显示在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();
        }


    }

1 个答案:

答案 0 :(得分:1)

我建议你看一下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();
        }
    }
    }

我建议你看看我上面给你的链接