0

My title may seem a bit improper, but I really don't know how to formulate this question.

Basically I got an Database with three columns which holds Text fields.

I would like to extract all values from a single column and group them in a huge string while separating them with \n.

The final three strings should be stored in a single String array. So I decided to query the DB with the three column names and loop through the entire table:

    String[] columns = new String[] {KEY_DESCRIPTION, KEY_REPS, KEY_WEIGHT};
    Cursor c = sqlDB.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result[] = new String[3];
    
    int iDesc = c.getColumnIndex(KEY_DESCRIPTION);
    int iReps = c.getColumnIndex(KEY_REPS);
    int iWeight = c.getColumnIndex(KEY_WEIGHT);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())  //loop thru 
    {
        result[0] = result[0] + c.getString(iDesc) + "\n";
        result[1] = result[1] + c.getString(iReps) + "\n";
        result[2] = result[2] + c.getString(iWeight) + "\n";
    }       

    return result;

I previously used a similar approach to concatenate all three strings, but now I need to have some sort of separation between those three values.

After retrieving the values I would like to insert them separately into Android TextViews:

TextView1.setText(string[0]);

TextView2.setText(string[1]);

TextView3.setText(string[2]);

So my actual question is should I keep on using this or should I choose an other way, such as one array list per string, etc. ?

Community
  • 1
  • 1
  • Hi,simply use array list it will reduce efforts.get the values in seperate arraylist and then use it. – User10001 Jan 27 '14 at 16:19
  • Have you thought of using GroupConcat in your query so you don't have to loop through each entry? http://www.sqlite.org/lang_aggfunc.html – Rarw Jan 27 '14 at 16:21
  • The effort wasn't that huge... Will your solution boost the performance? I think that storing the strings just in one array list will even save some memory! – user3125138 Jan 27 '14 at 16:26

2 Answers2

0

You can write a small (private inner) class to hold these three strings and give them a meaningful name. Chances are that when you come back to your code, you won't remember what the indexes mean:

class Data{
  public String description = "";
  public String reps = "";
  public String weight = "";
}

Now, you can write something like this:

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
  result.desciption += c.getString(iDesc) + "\n";
  result.reps       += c.getString(iReps) + "\n";
  result.weight     += c.getString(iWeight) + "\n";
}
// ...
TextView1.setText(result.description);
TextView2.setText(result.reps);
TextView3.setText(result.weight);

A word on efficiency though: Concatenating strings like this is very slow. You'll want to use a StringBuilder instead. See: Is string concatenaion really that slow?

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
0

My suggestion is to stop using Arrays (as in []) and move to ArrayList where possible. As an ArrayList is dynamic you can keep adding data. So there would be no need to separate using /n. Either Change results to a 2 dimension ArrayList or make 3 separate ArrayLists for each column. Then something like

ResultsIdesc.add(c.getString(iDesc));
ResultsiReps.add(c.getString(iReps));

This will make it easier to write each to a text view.

nKn
  • 13,691
  • 9
  • 45
  • 62
sootie8
  • 48
  • 3