我正在尝试在对话框中显示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>
只要填写总数的行被注释掉,显示的屏幕就可以了:
但是,只要我取消评论这些行以显示总数,我就会得到这个:
答案 0 :(得分:1)
问题是您的根布局是RelativeLayout。所以,正在发生的事情是你的第二个LinearLayout覆盖了第一个。
您可以通过以下两种方式解决:
答案 1 :(得分:1)
根据我的理解,您需要在视图顶部添加固定列表(行)的玩家,并在底部添加相应的总分数列表(行)。然后在中间,你想要一个已经玩过的游戏的每一手的分数列表。我是对的吗?
编辑:好的,忽略我使用/扩展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()
希望有所帮助。
答案 2 :(得分:1)
最后,我通过在数据库中为我的总计创建一个新的ListView并使用我创建的新XML填充它来解决这个问题。 我不知道这是否是最佳方式,但至少它是有效的。