dimanche 12 juillet 2015

Android: New line character in ListView

I know there are many questions asked regarding the new line character but my problem is little different.

I have some records in SQLite Database and while inserting them i am using \n as a new line character. There are ways in which you can unescape them in a single textview but i am having a ListView and have textview in seperate xml layout file for list row.

I am using SimpleCursorAdapter to populate the ListView with the data from database. Here is my code

private void setAdapterFromDb() {
    db = dbc.getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db
                .rawQuery(
                        "SELECT _id,question_id,question from Test WHERE section = 'Reasoning';",
                        null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.close();
    }

    String columns[] = new String[]{"question"};
    int to[] = new int[]{R.id.tvQuestion};

    adapter = new SimpleCursorAdapter(rootView.getContext(),
            R.layout.list_row, cursor, columns, to, 0);
    lv2.setAdapter(adapter);
}

As you can see the adapter is populating the data in ListView and i can see "\n" in my textview, i don't know how to unescape it here, please help me.

Aucun commentaire:

Enregistrer un commentaire