2012-03-28 161 views
0

我需要在eclipse中爲Android創建一個下拉列表(微調),從而可以由用戶創建下拉選項。將SQL數據庫的值轉換爲字符串數組

爲此,我創建了一個SQLiteDatabase,我想將它轉換爲一個字符串數組放入一個數組適配器中,然後可以將它送入微調器。

數據庫細節爲:

DATABASE_NAME = 「carddb」 DATABASE_TABLE = 「cardtable」

然後,我想從讀出的值的列是: KEY_CARDDETAILS = 「_carddetails」;

這是到目前爲止我的代碼,改編自另一個問題,關於這個網站(鏈接: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++; 
      } 

我在「myDB」和「cardtable」下面收到一條錯誤消息,說他們無法解析成變量!

請幫助!

回答

1

使用此代碼

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

這是給你的工作。使用微調而不是listView。