1

我有两个名为 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
    }
}
4

1 回答 1

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;
}
于 2013-07-18T06:13:25.187 回答