Я пытаюсь отобразить данные из базы данных SQLite в диалоговом окне.
Данные представляют собой список баллов.
Все работает хорошо до тех пор, пока я не добавлю итоговую строку, а затем она станет настоящей неудачей.
Это источник:
DBAdaptor dbAdaptor;
Cursor scoresCursor;
Cursor totalsCursor;
//ScoresDisplayerAdaptor scoresDisplayerAdaptor;
dbAdaptor = new DBAdaptor(this);
dbAdaptor.open();
scoresCursor = dbAdaptor.getScores();
totalsCursor = dbAdaptor.getTotalScores();
startManagingCursor(scoresCursor);
startManagingCursor(totalsCursor);
//scoresDisplayerAdaptor = new ScoresDisplayerAdaptor(this,scoresCursor);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.scores);
ListView lv = (ListView)dialog.getWindow().findViewById(R.id.lvwScores);
((TextView) dialog.findViewById(R.id.tvwPlayer1Name)).setText(players[0].playerName);
((TextView) dialog.findViewById(R.id.tvwPlayer2Name)).setText(players[1].playerName);
switch (settings.getNumberOfPlayers())
{
case 2:
lv.setAdapter(new SimpleCursorAdapter(this, R.layout.playerscoresrow, scoresCursor,
new String[] {DBAdaptor.KEY_PLAYER1_SCORE,DBAdaptor.KEY_PLAYER2_SCORE},
new int[] {R.id.tvwPlayer1Score, R.id.tvwPlayer2Score }
));
break;
case 3:
((TextView) dialog.findViewById(R.id.tvwPlayer3Name)).setText(players[2].playerName);
lv.setAdapter(new SimpleCursorAdapter(this, R.layout.playerscoresrow, scoresCursor,
new String[] {DBAdaptor.KEY_PLAYER1_SCORE,DBAdaptor.KEY_PLAYER2_SCORE,
DBAdaptor.KEY_PLAYER3_SCORE},
new int[] {R.id.tvwPlayer1Score, R.id.tvwPlayer2Score,
R.id.tvwPlayer3Score}
));
break;
case 4:
((TextView) dialog.findViewById(R.id.tvwPlayer3Name)).setText(players[2].playerName);
((TextView) dialog.findViewById(R.id.tvwPlayer4Name)).setText(players[3].playerName);
lv.setAdapter(new SimpleCursorAdapter(this, R.layout.playerscoresrow, scoresCursor,
new String[] {DBAdaptor.KEY_PLAYER1_SCORE,DBAdaptor.KEY_PLAYER2_SCORE,
DBAdaptor.KEY_PLAYER3_SCORE,DBAdaptor.KEY_PLAYER4_SCORE},
new int[] {R.id.tvwPlayer1Score, R.id.tvwPlayer2Score,
R.id.tvwPlayer3Score, R.id.tvwPlayer4Score }
));
}
/*
lv.setAdapter(new SimpleCursorAdapter(this, R.layout.scores, totalsCursor,
new String[] {DBAdaptor.KEY_PLAYER1_TOTAL,DBAdaptor.KEY_PLAYER1_TOTAL,
DBAdaptor.KEY_PLAYER1_TOTAL,DBAdaptor.KEY_PLAYER1_TOTAL},
new int[] {R.id.tvwPlayer1Total, R.id.tvwPlayer2Total,
R.id.tvwPlayer3Total, R.id.tvwPlayer4Total }
));*/
dialog.setTitle("Scores");
Button aButton = (Button)dialog.findViewById(R.id.btnOK);
aButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
askNewHand();
} });
dialog.show();
dbAdaptor.close();
Где два курсора:
public Cursor getScores(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_PLAYER1_SCORE,
KEY_PLAYER2_SCORE,
KEY_PLAYER3_SCORE,
KEY_PLAYER4_SCORE
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getTotalScores() {
return db.rawQuery(
"SELECT 0 as _id, SUM(" + KEY_PLAYER1_SCORE + ") as " + KEY_PLAYER1_TOTAL + ", " +
"SUM(" + KEY_PLAYER2_SCORE + ") as " + KEY_PLAYER2_TOTAL + ", " +
"SUM(" + KEY_PLAYER3_SCORE + ") as " + KEY_PLAYER3_TOTAL + ", " +
"SUM(" + KEY_PLAYER4_SCORE + ") as " + KEY_PLAYER4_TOTAL + " " +
"FROM " + DATABASE_TABLE,null);
}
Основной XML выглядит так:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/lloPlayerNames" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvwPlayer1Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginRight="14.5sp"/>
<TextView
android:id="@+id/tvwPlayer2Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginRight="14.5sp"/>
<TextView
android:id="@+id/tvwPlayer3Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginRight="14.5sp"/>
<TextView
android:id="@+id/tvwPlayer4Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginRight="14.5sp"/>
</LinearLayout>
<Button android:id="@+id/btnOK"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_weight="1"
android:text="OK" android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<LinearLayout android:id="@+id/lloTotalScores" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_above="@id/btnOK">
<TextView android:id="@+id/tvwPlayer1Total" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer2Total" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer3Total" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer4Total" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="14.5sp"/>
</LinearLayout>
<ListView
android:id="@+id/lvwScores"
android:layout_below="@id/lloPlayerNames"
android:layout_above="@id/lloTotalScores"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Пока строки, заполняющие итоги, закомментированы, тогда отображаемый экран в порядке:
Однако, как только я откомментирую эти строки для отображения итогового значения, я получаю следующее: