SQLite меняет макет списка - PullRequest
       1

SQLite меняет макет списка

0 голосов
/ 28 ноября 2011

Я работаю над учебником блокнота с сайта Android. Я создал базу данных с 2 записями: имя и адрес. Когда приложение запущено, отображается только имя. Я хочу отобразить имя и адрес. Я отредактировал свой метод filldata, но он не работает. Смотрите мой код ниже. Какие-либо предложения?

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (TITLE + BODY)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);
}

1 Ответ

2 голосов
/ 28 ноября 2011

В вашем R.layout.notes_row файле у вас должно быть два TextViews, один для заголовка и один для адреса, а затем вы напишите:

String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1, R.id.id_of_the_second_text};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);

Edit: R.layout.notes_row может быть что-то вроде этого:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/text1" 
 android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
<TextView android:id="@+id/text2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...