Android Spinner 所选项目

发布于 2024-11-04 01:18:42 字数 1039 浏览 4 评论 0原文

我正在从数据库中填充这样的微调器

    // Populating the City Spinner
    Cursor cities = db.cityList();
    startManagingCursor(cities);

    // create an array to specify which fields we want to display
    String[] from = new String[] { DBAdapter.KEY_NAME };
    // create an array of the display item we want to bind our data to
    int[] to = new int[] { android.R.id.text1 };

    Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cities, from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    cityList.setAdapter(adapter);

当我尝试从这样的微调器的选定项目中获取内容时,

// Get the City
                Spinner getCity = (Spinner) findViewById(R.id.citySpiner);
                String cityName = getCity.getSelectedItem().toString();

我得到以下内容。 有没有办法可以从数据库中获取城市名称或城市 ID。

在此处输入图像描述

I am populating a spinner from the database like this

    // Populating the City Spinner
    Cursor cities = db.cityList();
    startManagingCursor(cities);

    // create an array to specify which fields we want to display
    String[] from = new String[] { DBAdapter.KEY_NAME };
    // create an array of the display item we want to bind our data to
    int[] to = new int[] { android.R.id.text1 };

    Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cities, from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    cityList.setAdapter(adapter);

When i try to get the content from the selected item of the spinner like this

// Get the City
                Spinner getCity = (Spinner) findViewById(R.id.citySpiner);
                String cityName = getCity.getSelectedItem().toString();

i get the following.
Is there a way i can get the city name or the city id from the database.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我喜欢麦丽素 2024-11-11 01:18:42

我认为,当您使用自定义适配器并在适配器中给出三个列表时...

您无法仅通过调用 getSelectedItem() 来获取所选文本...

使用这个:

Spinner mySpinner = (Spinner)findViewbyId(R.id.spinner);
int position = mySpinner.getSelectedItemPosition();
String Text = yourCityList[position].toString(); // dont know which list is holding the city list... 
// i didn't use any db in any of my app so cant tell you how can you get list... 
// leaving it to you... :)

希望它有帮助...

I think, as you are using a customadapter and giving three lists in adapter...

you can't get the selected text simply by calling the getSelectedItem()..

Use this:

Spinner mySpinner = (Spinner)findViewbyId(R.id.spinner);
int position = mySpinner.getSelectedItemPosition();
String Text = yourCityList[position].toString(); // dont know which list is holding the city list... 
// i didn't use any db in any of my app so cant tell you how can you get list... 
// leaving it to you... :)

Hope it helps....

羞稚 2024-11-11 01:18:42

只需从微调器中获取适配器并从光标中获取字符串

Cursor cursor = (Cursor) myAdapter.getItem(position);
String cityName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_NAME));

Just get the adapter from your spinner and get the string from the cursor

Cursor cursor = (Cursor) myAdapter.getItem(position);
String cityName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_NAME));
轮廓§ 2024-11-11 01:18:42
    List<String> list = new ArrayList<String>();
    s = (Spinner)findViewById(R.id.spinner1);
    SQLiteDatabase sqldb = db.openDataBase();
    Cursor cr = sqldb.rawQuery("select Name from employee",null);
    if (cr.moveToFirst()) {
        do {
        list.add(cr.getString(0).toString());
        Toast.makeText(this,cr.getString(0).toString(),Toast.LENGTH_LONG).show();
        ArrayAdapter <String> a= new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);
         a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          s.setAdapter(a);
        } while (cr.moveToNext());

      }//urvesh patel
    List<String> list = new ArrayList<String>();
    s = (Spinner)findViewById(R.id.spinner1);
    SQLiteDatabase sqldb = db.openDataBase();
    Cursor cr = sqldb.rawQuery("select Name from employee",null);
    if (cr.moveToFirst()) {
        do {
        list.add(cr.getString(0).toString());
        Toast.makeText(this,cr.getString(0).toString(),Toast.LENGTH_LONG).show();
        ArrayAdapter <String> a= new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);
         a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          s.setAdapter(a);
        } while (cr.moveToNext());

      }//urvesh patel
溺渁∝ 2024-11-11 01:18:42

我实现它的方式是这样的:

您必须将 getItem() 方法转换为游标,因为它本质上只是返回游标而不是游标中的单个行,这很烦人。

final Cursor cursor = (Cursor) yourSpinner.getAdapter().getItem( position );

将光标移动到第一行

cursor.moveToFirst();

现在,您可以使用与设置旋转器适配器时最初查询的表相匹配的列名从光标获取字符串,该适配器是使用 getAdapter() 在上面抓取的,

final String selectedPartUUID = cursor.getString( cursor.getColumnIndex( TABLE_COLUMN_NAME ) );

希望这有帮助,并感谢任何反馈:)

The way I achieved it was like this:

You will have to cast the getItem() method to a cursor as it is essentially just returning a cursor rather than an individual row in the cursor which is annoying.

final Cursor cursor = (Cursor) yourSpinner.getAdapter().getItem( position );

Move the cursor to the first row

cursor.moveToFirst();

Now you can get the string from the cursor using the column name that matches table originally queried when setting your spinners adapter which was grabbed above using getAdapter()

final String selectedPartUUID = cursor.getString( cursor.getColumnIndex( TABLE_COLUMN_NAME ) );

Hope this helps and would appreciate any feedback :)

七颜 2024-11-11 01:18:42

希望这有帮助!

Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show();

Hope this helps!!

Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show();
殤城〤 2024-11-11 01:18:42
 public List<String> GetTexts() {
            ArrayList<String> texts = new ArrayList<String>();
            for (int i = 0; i < mSpinnerList.size(); i ++) {
                int position = mSpinnerList.get(i).getSelectedItemPosition();
                Log.d("DATA_TO_BE_SEND:", tennisModelArrayList.get(position).getStatus());
                texts.add(mSpinnerList.get(i).getItemAtPosition(i).toString());
            }

            return texts;
        }
 public List<String> GetTexts() {
            ArrayList<String> texts = new ArrayList<String>();
            for (int i = 0; i < mSpinnerList.size(); i ++) {
                int position = mSpinnerList.get(i).getSelectedItemPosition();
                Log.d("DATA_TO_BE_SEND:", tennisModelArrayList.get(position).getStatus());
                texts.add(mSpinnerList.get(i).getItemAtPosition(i).toString());
            }

            return texts;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文