Android:通过单击listview上的项目导航并检索数据库frm数据库并在另一个列表视图中显示它

时间:2013-07-18 06:54:07

标签: android sqlite

我有一个从数据库中检索到的列表视图

 public class DiseaseList extends ListActivity{


DataHelper dh;



@Override   
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diseaselist);

    dh = new DataHelper(this);
    dh.open();
    dh.getAllDiseaseNames();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

    setListAdapter(adapter);

}

@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")){
        dh.getSymptomsByDiseaseId(1);   
        startActivity(new Intent(DiseaseList.this,Symptoms.class));
            }
            else if(item.equals("Color change in teeth")){
                dh.getSymptomsByDiseaseId(2);
                startActivity(new Intent(DiseaseList.this,Symptoms.class));
            }
            else if(item.equals("bleeding gums")){
                startActivity(new Intent(DiseaseList.this,Questions.class));
            }
            else if(item.equals("decay in teeth")){
                startActivity(new Intent(DiseaseList.this,Questions.class));
            }
            else if(item.equals("dislodged caps")){
                startActivity(new Intent(DiseaseList.this,Questions.class));
            }
            }
              }

和我检索此数据的datahelper类方法是:

 public List<String> getAllDiseaseNames() {
         List<String> diseasename = null;

         cr = db.query(DISEASE_TABLE_NAME,new String[] {DISEASE_NAME}, null, null, null, null, null);

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

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


     //symptomsbydiseaseid to get symptoms in list view

     public List<String> getSymptomsByDiseaseId(long diseaseId) {

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

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

            return symptomsList;
        }

现在在第一个列表视图中,当我点击“A”时,我应该移动到另一个列表活动并在类别A下显示另一个列表视图,与B和C和D相同...并且在其他列表视图下,它将具有关于点击它们的另一个问题清单

我的症状如下:

package com.dentabtechnologies.appstart;

import com.example.dentabtechnologies.R;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Symptoms extends ListActivity {

        @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.symptoms);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

    setListAdapter(adapter);

}

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

} 

我怎样才能做到这一点? 谢谢

流程来自疾病列表视图&gt;症状列表视图&gt;

1 个答案:

答案 0 :(得分:0)

首先向适配器添加数据:

adapter.addAll(dh.getAllDiseaseNames())