开发者

Android spinner shows EMPTY values

开发者 https://www.devze.com 2023-04-11 04:28 出处:网络
I am new in android. I need to fill the spinner from SQLite database. my layout >> main.xml contains <Spinner

I am new in android. I need to fill the spinner from SQLite database. my layout >> main.xml contains

<Spinner 
            android:id="@+id/cmbLocations"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:prompt="@string/location_prompt"
        />

<EditText android:id="@+id/text1" android:layout_width="300dp" android:layout_height="wrap_content"/>

I have a public function which returns the locations:

public static final String KEY_ROWID = "_id";
public static final String KEY_LOCATION = "location";

public Cursor GetLocations()
    {
        return db.query(DATABASE_TABLE, 
                new String[] 
                { 
                    KEY_ROWID, // "_id"
                    KEY_LOCATION // "location"
                }, null, null, null, null, null);
    }

The code which is populating the spinner is :

    Spinner cmbLocations = (Spinner) findViewById(R.id.cmbLocations);
    DBAdapter db = new DBAdapter(this);
    db.open();

    Cursor cur = db.GetLocations();
    String[] from = new String[]{"_id","location"};
    int[] to = new int[]{R.id.text1};// I don't know that why text1 is required here

    startManagingCursor(cur);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, cur, from, to); 
                    adapter.setDropDownViewResource(android.R.layout.simple_spin开发者_如何学Cner_dropdown_item);
cmbLocations.setAdapter(adapter);
db.close();

The issue i am facing is that the spinner controls shows EMPTY locations. For example if i have two locations, Location1 & Location2. The spinner will show two EMPTY locations. If i have 3 locations the spinner will show three EMPTY locations.

Thanks in advance for your help.


For R.id.text1 to work (instead of android.R.id.text1), there has to be a layout in your resources with TextView with this id: R.id.text1. For example:

spin_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" >
<TextView 
    android:id="@+id/field1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp" />
<TextView 
    android:id="@+id/field2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="20sp" />
</LinearLayout>

Now you can use in the adapter:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
    R.layout.spin_layout, 
    ur_cursor, 
    new String[] {"Field1_in_ur_cursor", "Field2_in_ur_cursor"}, 
    new int[] {R.id.field1, R.id.field2}, 0);

adapter.setDropDownViewResource(R.layout.spin_layout);

It binds "Field1_in_ur_cursor" to R.id.field1, and "Field2_in_ur_cursor" to R.id.field2 for the spinner with layout spin_layout.xml.


Try using:

String[] from = new String[]{"_id","location"};
    int[] to = new int[]{R.id.text1,R.id.text2};// I don't know that why text1 is required here

int[] to is for placing the values from the cursor,you defined in from.. For example,you are fetching _id and location then you would have to specify its destination,here it might be textview.


Should your from array just be {"location"}

Have a look here:

Simple Spinner Item Source

This shows what the spinner 'simple_spinner_item' is actually doing and it explains what your r.id.text1 is.

Your passing two columns into the Spinner but only wanting to display one.

Print some log's around your Cursor to see if it actually contains data.


I have two columns to show on spinner and when : tried to use code like;

String[] from = new String[]{"_id","location"}; 
int[] to = new int[]{R.id.text1,R.id.text2};

It did not work. when I change it to:

String[] from = new String[]{"_id","location"}; 
int[] to = new int[]{android.R.id.text1,android.R.id.text1};

It worked for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号