1

我有一个包含八列的数据库表,其中包含以下字段:

|_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 中得到的是一个冗余(所有行)值字符串(我的记录可以是多行)我想将这些值分成名称-值对,我对如何实现这一点感到困惑。

4

4 回答 4

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
于 2013-08-01T12:22:31.633 回答
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来自NameValuePairs的行

之后,您可以从一行中获取一个值,例如

ArrayList<NameValuePair> row = table.get(0);
NameValuePair column = row.get(0);
String columnName = column.getName();
String columnValue = column.getValue();
于 2013-08-01T12:16:10.220 回答
1

您可以使 B 成为 HashMap 的 ArrayList 并将对存储在 B 中。将键放入映射中,同时从循环中的 mCursor 中提取它们。如果 B 必须是字符串,请使用 JSON 格式。

于 2013-08-01T12:16:57.847 回答
1

目前还不清楚你想做什么。如果我以正确的方式理解您的问题,您会得到这样的字符串 "Vname|Vdate|Vtime|Vcost|VmedicineVname|Vdate|Vtime|Vcost|Vmedicine"

但是您希望每一行都有一个字符串,如下所示:

  1. 字符串 "Vname|Vdate|Vtime|Vcost|Vmedicine"
  2. 字符串 "Vname|Vdate|Vtime|Vcost|Vmedicine"

如果这是您想要的,您可以将每一行传递给一个 ArrayList,然后将该 ArrayList 传递给一个 ArrayList,这看起来类似于:

      private ArrayList<ArrayList<String>> doubleArray = new ArrayList<ArrayList<String>>();

然后当您从数据库中获取您的值时:

       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
         }

但就像我说的那样,我不知道你的解释是否是你想要的......

于 2013-08-01T12:18:03.567 回答