查询列表项单击

时间:2012-11-27 19:09:34

标签: android sqlite listview

我已经设法将我的数据库中的本地健康中心列表显示到列表视图,并且还成功地在点击的项目上显示祝酒词,我现在的目标是显示所点击的项目上的信息(地址,联系号码)列表并将其显示给下一个活动。我想使用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时缺少哪些声明?谢谢!

1 个答案:

答案 0 :(得分:0)

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

i.putExtra("field1", val1);
i.putExtra("field2", val2);
i.putExtra("field3", val3);