Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:02:10+00:00 2026-05-27T21:02:10+00:00

I’ve tried out a couple different ways of loading the spinner from SQLite database,

  • 0

I’ve tried out a couple different ways of loading the spinner from SQLite database, this method seems to be the simplest and easiest. The only thing is since i have added in a couple of my own variables (three more). When I run the edited version to fit my testing needs everything runs correctly except the spinner doesn’t fill with data from the database

enter image description here

SpinnerLoad Class

public class SpinnerLoad extends Activity implements OnClickListener {
    private Db thisTestDBAdapter;
    Button save;
    EditText one, two, three;
    Spinner spinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinnerform);
        save = (Button) findViewById(R.id.spinLoad_save);
        save.setOnClickListener(this);
        one = (EditText) findViewById(R.id.spinLoad_serial);
        two = (EditText) findViewById(R.id.spinLoad_name);
        three = (EditText) findViewById(R.id.spinLoad_place);
        fillData();
        spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
        try {
            spinner.setOnItemSelectedListener(new Person());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Person implements OnItemSelectedListener {
        static String personReturn;
        static boolean personTest = false;

        @Override
        public void onItemSelected(AdapterView<?> x, View y, int z, long w) {
            personReturn = (x.getItemAtPosition(z)).toString();
            personTest = true;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            personTest = false;
        }
    }

    private void fillData() {
        try {
            Cursor coloursCursor;
            spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
            coloursCursor = thisTestDBAdapter.fetchAllColours();
            startManagingCursor(coloursCursor);

            String[] from = new String[] { thisTestDBAdapter.KEY_TWO };
            int[] to = new int[] { R.id.tvDBViewRow };

            SimpleCursorAdapter colourAdapter = new SimpleCursorAdapter(this,
                    R.layout.db_view_row, coloursCursor, from, to);

            spinner.setAdapter(colourAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.spinLoad_save:
            String bx = two.getText().toString();
            /**/
            String ax = one.getText().toString();
            String cx = three.getText().toString();
            String returned = Person.personReturn;

            Db entry = new Db(this);
            entry.open();
            entry.createEntry(ax, bx, cx, returned);
            entry.close();

            fillData();
            /**/
            break;
        }
    }
}

Db SQL helper class

public class Db {

    private static final String TAG = "SpinnerDBHelper";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    private static final int DATABASE_VERSION = 2;
    private final Context mCtx;
    /**/
    private static final String KEY_DB = "DataBaseName";
    private static final String KEY_TABLE = "DbTable";
    public static final String KEY_ROWID = "_rowid"; // 0
    public static final String KEY_ONE = "serial";
    public static final String KEY_TWO = "name";
    public static final String KEY_THREE = "place";
    public static final String KEY_FOUR = "returned";
    /**/

    private static final String DATABASE_CREATE = "CREATE TABLE DbTable (_rowid INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "serial TEXT NOT NULL, "+ "name TEXT NOT NULL, " + "place TEXT NOT NULL, " + "returned TEXT NOT NULL);";  

    public Db(Context ctx) {
        this.mCtx = ctx;
    }

    public Db open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public long createEntry(String ax, String bx, String cx, String returned) {
        if (mDb == null) {
            this.open();
        }

        ContentValues cv = new ContentValues();
        cv.put(KEY_ONE, ax);
        cv.put(KEY_TWO,  bx);
        cv.put(KEY_THREE,  cx);
        cv.put(KEY_FOUR, returned);

        return mDb.insert(KEY_TABLE, null, cv);
    }

    public boolean deleteEntry(long rowId) {
        return mDb.delete(KEY_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public boolean deleteAll() {
        if (mDb == null) {
            this.open();
        }

        return mDb.delete(KEY_TABLE, null, null) > 0;
    }

    public Cursor fetchAllColours() {
        if (mDb == null) {
            this.open();
        }

        return mDb.query(KEY_TABLE, new String[] { KEY_ROWID, KEY_TWO},
                null, null, null, null, null);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, KEY_DB, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + KEY_TABLE);
            onCreate(db);
        }
    }
}

LOGCAT

12-26 23:39:33.409: W/System.err(30879): java.lang.NullPointerException
12-26 23:39:33.409: W/System.err(30879):    at com.my.examples.SpinnerLoad.fillData(SpinnerLoad.java:59)
12-26 23:39:33.409: W/System.err(30879):    at com.my.examples.SpinnerLoad.onCreate(SpinnerLoad.java:30)
12-26 23:39:33.418: W/System.err(30879):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-26 23:39:33.418: W/System.err(30879):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-26 23:39:33.418: W/System.err(30879):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-26 23:39:33.418: W/System.err(30879):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-26 23:39:33.418: W/System.err(30879):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-26 23:39:33.418: W/System.err(30879):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 23:39:33.418: W/System.err(30879):    at android.os.Looper.loop(Looper.java:123)
12-26 23:39:33.418: W/System.err(30879):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-26 23:39:33.428: W/System.err(30879):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 23:39:33.428: W/System.err(30879):    at java.lang.reflect.Method.invoke(Method.java:521)
12-26 23:39:33.428: W/System.err(30879):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-26 23:39:33.428: W/System.err(30879):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-26 23:39:33.428: W/System.err(30879):    at dalvik.system.NativeStart.main(Native Method)
12-26 23:40:00.628: E/Database(30879): Error inserting place=zyxwvutsr returned=null serial=123456789 name=abcdefghij
12-26 23:40:00.628: E/Database(30879): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
12-26 23:40:00.628: E/Database(30879):  at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
12-26 23:40:00.628: E/Database(30879):  at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
12-26 23:40:00.628: E/Database(30879):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
12-26 23:40:00.628: E/Database(30879):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
12-26 23:40:00.628: E/Database(30879):  at com.my.examples.Db.createEntry(Db.java:56)
12-26 23:40:00.628: E/Database(30879):  at com.my.examples.SpinnerLoad.onClick(SpinnerLoad.java:86)
12-26 23:40:00.628: E/Database(30879):  at android.view.View.performClick(View.java:2408)
12-26 23:40:00.628: E/Database(30879):  at android.view.View$PerformClick.run(View.java:8816)
12-26 23:40:00.628: E/Database(30879):  at android.os.Handler.handleCallback(Handler.java:587)
12-26 23:40:00.628: E/Database(30879):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-26 23:40:00.628: E/Database(30879):  at android.os.Looper.loop(Looper.java:123)
12-26 23:40:00.628: E/Database(30879):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-26 23:40:00.628: E/Database(30879):  at java.lang.reflect.Method.invokeNative(Native Method)
12-26 23:40:00.628: E/Database(30879):  at java.lang.reflect.Method.invoke(Method.java:521)
12-26 23:40:00.628: E/Database(30879):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-26 23:40:00.628: E/Database(30879):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-26 23:40:00.628: E/Database(30879):  at dalvik.system.NativeStart.main(Native Method)
12-26 23:40:00.628: W/System.err(30879): java.lang.NullPointerException
12-26 23:40:00.639: W/System.err(30879):    at com.my.examples.SpinnerLoad.fillData(SpinnerLoad.java:59)
12-26 23:40:00.639: W/System.err(30879):    at com.my.examples.SpinnerLoad.onClick(SpinnerLoad.java:89)
12-26 23:40:00.639: W/System.err(30879):    at android.view.View.performClick(View.java:2408)
12-26 23:40:00.639: W/System.err(30879):    at android.view.View$PerformClick.run(View.java:8816)
12-26 23:40:00.639: W/System.err(30879):    at android.os.Handler.handleCallback(Handler.java:587)
12-26 23:40:00.639: W/System.err(30879):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-26 23:40:00.639: W/System.err(30879):    at android.os.Looper.loop(Looper.java:123)
12-26 23:40:00.639: W/System.err(30879):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-26 23:40:00.639: W/System.err(30879):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 23:40:00.649: W/System.err(30879):    at java.lang.reflect.Method.invoke(Method.java:521)
12-26 23:40:00.649: W/System.err(30879):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-26 23:40:00.649: W/System.err(30879):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-26 23:40:00.649: W/System.err(30879):    at dalvik.system.NativeStart.main(Native Method)

With the log cat, I can do just about the beginner Newbie stuff lol .. but I have looked it over a couple times and side by side with the original and not sure what i might have messed up. Hoping someone might be able to shed little light on this for me, if any more info is needed please let me know. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:02:11+00:00Added an answer on May 27, 2026 at 9:02 pm

    Why you are referencing Spinner two times

     spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
    

    here

    fillData();
    spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
    ....
    

    and here

     private void fillData() {
            try {
                Cursor coloursCursor;
                spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
    ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.