[英]Android dialog not displaying data sqlite
我正在嘗試在對話框中顯示來自 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>
只要將填寫總數的行注釋掉,那么顯示的屏幕就可以了:
但是,一旦我取消注釋這些行以顯示總數,我就會得到:
問題是你的根布局是一個RelativeLayout。 所以,發生的事情是你的第二個 LinearLayout 掩蓋了第一個。
您可以通過兩種方式解決此問題:
據我了解,您希望在視圖頂部有一個固定的球員列表(行),在底部有一個相應的總得分列表(行)。 然后在這兩者之間,您需要一個已玩游戲的每手牌的擴展得分列表。 我對嗎?
編輯:好的,忽略我使用/擴展 HeaderViewListAdapter 的建議,因為 ListView 本身支持頁眉/頁腳並在內部使用 HeaderViewListAdapter(DOH.!!)。
請嘗試以下操作(但不確定代碼是否 100% 正確)...
執行您當前正在執行的操作以在lloPlayerNames
TextViews 中設置播放器名稱。 然后調整您的代碼以將lloTotalScores
TextViews 設置為包含從您的getTotalScores()
查詢返回的值。 然后使用類似...
LinearLayout llHeaderView = (LinearLayout) dialog.findViewById(R.id.lloPlayerNames);
lv.addHeaderView(llHeaderView);
...
LinearLayout llFooterView = (LinearLayout) dialog.findViewById(R.id.lloTotalScores);
lv.addFooterView(llFooterView);
然后你使用lv.setAdapter()
來輸入你的getScores()
查詢的結果。 注意:您必須在調用setAdapter()
之前設置 header 和頁腳
希望有幫助。
最后,我通過在數據列表下為我的總計創建一個新的 ListView 並用我創建的新 XML 填充它來解決這個問題。 我不知道這是否是 go 的最佳方式,但至少它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.