Android:datahelper类中的冗余问题

时间:2013-07-18 05:54:37

标签: android sqlite

我有两个名为disease_table和sysmptoms_table的表。我从db中检索了来自disease_table的数据并显示在listview上,当点击listitem时,我必须相应地选择并显示疾病类别sysmtoms,我成功地做了 但是我的代码有冗余,我必须在datahelper类中编写两个方法来根据另一个列表视图中的疾病来检索症状。我正在检查列表视图中的症状数据与条件为WHERE“disease_id = 1”的查询与外键引用

方法的代码如下,

//在arraylist中获取痛苦症状名称,然后在listview中显示          //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;
           }

如何在单个方法中编写这两个,然后在onListItemclick方法下扩展listactivity的类中调用它?

我的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
            }
        }

1 个答案:

答案 0 :(得分: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;
}