我已经创建了一个蔬菜类,我将从数据库类中获取所有数据,我需要将数据存储在字符串数组中。 说我有洋葱,马铃薯的物品在数据库中有50,80的价格。 现在我需要从数据库中获取这些值并将其存储在我的主类中
String items[] = {"onion","potato"};
String price[] = {"50","80"};
我的主要课程如下:
package com.ku.bazzar;
public class VegetableActivity extends Activity {
//String items[];
//String price[];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vegetables_info);
我尝试过类似下面的内容:
Vegetablesdatabase info = new Vegetablesdatabase(this);
info.open();
items[] = { info.getvegetable();}
price[]= { info.getprice();}
info.close();
我知道这是错的:
items[] = { info.getvegetable();}
price[]= { info.getprice();}
所以任何人都可以请教我制作项目和价格的字符串数组,并在我的vegetabledatabase文件中创建方法getvegetable()
和getprice()
?
我创建了一个数据库类,如下所示
package com.ku.bazzar;
public class Vegetablesdatabase {
public static final String KEY_ROWID = "_id";
public static final String KEY_VEGETABLES = "vegetables";
public static final String KEY_PRICE = "price";
private static final String DATABASE_NAME="ITEM_VEGETABLES";
private static final String DATABASE_TABLE="VEGETABLES";
private static final int DATABASE_VERSION= 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourdatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_VEGETABLES + " TEXT NOT NULL, " +
KEY_PRICE + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Vegetablesdatabase(Context c){
ourContext = c;
}
public Vegetablesdatabase open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourdatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String vegetables, String price) {
ContentValues cv = new ContentValues();
cv.put(KEY_VEGETABLES, vegetables);
cv.put(KEY_PRICE, price);
return ourdatabase.insert(DATABASE_TABLE, null, cv);
}
public String getvegetablename(long l)throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
Cursor c= ourdatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l,null, null, null, null);
if(c!= null){
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getvegetableprice(long l)throws SQLException {
String[] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
Cursor c= ourdatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l,null, null, null, null);
if(c!= null){
c.moveToFirst();
String name = c.getString(2);
return name;
}
return null;
}
public void updateentry(long lRow, String vegename, String vegeprice) throws SQLException {
// TODO Auto-generated method stub
ContentValues cvupdate = new ContentValues();
cvupdate.put(KEY_VEGETABLES, vegename);
cvupdate.put(KEY_PRICE, vegeprice);
ourdatabase.update(DATABASE_TABLE, cvupdate, KEY_ROWID + "=" + lRow, null);
}
public String getData() {
String [] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
Cursor C =ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = C.getColumnIndex(KEY_ROWID);
int ivegetable = C.getColumnIndex(KEY_VEGETABLES);
int iprice = C.getColumnIndex(KEY_PRICE);
for(C.moveToFirst(); !C.isAfterLast(); C.moveToNext()){
result = result + C.getString(iRow) + " " + C.getString(ivegetable) + " " + C.getString(iprice) + "\n";
}
return result;
}
public void deleteEntry(long lRow1) throws SQLException {
// TODO Auto-generated method stub
ourdatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);
}
}
答案 0 :(得分:0)
可以在声明期间使用值直接初始化字符串数组。但是,当您想通过调用方法初始化值时,应该遵循
String[] strArray = new String[5]; //Ex: 5 is the size of the array
Vegetablesdatabase info = new Vegetablesdatabase(this);
for(int i=0;i<strArray.length;i++){
strArray[i] = info.getvegetable();
}
//strArray is filled with values after the loop
请注意info.getvegetable()
应返回String字面值。如果您不想要固定大小的集合,请转到列表实现。
您必须使用ArrayList,因为您在初始化期间没有所需的大小
public ArrayList<String> getallvegetable() {
String [] columns = new String[]{KEY_VEGETABLES};
Cursor C =ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
ArrayList<String> result = new ArrayList<String>();
int ivegetable = C.getColumnIndex(KEY_VEGETABLES);
int iprice = C.getColumnIndex(KEY_PRICE);
for(C.moveToFirst(); !C.isAfterLast(); C.moveToNext()){
result.add(C.getString(ivegetable));
}
return result;
}
希望你现在明白!
答案 1 :(得分:0)
您可以使用章鱼提供的答案或将ID传递给
for(int i=0;i<strArray.length;i++){
strArray[i] = info.getvegetable(i);
}
或者你可以改变方法,使它返回一个字符串数组,如下所示
public String[] getvegetablenames()throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID,KEY_VEGETABLES,KEY_PRICE};
Cursor c= ourdatabase.query(DATABASE_TABLE, columns, null,null, null, null, null);
int i=0;
String[] values=new String[c.getCount()];
c.moveToFirst();
do{
values[i] = c.getString(1);
i++;
}while(c.moveToNext());
return values;
}
上面的代码可能有错误,但它足以让您开始使用