Witajcie,
Próbuję pobrać z bazy danych kilka rekordów a następnie wyświetlić je w odpowiednio sformatowanym widoku. W tym celu w klasie DatabaseHelper stworzyłem sobie selecta pobierającego dane:
public Cursor viewResults() {
SQLiteDatabase db = this.getWritableDatabase();
String sqlSelect = "SELECT " + COL_2 + ", " +
COL_4 + ", " + COL_6 + ", " +
COL_7 + ", " + COL_8 + ", " +
COL_9 + ", " + COL_11 + ", " +
COL_13 + ", " + COL_14 + ", " +
COL_15 + ", " + COL_16 + " FROM " + TABLE_NAME;
Cursor select = db.rawQuery(sqlSelect, null);
if(select != null) {
select.moveToFirst();
}
return select;
}
Następnie otworzyłem widok listy w XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/playerName1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/playerName2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="169dp"
android:layout_marginEnd="169dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Round"
android:id="@+id/round"
android:layout_marginTop="26dp"
android:layout_below="@+id/playerName2"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Total"
android:id="@+id/total1"
android:layout_alignBottom="@+id/round"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Score"
android:id="@+id/score1"
android:layout_alignBottom="@+id/total1"
android:layout_toRightOf="@+id/total1"
android:layout_toEndOf="@+id/total1"
android:layout_marginLeft="20dp"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Coins"
android:id="@+id/coins1"
android:layout_alignTop="@+id/score1"
android:layout_toRightOf="@+id/score1"
android:layout_toEndOf="@+id/score1"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Queen"
android:id="@+id/queen1"
android:layout_alignTop="@+id/coins1"
android:layout_toRightOf="@+id/coins1"
android:layout_toEndOf="@+id/coins1"
android:layout_marginLeft="27dp"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Break"
android:id="@+id/beak1"
android:layout_alignTop="@+id/queen1"
android:layout_toRightOf="@+id/queen1"
android:layout_toEndOf="@+id/queen1"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Total"
android:id="@+id/total2"
android:layout_alignBottom="@+id/round"
android:layout_alignLeft="@+id/playerName2"
android:layout_alignStart="@+id/playerName2"
android:layout_marginLeft="28dp"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Score"
android:id="@+id/score2"
android:layout_alignTop="@+id/total2"
android:layout_toRightOf="@+id/total2"
android:layout_toEndOf="@+id/total2"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Coins"
android:id="@+id/coins2"
android:layout_alignTop="@+id/total2"
android:layout_toRightOf="@+id/score2"
android:layout_toEndOf="@+id/score2"
android:layout_marginLeft="24dp"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Queen"
android:id="@+id/queen2"
android:layout_alignTop="@+id/coins2"
android:layout_toRightOf="@+id/coins2"
android:layout_toEndOf="@+id/coins2"
android:layout_marginLeft="29dp"
android:layout_marginStart="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Break"
android:id="@+id/break2"
android:layout_alignTop="@+id/queen2"
android:layout_toRightOf="@+id/queen2"
android:layout_toEndOf="@+id/queen2"
android:layout_marginStart="18dp" />
</RelativeLayout>
W widoku swojej aktywności utworzyłem listę:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.user.carrom.Result"
tools:showIn="@layout/activity_result">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resultListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
I aby to wszystko obsłużyć utworzyłem w pliku aktywności metodę aby całość obsłużyć.
private void resultListView() {
Cursor cursor = matchDb.viewResults();
startManagingCursor(cursor);
String[] fromFieldName = new String[]
{matchDb.COL_2};
int[] toViewIDs = new int[]
{R.id.round};
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this,
R.layout.item_result,
cursor,
fromFieldName,
toViewIDs
);
ListView resultList = (ListView) findViewById(R.id.resultListView);
resultList.setAdapter(myCursorAdapter);
}
Póki co wrzuciłem tam tylko jedno pole aby sprawdzić jak działa i efektem działania jest następujący błąd:
62 2775-2775/? E/AndroidRuntime: FATAL EXCEPTION: main
12-28 19:42:35.862 2775-2775/? E/AndroidRuntime: Process: com.example.user.carrom, PID: 2775
12-28 19:42:35.862 2775-2775/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.carrom/com.example.user.carrom.Result}: java.lang.IllegalArgumentException: column '_id' does not exist
12-28 19:42:35.862 2775-2775/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
12-28 19:42:35.862 2775-2775/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-28 19:42:35.862 2775-2775/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
...
...
Pomożecie Panowie?