开发者

return db.query to listeview (unless theres something better?)

I have in my DBAdapter, a getAll which should pull all the data from the database:

public Cursor getAllFinalscores()
{
    return db.query(DATABASE_TABLE, new String[] {
            KEY_ROWID,
            KEY_DATE,
            KEY_FINALSCORE,
            KEY_HOLESPLAYED},
            null, null, null, null, null);
}

and right now, I have it so that when a user wants to view all the data (from the main activity) it pulls it all as follows:

public void DisplayFinalscore(Cursor c)
{
    Toast.makeText(this,
            "DATE: " + c.getString(1) + "\n" +
            "FINALSCORE: " + c.getString(2) + "\n" +
            "HOLESPLAYED: " + c.getString(3),
            Toast.LENGTH_LONG).show();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.past_games:
            DBAdapter db = new DBAdapter(this);

            db.open();
            Cursor c = db.getAllFinalscores();
            if (c.moveToFirst())
            {
                do {
            开发者_运维知识库        DisplayFinalscore(c);
                }
                while (c.moveToNext());
            }
            db.close();

        }
    return true;
}

This works, however, it displays them all as little messages that pop up. I am looking to display them in a list on a separate screen.

I created a new class (PastGames) which looks like the following:

public class PastGames extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pastgames);

        DBAdapter db = new DBAdapter(this);

        db.open();
        Cursor c = db.getAllFinalscores();
        startManagingCursor(c);

        String[] from = new String[] { DBAdapter.KEY_DATE};
        int[] to = new int[] {R.id.text1};

        SimpleCursorAdapter games = new SimpleCursorAdapter(this, layout.pastgames_row, c, from, to);
        setListAdapter(games);
        db.close();
    }
}

I also have my xml files:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ListView android:id="@id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<TextView android:id="@id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_games"/>

and the row layout

<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Could help me with what I need to do in order to get that info into those layouts?

Thanks!


Okay, a few things:

First, to access the Database, you should use a SQLiteOpenHelper. It helps you read/write to it and create/update the Database.

Also, the startManagingCursor()-method is deprecated and shouldn't be used anymore. If you need to close your cursor, do it manually.

Don't open your Database-connection in the onCreate-method as it's only called when the Activity is first created. Use the onStart-method instead. Also, don't use the onCreate-method to populate the List. Create a new method for this and call it in the onStart-method, too. If the Activity gets backgrounded and foregrounded again, there won't be a problem with older data.

Also, if you have a ListActivity, you don't need a Layout containing a ListView. This is done automatically.

That's all I see for the moment. If you're getting any errors or exceptions, please post those messages, too. See if this gets you any further.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜