Android:datahelper类中的冗余问题



我有两个名为disease_table和sysmptoms_table的表。我从数据库中的disease_table中检索数据,并显示在列表视图中,当单击列表项时,我必须相应地选择和显示疾病类别症状,我成功地做到了,但我的代码有冗余,我必须在datahelper类中写两个方法,以在另一个列表视图中根据疾病检索症状。我正在检索列表视图中的症状数据,查询条件为WHERE "disease_id=1",外键引用

这些方法的代码如下,

//getting pain symptom names in a arraylist and then display in 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
    }
}

试试这个:

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

最新更新