2014-06-27 95 views
0

我已經檢索到Spinner中的所有第一列數據。我想在選擇微調項目時檢索SQLite數據。數據將顯示在EditText s。但是當我運行應用程序時,數據無法顯示在EditText中。有人可以給我一個建議嗎?提前致謝。如何從Android中選定的Spinner項目獲取SQLite數據

這裏是我的代碼

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(); 
     } 


    } 
+0

同時運行該代碼會發生什麼我給你的鏈接看看? – Gunaseelan

+0

@Gunaseelan:當我運行它不顯示任何錯誤,並沒有顯示edit_text的任何行數據。 – Robotics

+0

好吧,現在打印'cursor.getCount()'並告訴我答案。 – Gunaseelan

回答

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(); 
     } 
    } 
    } 

我建議你把上面

+0

仍然不顯示數據。 – Robotics

+0

請確保您有數據首先,使用SQLite嘗試在終端或者你可以下載一個SQLite瀏覽器Mozilla Firefox的 –

+0

問題必須在查詢@Robotics –

相關問題