Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Display array column using spinner

android sqlite arrays spinner

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 04 April 2015 - 07:30 AM

I have tried to display content from a specific column of a specific row, in a spinner. It is a column from which i saved content using an array of checkbox's (Sparseboolean array) so meaning it has many words seperated with a ",". I managed to display the content in the spinner but it all coming in one line instead of each word coming on its own line and with the comma. here is the code i used to display the cursor spinner.

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

            }
        });

Result = Wifi,Toilets,Ramps

Wanted result =  Wifi
                           Toilets
                           Ramps 
 



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 04 April 2015 - 01:58 PM

You've a lot of misses in there.

 

First you are making an int array out of a text.

 

You need to:

 

-Create string array out of the text.Split( "," )  ( you check if it's string.Split, or whatever, I don't have it in mind right now, but Split(",") will sure give you what you need.

 

-  Use an array adapter instead of a cursor adapter.

 

- and the datatype for parent.getItem... is String (not cursor)

 

Fixing those will sure make it work :)



#3 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 05 April 2015 - 04:39 AM

You've a lot of misses in there.

 

First you are making an int array out of a text.

 

You need to:

 

-Create string array out of the text.Split( "," )  ( you check if it's string.Split, or whatever, I don't have it in mind right now, but Split(",") will sure give you what you need.

 

-  Use an array adapter instead of a cursor adapter.

 

- and the datatype for parent.getItem... is String (not cursor)

 

Fixing those will sure make it work :)

This is what i have tried, but the app is crashing when i open the activity

 

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

                }
            });





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download