Android 对话框不显示数据 sqlite

发布于 2024-11-05 01:14:27 字数 7818 浏览 0 评论 0原文

我正在尝试在对话框中显示 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:

enter image description here

However, as soon as I un-comment those lines to display the total I get this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉默的熊 2024-11-12 01:14:27

据我了解,您需要在视图顶部有一个固定的玩家列表(行),并在底部有一个相应的总得分列表(行)。然后,在这之间,您需要一份已玩游戏的每手牌的扩展分数列表。我说得对吗?

编辑:好的,忽略我使用/扩展 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() 之前设置页眉和页脚

希望有所帮助。

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 the lloTotalScores TextViews to contain the values returned from your getTotalScores() query. Then use something like...

LinearLayout llHeaderView = (LinearLayout) dialog.findViewById(R.id.lloPlayerNames);
lv.addHeaderView(llHeaderView);

...

LinearLayout llFooterView = (LinearLayout) dialog.findViewById(R.id.lloTotalScores);
lv.addFooterView(llFooterView);

Then you use lv.setAdapter() to put in the results from your getScores() query. NOTE: you MUST set the header and footer before calling setAdapter()

Hope that helps.

少跟Wǒ拽 2024-11-12 01:14:27

问题是你的根布局是一个RelativeLayout。所以,发生的事情是你的第二个 LinearLayout 覆盖了第一个。

您可以通过两种方式解决这个问题:

  • 将根布局设置为 LinearLayout。这样,第二个 LinearLayout 将定位在第一个 LinearLayout 之后,而不是在它的上面。
  • 在第二个线性布局中添加“上边距”规则,例如 100 像素。这会将其向下推并暴露第一个布局......

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:

  • Make your root layout a LinearLayout. This way, the second LinearLayout will be positioned after the first, instead of on top of it.
  • Add a "Margin-top" rule of say, 100px, to the 2nd linear layout. This will push it down and expose the 1st layout...
风吹过旳痕迹 2024-11-12 01:14:27

最后,我通过在数据列表下为总计创建一个新的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文