【问题标题】:Display array line by line on spinner在微调器上逐行显示数组
【发布时间】:2015-04-04 15:16:57
【问题描述】:

我已将列中的内容放入微调器中。这是我使用复选框数组(稀疏布尔数组)保存内容的列,因此这意味着它有许多用“,”分隔的单词。我设法在微调器中显示内容,但所有内容都在一行中,而不是每个单词都在自己的行中并带有逗号。这是我用来显示光标微调器的代码

Cursor cursor = dbHelper.getAllRows();

        if(cursor.getCount()>0){
            String[] from = new String[]{"Facilities"};
            // create an array of the display item we want to bind our data to
            int[] to = new int[]{android.R.id.text1};
            SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,
                    cursor, from, to);
            mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spFacilityType.setAdapter(mAdapter);
        }


        spFacilityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view,
                                       int pos, long log) {


                Cursor c = (Cursor)parent.getItemAtPosition(position);
                int id = c.getInt(c.getColumnIndexOrThrow("Facilities"));

            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

维克托。这是 getAllRows() 的代码

public Cursor getAllRows() {
        String where = null;
        Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
                where, null, null, null, KEY_STATION_NAME, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

Kyus 上瘾。这是我尝试过的。但是应用程序崩溃了

Cursor cursor = dbHelper.getAllRows();

        TextUtils.SimpleStringSplitter simpleStringSplitter=new TextUtils.SimpleStringSplitter(',');

        List<String> lables = (List<String>) dbHelper.getAllRows();

        if(cursor.getCount()>0) {
            String[] from = new String[]{"Facilities"};
            // create an array of the display item we want to bind our data to
            int[] to = new int[]{android.R.id.text1};
            ArrayAdapter<String> adapter =
                    new ArrayAdapter<>(AddReview.this, // This is your context
                            android.R.layout.simple_dropdown_item_1line, // This is your layout
                            lables); // This is your data

            spFacilityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    int index = arg0.getSelectedItemPosition();
                    position = index;
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

【问题讨论】:

  • 添加dbHelper.getAllRows()的代码;

标签: android arrays spinner


【解决方案1】:

如果我理解正确,您存储的数据如下:

id  spinner_data    comment
--  ------------    -------
0   A1,A2,A3        my 'A' data
1   B2,B4,B6        my 'B' data
2   C3,C6,C9        my 'C' data


所以当你加载和迭代光标时:

Cursor cursor = dbHelper.getAllRows();

它给你的正是你所要求的......单行:

0   A1,A2,A3        my 'A' data


您的微调器将文本显示为“A1,A2,A3”

如果您希望现有代码正常工作,则必须将每个字符串存储为单独的记录:

id  spinner_data    comment
--  ------------    -------
0   A1              my 'A1' data
1   A2              my 'A2' data
2   A3              my 'A3' data


或者,一个(相对)简单的解决方法是切换到 ArrayAdapter。 您可以做的是将列数据作为字符串获取

String data = cursor.getString(COLUMN_SPINNER_DATA); // A1,A2,A3

使用一些字符串标记器将该字符串(数据)解析为字符串列表,我更喜欢 guava 的 Splitter:

// Split using the comma (,) as a separator
Splitter spl = Splitter.onPattern(",");     
List<String> lstData = spl.split(data);

现在,实例化一个 ArrayAdapter,而不是 SimpleCursorAdapter:

ArrayAdapter<String> adapter =
            new ArrayAdapter<>(SomeActivity.this, // This is your context
                    android.R.layout.simple_dropdown_item_1line, // This is your layout
                    lstData); // This is your data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多