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!

Community
  • 1
  • 1

1 Answers1

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.

Bhargav Panchal
  • 1,159
  • 1
  • 12
  • 27