在微调器上逐行显示数组

时间:2015-04-04 15:16:58

标签: android arrays spinner

我已将一个列中的内容放在微调器中。这是一个列,我使用一个复选框(Sparseboolean数组)数组保存内容,这意味着它有许多单词用",#34;分隔。我设法在微调器中显示内容,但它们都在一行中,而不是每个单词都在它自己的行和逗号上。这是我用来显示光标微调器的代码



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) {

            }
        });
&#13;
&#13;
&#13;


维克托。这是getAllRows()

的代码

&#13;
&#13;
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;
    }
&#13;
&#13;
&#13;


Kyus Addiction。这就是我尝试过的。但该应用程序正在崩溃

&#13;
&#13;
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) {

                }
            });
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

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

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


并且您的微调器显示文本为&#34; A1,A2,A3和#34;

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

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&#s; s Splitter:

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

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

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