我有一个包含八列的数据库表,其中包含以下字段:
|_id|flag|HVID|Vname|Vdate|Vtime|Vcost|Vmedicine|
我正在查询此数据库以提取属于某个'HVID'的所有记录:
public Cursor fetchAllVac(String ID) {
String Key = ID;
Cursor mCursor = mDb.query(DATABASE_TABLE1, new String[] { IDx, FLAG,
HVID1, Vname1, VDate1, Vtime1, Vcost1, Vmedicine1 }, "HVID=?",
new String[] { Key }, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
在Activity中,我从游标中获取值并将它们存储在数组列表中:
public void Vacforshare() {
String B = null;
ArrayList<String> mArrayList = new ArrayList<String>();
mCursor = DBHelper.fetchAllVac(IDB);
if (mCursor.moveToFirst()) {
do {
try {
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("_id")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("flag")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("HVID")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vname")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vdate")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vtime")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vcost")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vmedicine")));
} catch (Exception h) {
}
} while (mCursor.moveToNext());
}
for (int i = 0; i < mArrayList.size(); i++) {
String G = (mArrayList.get(i));
B = B + G;
}
System.out.println("" + B);
}
我在B中得到的是一个冗余(所有行)值的字符串(我的记录可以是多行)我想将这些值分成名称 - 值对,我很困惑如何实现它。
答案 0 :(得分:3)
ArrayList<ArrayList<NameValuePair>> table = new ArrayList<ArrayList<NameValuePair>>();
if (mCursor.moveToFirst()) {
do {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
nameValuePairs.add(new BasicNameValuePair("_id",mCursor.getString(mCursor.getColumnIndex("_id"))));
//do this for the rest columns...
//...
//...
table.add(nameValuePairs);
} catch (Exception h) {
}
} while (mCursor.moveToNext());
在table
中您将ArrayList
的行NameValuePair
作为<{1}}
之后,您可以从
这样的行中获取值ArrayList<NameValuePair> row = table.get(0);
NameValuePair column = row.get(0);
String columnName = column.getName();
String columnValue = column.getValue();
答案 1 :(得分:3)
您可以引入一个新类,该类具有一个记录的值列表,如下所示:
public class Record {
List<String> values = new ArrayList<String>();
public List<String> getValues() {
return values;
}
}
然后在你的循环中填写一份记录列表:
ArrayList<Record> mArrayList = new ArrayList<Record>();
do {
try {
Record record = new Record();
List<String> values = record.getValues();
values.add(mCursor.getString(mCursor.getColumnIndex("_id")));
...
mArrayList.add(record);
} catch (Exception h) {
}
}
现在,您可以遍历每个记录的字段名称和值,以创建所需的输出:
String[] names = new String[] {"_id", "flag", ....};
for (int i = 0; i < mArrayList.size(); i++) {
Record record = mArrayList.get(i);
String current = "";
List<String> values = record.getValues();
for (int j = 0; j < values.size(); j++) {
String fieldName = names[j];
String s = values.get(j);
current += " " + fieldName + "=" + s;
}
B = B + "[" + current.trim() + "]";
}
System.out.println(B); // will print: [_id=value1 flag=value2 ...][_id=value1 flag=value2 ...] etc
答案 2 :(得分:1)
你可以使B成为HashMaps的ArrayList并将这些对存储在B中。当你从循环中的mCursor中提取它们时,将它们放在地图中。如果B必须是字符串,请使用JSON格式。
答案 3 :(得分:1)
目前还不清楚你想做什么。如果我以正确的方式理解你的问题,你得到这样的字符串 “VNAME | Vdate | VTIME | Vcost | VmedicineVname | Vdate | VTIME | Vcost | Vmedicine”
但是你希望每一行都有一个字符串,如下所示:
如果这是你想要的,你可以将每一行传递给一个ArrayList,并将该ArrayList传递给一个ArrayList,这看起来类似于:
private ArrayList<ArrayList<String>> doubleArray = new ArrayList<ArrayList<String>>();
然后当您从DB获取您的值时:
ArrayList<String> mArrayList = new ArrayList<String>();
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("_id")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("flag")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("HVID")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vname")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vdate")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vtime")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vcost")));
mArrayList.add(mCursor.getString(mCursor
.getColumnIndex("Vmedicine")));
doubleArray.add(mArrayList);
所以你可以得到一行:
for(int i=0;i<doubleArray.size();i++){
String a = doubleArray.get(i)
// now pass the String wherever You want
}
但就像我说的那样,我不知道你的解释是否这就是你想要的......