开发者

Storing selected spinner item in database table - Android

So my situation is this. I have two tables. A main table where data is added using a form, this form has a spinner which is powered from the second table (this part is working correctly). I then want the selected item in the spinner to be stored in a 开发者_开发问答foreignkey field in the main table.

I have been using the following code:

private Spinner mSpinner;

mSpinner = (Spinner) findViewById(R.id.spinCat);

and in my populate fields method I have the following:

  mSpinner.getSelectedItem();

but this stores something like "android.database.sqlite.SQLiteCursor@43e5be60" in to the database rather than the name.

If anyone can offer some help it would be great, this has been bothering me for some days now and has halted development somewhat. If you need to see more code, then please don't hesitate to ask.

Any help is much appreciated.

EDIT: new code:

            mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View vw,
                    int pos, long id) {
                LinearLayout rowll = (LinearLayout) vw;
                TextView test = (TextView) rowll.getChildAt(0);
                Toast t = Toast.makeText(parent.getContext(), test
                        .getText().toString(), Toast.LENGTH_SHORT);
                t.show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // Do nothing..
            }
        });


getSelectedItem returns an Object (which is what your output looks a lot like).

Did you try simply calling toString like this?

mSpinner.getSelectedItem().toString();

I personally just set a variable in the onItemSelected listener, but this should work too. What I use is:

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            myIP = parent.getItemAtPosition(position).toString();
        }

Variable is then continuously updated and I just add it to the DB when I connect to the IP in question.

Cursor data extraction:

    while (cursor.moveToNext()) {
        ipList.add(cursor.getString(1));
    }

getString(int columnIndex) does: Returns the value of the requested column as a String.

http://developer.android.com/reference/android/database/Cursor.html


Don't use .getChildAt(), use .getItemAtPositon(). See how the code does it at http://developer.android.com/resources/tutorials/views/hello-spinner.html.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜