【问题标题】:Converting values of SQL Database into a String Array将 SQL 数据库的值转换为字符串数组
【发布时间】:2012-03-28 16:51:33
【问题描述】:

我需要在 Eclipse for Android 中创建一个下拉列表(微调器),用户可以创建下拉选项。

为此,我创建了一个 SQLiteDatabase,我想将其转换为字符串数组以放入数组适配器中,然后可以将其输入到微调器中。

数据库详情如下:

DATABASE_NAME = "carddb" DATABASE_TABLE = "卡片表"

然后我希望从中读取值的列是: 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); 光标 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”下遇到错误,这两个错误都表示它们无法解析为变量!

请帮忙!

【问题讨论】:

    标签: android sql arrays string


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 2021-11-18
      • 1970-01-01
      • 2021-12-14
      • 2013-08-04
      • 2017-11-27
      相关资源
      最近更新 更多