1

I have a database table with eight columns, with the following fields:

|_id|flag|HVID|Vname|Vdate|Vtime|Vcost|Vmedicine|

I am querying on this database to extract all records which belong to a certain '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;
    }

And in the Activity, I fetch values from the cursor and store them in an Array list:

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);

}

What I am getting in B is a redundant(all rows) string of values (My records can be of multiple rows) I want to separate these values into Name-Value pairs, I am confused as how to achieve that.

4 Answers 4

3

You can introduce a new class that would have the list of values for one record only like below:

public class Record {
    List<String> values = new ArrayList<String>();

    public List<String> getValues() {
        return values;
    }
}

Then in your loop fill-in a list of records:

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) {

    }
}

Now you can iterate through the field names and the values for each record to create the output you want:

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
Sign up to request clarification or add additional context in comments.

2 Comments

Gives me The method add(Record) in the type ArrayList<Record> is not applicable for the arguments (String)
@user2613996 You are calling add() with a string argument. You should call it with a Record. Please check first code sample.
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());

in table you will have the rows as an ArrayList from NameValuePairs

after, you can get a value from a row like

ArrayList<NameValuePair> row = table.get(0);
NameValuePair column = row.get(0);
String columnName = column.getName();
String columnValue = column.getValue();

3 Comments

This is an easier way out! Thank you!
What does the Table array list do?
the ArrayList<NameValuePair> repesents a row from the table. the table vairable hold the rows.
1

You can make B an ArrayList of HashMaps and store the pairs in B. Put the keys in the map as you extract them from mCursor in the loop. If B must be a string, use JSON format.

Comments

1

It is not exactly clear what You wanna do. If I am understanding You problem the right way, You get a String like this "Vname|Vdate|Vtime|Vcost|VmedicineVname|Vdate|Vtime|Vcost|Vmedicine"

but You want to have a single String for every row, like this:

  1. String "Vname|Vdate|Vtime|Vcost|Vmedicine"
  2. String "Vname|Vdate|Vtime|Vcost|Vmedicine"

If this is what You want, You can pass every Row to an ArrayList, and that ArrayList to an ArrayList, this looks similar to this:

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

and then when You get Your values from 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);

So You can get exactly one row as:

          for(int i=0;i<doubleArray.size();i++){

              String a = doubleArray.get(i)
          // now pass the String wherever You want
         }

but like I am said, I don´t know from Your explanation if this is what You want...

3 Comments

No those are my coulmn names, however I want aseparate string for each row!
sure this are Your column names, I know, but like I described, You should get the values from each column/row into one array.
I will get back here! :) Thanks mate for the guidance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.