-1

我已经设法将本地医疗中心列表从我的数据库显示到列表视图,并且还成功地在单击的项目上显示了祝酒词,我现在的目标是在列表中单击的项目上显示信息(地址、联系电话)和将其显示到下一个活动。我想使用 putExtras 吗?以及如何进行原始查询?而且我正在从我的数据库中选择 3 个字段。这是否意味着我还必须进行 3 个查询和 3 个捆绑包?

这是我的代码:

public class Health extends ListActivity {

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

    public void displayHospitals (){
        DbHelper tblHL = new DbHelper(this);
        tblHL.open();
        ArrayList<String> result = tblHL.getHData();
        result = tblHL.getHData();
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, result));
         tblHL.close();
        }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String hosp = (String)getListAdapter().getItem(position);
        Toast.makeText(this, hosp, Toast.LENGTH_SHORT).show();

    }



}

这是我的 dbhelper 类

public class DbHelper {

public static final String Row_id = "_id";
private static final String db_DgTable = "tblDrugs";
public static final String Row_Name = "faName";
private static final String Row_Drugs = "GenericName";
public static final String Row_Desc = "faInfo";
private static final String db_Name = "dbDrDroid";
private static final String db_Table = "tblFirstAid";
private static final String db_HTable = "tblHospitals";
private static final String Row_HosName = "HospitalName";
private static final String Row_HosAdd = "Address";
private static final String Row_HosReg = "Region";
private static final String Row_HosCity = "City";
private static final String Row_HosContact = "Contact";
private static final String Row_dName = "dName";
private static final String Row_dTable = "tblDisease";
private static final int db_Version = 1;
private dbhelp ourhelper;
private static Context ourcontext;
private SQLiteDatabase ourDB;

private static class dbhelp extends SQLiteOpenHelper{

    public dbhelp(Context context) {
        super(context, db_Name, null, db_Version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub



        db.execSQL("CREATE TABLE IF NOT EXISTS " + db_Table + " (" + Row_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + Row_Name + " TEXT NOT NULL, "
            + Row_Desc + " TEXT NOT NULL)" );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS" + db_Table);
        onCreate(db);

    }

}

public DbHelper (Context c){
    ourcontext= c;
}

public DbHelper open(){
    ourhelper = new dbhelp(ourcontext);
    ourDB = ourhelper.getWritableDatabase();
    return this;
}

public void   close(){
    ourhelper.close();
}

public ArrayList<String> getFAData() {
    // TODO Auto-generated method stub
    ArrayList<String> comments = new ArrayList<String>();
    String [] columns = new String[]{Row_Name};
    Cursor c = ourDB.query(db_Table, columns, null, null, null, null, null);
    int iRow = c.getColumnIndex(Row_Name);
    c.moveToFirst();
    while (!c.isAfterLast()) {          
        comments.add(c.getString(iRow));
        c.moveToNext();
    }

    c.close();
    return comments;
}

public ArrayList<String> getHData(){
    ArrayList<String>  res = new  ArrayList<String>();
    String [] columns = new String []{Row_HosName};
    Cursor h = ourDB.query(db_HTable, columns, null, null, null, null, Row_HosName);
    int HRow = h.getColumnIndex(Row_HosName);
    h.moveToFirst();
     while (!h.isAfterLast()) {          
            res.add(h.getString(HRow));
            h.moveToNext();
        }

        h.close();
        return res;

}

public ArrayList<String> getDData(){
    ArrayList<String>  res = new  ArrayList<String>();
    String [] columns = new String []{Row_dName};
    Cursor d = ourDB.query(Row_dTable, columns, null, null, null, null, null);
    int HRow = d.getColumnIndex(Row_dName);
    d.moveToFirst();
     while (!d.isAfterLast()) {          
            res.add(d.getString(HRow));
            d.moveToNext();
        }

        d.close();
        return res;

}

public ArrayList<String> getDgData(){
    ArrayList<String>  res = new  ArrayList<String>();
    String [] columns = new String []{Row_Drugs};
    Cursor d = ourDB.query(db_DgTable, columns, null, null, null, null, Row_Drugs);
    int HRow = d.getColumnIndex(Row_Drugs);
    d.moveToFirst();
     while (!d.isAfterLast()) {          
            res.add(d.getString(HRow));
            d.moveToNext();
        }

        d.close();
        return res;

}

}

在我的 onitemclick 方法中添加 rawquery 时缺少哪些声明?谢谢!

4

1 回答 1

0

您可以从单个查询中返回多个字段,并使用同一个包将您的 3 个字段设置为:

i.putExtra("field1", val1);
i.putExtra("field2", val2);
i.putExtra("field3", val3);
于 2012-11-27T19:30:52.067 回答