0

I need to create a drop down list (spinner) in eclipse for Android whereby the drop down options can be created by the user.

To do this I've created a SQLiteDatabase, which I want to convert into a string array to be put into an array adapter, which can then be fed into the spinner.

Database details are:

DATABASE_NAME = "carddb" DATABASE_TABLE = "cardtable"

Then the column I wish to read the values from is: KEY_CARDDETAILS = "_carddetails";

Here is my code so far, adapted from another question on this site (Link: how to get the database value to a String array in android(sqlite Database))

myDB = cardtable.this.openOrCreateDatabase("carddb", MODE_PRIVATE, null); Cursor cardcursor = cardDB.rawQuery("SELECT * FROM cardtable");

        String[] cardstringarray = new String[cardcursor.getCount()];
           cardcursor.moveToFirst();   

           int counter = 0;
           while(cardcursor.moveToNext()){
               String carddetailss = cardcursor.getString(cardcursor.getColumnIndex("NAME"));
               cardstringarray[counter] = carddetailss;
               counter++;
            }

I'm getting an error under "myDB" and "cardtable", both errors saying they cannot be resolved into a variable!

PLEASE HELP!

1 Answer 1

1

Use this code

String[] displayName={};
ArrayList<String> List=new ArrayList<String>();
myDB = cardtable.this.openOrCreateDatabase("carddb", MODE_PRIVATE, null); Cursor cardcursor = cardDB.rawQuery("SELECT * FROM cardtable");

    String[] cardstringarray = new String[cardcursor.getCount()];
       cardcursor.moveToFirst();   

       int counter = 0;
       while(cardcursor.moveToNext()){
           String carddetailss = cardcursor.getString(cardcursor.getColumnIndex("NAME"));
           List.add(carddetailss);
           counter++;
        }
    if(List != null){
    listEmail=(ListView)findViewById(R.id.listEmail);
    listEmail.setVisibility(view.VISIBLE);
    displayName=(String[])List.toArray(new String[0]);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, displayName);
    // Assign adapter to ListView
    listEmail.setAdapter(adapter);
    }

This is work for you. Use spinner instead of listView.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.