SQLiteDatabase中的SimpleCursorAdapter光标无法在Spinner适配器中正确显示

时间:2010-12-07 18:37:37

标签: java android sqlite

我正在尝试连接查询数据库并在Spinner中显示结果。以下是我到目前为止的情况。不幸的是,微调器只显示应该从查询返回的最后一行。所以从上面的例子中,只有“Elvis Presley”出现在列表中。我尝试过使用“Cursor.moveToFirst()”(正如你所看到的那样),但是没有这样做。

我意识到每次打开它都会重建和更新数据库。我现在正故意这样做。

package com.conceptualsystems.android4api.sms;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;

import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Window;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.SimpleCursorAdapter;


public class smsActivity extends Activity
{
    private static class smsDbOpenHelper extends SQLiteOpenHelper {

        smsDbOpenHelper(Context context) {
            super(context,smsDbSchema.DATABASE_NAME, null, smsDbSchema.DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(smsDbSchema.CustomerSchema.CREATE_TABLE);
            db.execSQL(smsDbSchema.ProductSchema.CREATE_TABLE);
        }

        @Override
        public void onOpen(SQLiteDatabase db) {
            //open db
            db.execSQL("DROP TABLE IF EXISTS " + smsDbSchema.CustomerSchema.TABLE_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + smsDbSchema.ProductSchema.TABLE_NAME);
            this.onCreate(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            //upgrade db
        }
    }

    private smsDbOpenHelper mDbHelper;
    private SQLiteDatabase mDb;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        mDbHelper = new smsDbOpenHelper(getApplicationContext());
        mDb = mDbHelper.getReadableDatabase();

        //fill customer table with some fake data
            ContentValues cv = new ContentValues();
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Charles Keith Gilliam");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Jarrod Martin");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "John Lennon");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Sammy Hagar");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "David Lee Roth");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Keith Richards");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Steven Tyler");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Brent Michaels");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Eddie Veder");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Kurt Cobain");
            cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Elvis Presley");
            cv.put(smsDbSchema.CustomerSchema._ID, "0298437598745");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
            mDb.insert(smsDbSchema.CustomerSchema.TABLE_NAME, null, cv);

            //cv.

        setScreen(R.layout.inbound);

        Spinner custSpn = (Spinner)findViewById(R.id.cust_spn);
        Cursor custCur = null;
        try {
            custCur = mDb.query(smsDbSchema.CustomerSchema.TABLE_NAME, null, null, null, null, null, null);
        } catch(Exception e) {
            Log.e("smsdb", e.toString());
        }
        custCur.moveToFirst();
        startManagingCursor(custCur);
        SimpleCursorAdapter qc = new SimpleCursorAdapter(
            this,
            android.R.layout.simple_spinner_item,
            custCur,
            new String[] {smsDbSchema.CustomerSchema.COLUMN_NAME},
            new int[] {android.R.id.text1}
        );
        qc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        custSpn.setAdapter(qc);

        Spinner prdSpn = (Spinner)findViewById(R.id.prd_spn);
        Cursor prdCur = null;
        try {
            prdCur = mDb.query(smsDbSchema.ProductSchema.TABLE_NAME, null, null, null, null, null, null);
        } catch(Exception e) {
            Log.e("smsdb", e.toString());
        }
        prdCur.moveToFirst();
        startManagingCursor(prdCur);
        qc = new SimpleCursorAdapter(
            this,
            android.R.layout.simple_spinner_item,
            prdCur,
            new String[] {smsDbSchema.ProductSchema.COLUMN_NAME},
            new int[] {android.R.id.text1}
        );
        qc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        prdSpn.setAdapter(qc);

        mDb.close();
    }

    public void setScreen(int resource) {
        setContentView(resource);

        if(resource==R.layout.inbound) {
            //setup main entry screen
            final Button transmit = (Button)findViewById(R.id.transmit_btn);
            final Button clear = (Button)findViewById(R.id.clear_btn);
            transmit.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // transmit data ////////
                    /////////////////////////
                }
            });
            clear.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    //clear fields
                }
            });
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您将值放在内容值中。

ContentValues cv = new ContentValues();
cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Charles Keith Gilliam");
..
..
..

Anf最后你写了一个插入语句。所以,根据我的理解,基本上你只在ContentValues中放入一个值,因为其他值被覆盖。

您也可以使用DDMS提取db文件并使用SQLite浏览器打开以使数据库中存在所有内容

您应该尝试插入每个内容值。请尝试这样,以便所有条目都在db中生成。

cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Charles Keith Gilliam");
cv.put(smsDbSchema.CustomerSchema._ID, "7585684317298");
mDb.insert(smsDbSchema.CustomerSchema.TABLE_NAME, null, cv);

cv.put(smsDbSchema.CustomerSchema.COLUMN_NAME, "Jarrod Martin");
cv.put(smsDbSchema.CustomerSchema._ID, "0298437598745");
mDb.insert(smsDbSchema.CustomerSchema.TABLE_NAME, null, cv);