简体   繁体   English

来自SQLiteDatabase Cursor的SimpleCursorAdapter在Spinner适配器中无法正确显示

[英]SimpleCursorAdapter from SQLiteDatabase Cursor not displaying properly in Spinner adapter

i'm trying to connect query a database and display the results in a Spinner. 我正在尝试连接查询数据库并在Spinner中显示结果。 below is what i have so far. 以下是我到目前为止所拥有的。 unfortunately the spinner only displays the last row that should have been returned from the query . 不幸的是,微调器仅显示应该从查询返回的最后一行 so from the example above, only "Elvis Presley" shows up in the list. 因此在上面的示例中,列表中仅显示“ Elvis Presley”。 i've tried using "Cursor.moveToFirst()" (as you can see) but that doesn't do it. 我已经尝试使用“ Cursor.moveToFirst()”(如您所见),但这没有做到。

i realized that i rebuild and update the database everytime i open it. 我意识到每次打开它都会重建和更新数据库。 i'm doing this on purpose for now. 我现在是故意这样做的。

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
                }
            });
        }
    }
}

You are putting values in Content Values. 您正在将值放入“内容值”中。

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

Anf finally u write a insert statement. 最后,您写了一条插入语句。 So, to my understanding essentially u put only one value in the ContentValues since others are overwritten. 因此,据我所知,您基本上只在ContentValues中输入了一个值,因为其他值被覆盖了。

You can also pull the db file using DDMS and open using SQLite browser to make that all entires are there in the DB 您也可以使用DDMS拉出db文件,然后使用SQLite浏览器打开数据库,以确保所有内容都在数据库中

You should try inserting each content value lik this. 您应该尝试插入每个内容值。 Pls try this so that all entries are made in the db. 请尝试这样,以便所有条目都在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM