2013-07-18 43 views
0

我有兩個名爲disease_table和sysmptoms_table的表。我從數據庫中檢索了來自disease_table的數據並顯示在列表視圖中,並且當單擊listitem時,我必須相應地選擇並顯示疾病類別sysmtoms,並且我成功地做到了這一點,但是我的代碼有冗餘,我不得不在兩種方法中編寫datahelper類在另一個列表視圖中按照疾病檢索症狀。但我是與外鍵引用Android:datahelper類中的冗餘問題

的代碼如下的方法,其中「disease_id = 1」條件的查詢檢索列表視圖症狀的數據,在

//越來越痛症狀名稱一個ArrayList,然後顯示在列表視圖 //this.setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,symptompain));

 public List<String> getAllSymptomPain() { 
     List<String> symptompain = null; 

     cr = db.query(SYMPTOM_TABLE_NAME, new String[] {"symname"}, "diseaseid=1", null, null, null, null); 

      if(null != cr){ 
        symptompain = new ArrayList<String>(); 
        if (cr.moveToFirst()) { 
        do { 
         symptompain.add(cr.getString(0)); 
        } while (cr.moveToNext()); 
        } 

        if (cr != null && !cr.isClosed()) { 
         cr.close(); 
        } 
       } 
       return symptompain; 
      } 



    //getting colorchange symptom names in a arraylist and then display in listview 
    //this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,symptomcolorchange)); 


    public List<String> getAllSymptomColorChange() { 
     List<String> symptomcolorchange = null; 

     cr = db.query(SYMPTOM_TABLE_NAME, new String[] {"symname"}, "diseaseid=2", null, null, null, null); 

      if(null != cr){ 
        symptomcolorchange = new ArrayList<String>(); 
        if (cr.moveToFirst()) { 
        do { 
         symptomcolorchange.add(cr.getString(0)); 
        } while (cr.moveToNext()); 
        } 

        if (cr != null && !cr.isClosed()) { 
         cr.close(); 
        } 
       } 
       return symptomcolorchange; 
      } 

如何在單個方法中編寫這兩個函數,然後在擴展listactivity類的onListItemclick方法中調用它?

而且我OnListItemClick()方法如下:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 

    String item=(String)getListAdapter().getItem(position); 
if(item.equals("Pain in Teeth")){ 
       // passing the method here 
      } 
      else if(item.equals("Pain in Gums")){ 
       // passing the method here 
      } 
      else if(item.equals("Pain in Mucosa")){ 
       // passing the method here 
      } 
      else if(item.equals("Pain in TMJoint")){ 
       // passing the method here 
      } 
      else if(item.equals("Non-Specific Pain")){ 
       // passing the method here 
      } 
     } 

回答

0

試試這個:

public List<String> getSymptomsByDiseaseId(long diseaseId) { 

    List<String> symptomsList = new ArrayList<String>(); 

    String selection = "diseaseid=?"; 
    String[] selectionArgs = { String.valueOf(diseaseId) }; 
    Cursor cursor = db.query(false, SYMPTOM_TABLE_NAME, null, selection, selectionArgs, null, null, null, null); 
    if (cursor.moveToFirst()) { 
     do { 
      symptomsList.add(cursor.getString(0)); 
     } while (cursor.moveToNext()); 
    } 
    cursor.close(); 

    return symptomsList; 
}