Android对话框未显示数据sqlite
我试图在对话框中显示来自SQLite数据库的数据。 数据是一个分数列表。 一切都很好,直到我添加了一行,然后它就真的失控了 这是来源:Android对话框未显示数据sqlite,android,sqlite,dialog,Android,Sqlite,Dialog,我试图在对话框中显示来自SQLite数据库的数据。 数据是一个分数列表。 一切都很好,直到我添加了一行,然后它就真的失控了 这是来源: DBAdaptor dbAdaptor; Cursor scoresCursor; Cursor totalsCursor; //ScoresDisplayerAdaptor scoresDisplayerAdaptor; dbAdaptor = new DBAdapt
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>
只要填写总计的行被注释掉,则显示的屏幕正常:
但是,只要我取消注释这些行以显示总计,我就会得到以下结果:
问题在于您的根布局是相对的。所以,现在发生的是你的第二次线性布局掩盖了第一次 您可以通过两种方式解决此问题:
- 使根布局成为线性布局。这样,第二个线性布局将位于第一个线性布局之后,而不是位于第一个线性布局之上
- 在第二个线性布局中添加“边距顶部”规则,例如100px。这将向下推它并显示第一个布局
- 使根布局成为线性布局。这样,第二个线性布局将位于第一个线性布局之后,而不是位于第一个线性布局之上
- 在第二个线性布局中添加“边距顶部”规则,例如100px。这将向下推它并显示第一个布局
lloPlayerNames
textView中设置播放器名称。然后调整代码,将lloTotalScores
textview设置为包含从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()
希望这能有所帮助。据我所知,您希望在视图的顶部有一个固定的玩家列表(行),在底部有一个相应的玩家总分列表(行)。然后,在这中间,您需要一个扩展列表,列出已玩游戏的每一手的分数。我说得对吗
编辑:好的,忽略我关于使用/扩展HeaderViewListAdapter的建议,因为ListView本机支持页眉/页脚,并在内部使用HeaderViewListAdapter(DOH!!!)
请尝试以下操作(但不确定代码是否100%正确)
执行当前正在执行的操作,在lloPlayerNames
textView中设置播放器名称。然后调整代码,将lloTotalScores
textview设置为包含从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()
希望能有所帮助。最后,我解决了这个问题,在数据列表下为我的总计创建了一个新的列表视图,并用我创建的新XML填充它。
我不知道这是否是最好的方法,但至少它是有效的。最后,我解决了这个问题,在数据列表下为我的总计创建了一个新的列表视图,并用我创建的新XML填充它。
我不知道这是否是最好的方式,但至少它是有效的。问题是我必须使它成为一个相对的布局,否则整个事情都不起作用。尝试转换为线性,但这完全搞砸了。@theblitz-尝试使用两个布局参数使根元素成为线性布局,如-android:layout\u height=“fill\u parent”&android:layout\u width=“fill\u parent”。问题是我必须使其成为一个相对布局,否则整个事情都不起作用。尝试转换为线性,但这完全搞砸了。@theblitz-尝试使用两个布局参数使根元素成为线性布局,如-android:layout\u height=“fill\u parent”&android:layout\u width=“fill_parent”。@theblitz:查看我对我的回答的编辑我是将页眉和页脚保留在分数xml中,还是分开保存?我问,因为我现在在setadapter命令上得到一个ClassCastException。@theblitz:尝试将按钮移动到xml布局文件的最底部(但显然仍在RelatieLayout中)。还是一样。我明天会试一试并报告。@theblitz:我看到你找到了一个解决办法。我认为它应该仍然适用于页眉/页脚,尽管我将页眉/页脚线性布局放在一个单独的布局文件中,并用代码进行膨胀。然后,主布局文件将只有ListView和按钮(可能是线性布局,而不是相对布局)。如果您对所获得的感到满意,那就太好了。@theblitz:查看我对我的回答的编辑我是将页眉和页脚保留在我的分数xml中,还是将它们放在单独的分数xml中?我问,因为我现在在setAdapter命令上得到一个ClassCastException。@theblitz:尝试将您的按钮移动到xml布局文件的最底部(但显然仍然在RelatieLayout中)。仍然是一样的。我明天会尝试一下,然后回来报告。@theblitz:我看到你找到了一个解决方法。我认为它应该仍然适用于页眉/页脚,尽管我会将页眉/页脚线性布局放在一个单独的布局文件中,并用代码进行膨胀。然后,主布局文件就没有了