Android 对话框不显示数据 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>
只要注释掉填充总计的行,那么显示的屏幕就可以了:
但是,一旦我取消注释这些行以显示总数,我就会得到:
I am trying to display data from an SQLite database in a dialog.
The data is a list of scores.
All works well until I add a totals line and then it goes real haywire.
This is the source:
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();
Where the two cursors are:
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);
}
The main XML looks like this:
<?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>
As long as the lines filling the totals are commented out then the screen displayed is ok:
However, as soon as I un-comment those lines to display the total I get this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解,您需要在视图顶部有一个固定的玩家列表(行),并在底部有一个相应的总得分列表(行)。然后,在这之间,您需要一份已玩游戏的每手牌的扩展分数列表。我说得对吗?
编辑:好的,忽略我使用/扩展 HeaderViewListAdapter 的建议,因为 ListView 本身支持页眉/页脚并在内部使用 HeaderViewListAdapter (DOH!!!)。
尝试以下操作(但不确定代码是否 100% 正确)...
执行您当前正在执行的操作,在
lloPlayerNames
TextView 中设置玩家名称。然后调整您的代码以将lloTotalScores
TextView 设置为包含从getTotalScores()
查询返回的值。然后使用类似...然后使用
lv.setAdapter()
放入getScores()
查询的结果。注意:您必须在调用 setAdapter() 之前设置页眉和页脚希望有所帮助。
From what I understand, you want a fixed list (row) of players at the top of your view plus a corresponding list (row) of their total scores at the bottom. Then inbetween, you want an expanding list of scores for each hand of a game that has been played. Am I right?
EDIT: OK, ignore my suggestion of using / extending HeaderViewListAdapter as ListView supports headers/footers natively and uses HeaderViewListAdapter internally (DOH!!!).
Try the following (not sure the code is 100% correct though)...
Do what you're currently doing to set the player names in the
lloPlayerNames
TextViews. Then adjust your code to set thelloTotalScores
TextViews to contain the values returned from yourgetTotalScores()
query. Then use something like...Then you use
lv.setAdapter()
to put in the results from yourgetScores()
query. NOTE: you MUST set the header and footer before callingsetAdapter()
Hope that helps.
问题是你的根布局是一个RelativeLayout。所以,发生的事情是你的第二个 LinearLayout 覆盖了第一个。
您可以通过两种方式解决这个问题:
The problem is that your root layout is a RelativeLayout. So, what's happening is your second LinearLayout is covering up the first one.
You can solve this two ways:
最后,我通过在数据列表下为总计创建一个新的 ListView 并用我创建的新 XML 填充它来解决了这个问题。
我不知道这是否是最佳方法,但至少它有效。
In the end I solved this by creating a new ListView for my totals under the one for the data and populating it with a new XML that I created.
I don't know if this is the optimum way to go but at least it works.