Search code examples
javaarrayssqliteandroid-listviewstringbuffer

How to store stringbuffer into string array?


Hi i am reading sqlite db data and store into string buffer. I want to display it in the list view in android. i am facing little problem. please help me to solve this issue.

String studentno=cursor.getString(1);
String date=cursor.getString(2);
buffer.append("student: "+studentno+"  Time:"+date+"\n");
String data=buffer.toString();
Log.d("student: "+studentno+"  Time:"+date);

the output is:student: 1234 Time:12:13 student: 1234 Time:12:14 student: 1234 Time:12:15

I want to store string buffer like this values[0]=student: 1234 Time:12:13 values[1]=student: 1234 Time:12:14 values[2]=student: 1234 Time:12:15

i tried below coding but its not working.

String[] values = data.split("");
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}

is there any way to do this. thanks in advance.


Solution

  • You seem to be adding the data into the buffer with new line delimiter at the end. Splitting them should look like this: data.split("\n");

    This does not seem like a good idea, however. Mainly because you can have a very large data set and split is an expensive operation.

    Why not create an ArrayList<String> and call list.add("student: " + studentno + " Time: " + date) or some other more efficient structure?

    Code:

    List<String> list = new ArrayList<String>();
    
    String studentno=cursor.getString(1);
    String date=cursor.getString(2);
    list.add("student: " + studentno + " Time: " + date);