2016-01-06 92 views
1

我想進行查詢並使用用戶在edittext中輸入的值,因爲條件是where子句。 在數據庫helper.java類: 查詢如下:不能正常工作查詢

public Cursor patientHisto (String value) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
Cursor res = db.rawQuery (" select dateap,diagnostic,prestreatment from patients_table, appointments_table, examinations_table where patients_table.id = appointments_table.idp and appointments_table.idap = examinations_table.id_ap and ID = ? order by name, familyname asc" , new String[] {value}); 
     return res; 
    } 

在ExaminatFragment.java:

public class ExaminatFragment extends Fragment { 
    DatabaseHelper myDB; 
    EditText id_p; 
    Button showhp; 
    String value; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){ 
    View v = inflater.inflate(R.layout.fragment_examinat, parent, false); 
    myDB = new DatabaseHelper(getActivity()); 

    id_p = (EditText)v.findViewById(R.id.id_p_text); 


    getPatHisto(); 
    return v; 
    } 

public void getPatHisto() { 

    value = id_p.getText().toString(); 

    showhp.setOnClickListener(

       new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     Cursor res = myDB.patientHisto(value); 
     if (res.getCount() == 0){ 

    showMessage("Patient history", "No patient history found"); 
     return; 

        } 


     StringBuffer buffer = new StringBuffer(); 
     while (res.moveToNext()){ 
          buffer.append("Dateap:"+res.getString(0)+"\n"); 
          buffer.append("Diagnostic:"+res.getString(1)+"\n"); 
          buffer.append("Prestreatment:"+res.getString(2)+"\n\n"); 




         } 
       showMessage("Patient's history", buffer.toString()); 
        } 


       } 
       ); 
       } 






public void showMessage(String title, String Message){ 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setCancelable(true); 
     builder.setTitle(title); 
     builder.setMessage(Message); 
     builder.show(); 

    } 

運行應用程序時,在editext patientid選擇並點擊按鈕,它總是給我:即使患者有檢查歷史,也沒有找到患者病史。 請幫助..提前謝謝

回答

1

您在設置活動時僅捕獲EditText的內容一次。

將點擊監聽器中的value = id_p.getText().toString();移動到每次按下按鈕時捕獲當前內容。