【问题标题】:How to get selected item position from Spinner using SimpleAdapter如何使用 SimpleAdapter 从 Spinner 获取选定项目的位置
【发布时间】:2016-06-29 23:29:04
【问题描述】:

我有一个微调器,它使用下面的代码填充,我想从微调器中获取用户选择的项目。 最好的方法是什么?

    List<Map<String, String>> tablelist  = new ArrayList<>();
    SQLiteDatabase db = openOrCreateDatabase("database", MODE_PRIVATE, null);
    String query = "select * from table";
    Cursor ps = db.rawQuery(query, null);
    while (ps.moveToNext()){
        Map<String, String> datanum = new HashMap<>();
        datanum.put("Id", ps.getString(ps.getColumnIndex("Id")));
        datanum.put("Some", ps.getString(ps.getColumnIndex("Some")));
        tablelist.add(datanum);
    }
    db.close();
    ps.close();
    SimpleAdapter spinnerAdapter = new SimpleAdapter(this, tablelist,  R.layout.row_spinner, new String[] {"Id", "Some"},  new int[] {android.R.id.text1, android.R.id.text1});
    spinnerAdapter.notifyDataSetChanged();
    spinnerAdapter.setDropDownViewResource(R.layout.row_spinner_list);
    spinner.setAdapter(spinnerAdapter);

【问题讨论】:

    标签: android spinner android-spinner simpleadapter


    【解决方案1】:

    我将回答这个问题:“获取 setSelection() 方法所需的项目位置的最佳方法”我希望这是您所要求的。

    我将 SimpleAdapter 更改为 ArrayAdapter。

    我更喜欢为您的 DTB 请求为每个结果创建一个对象,并将该对象映射到微调器中。因此,您可以轻松访问每个结果。

    像这样:

    public class test {
        private String TAG = "test";
    
        private Activity curAct;
        private Spinner spinner;
        private SpnObj[] spinnerOptions;
    
        public test(Activity curAct) {
            this.curAct = curAct;
        }
    
        public void test() {
            this.spinner = new Spinner(this.curAct);
            this.spinnerOptions = this.getSpnContent();
    
            final ArrayAdapter spinnerAdapter = new ArrayAdapter<>(this.curAct, android.R.layout.simple_spinner_item, spinnerOptions);
            spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            this.spinner.setAdapter(spinnerAdapter);
    
            // setSelection with item id = 1
            this.preSelectItem(1);
    
            // setSelection with item some = 'some'
            this.preSelectItem("some");
    
            // get selectedItem
            SpnObj selectedItem = this.getSelectedItem();
    
            Log.d(TAG, "the current selected item id is : " + selectedItem.id);
            Log.d(TAG, "the current selected item some is : " + selectedItem.some);
        }
    
        // this method should be used when you need to access on you're selectedItem
        public SpnObj getSelectedItem() {
            return (SpnObj) this.spinner.getSelectedItem();
        }
    
        // this method should be used for set the selection on a item with the id => maybe that was what you're asking for
        public void preSelectItem(int id) {
            boolean doWeStopTheLoop = false;
    
            for (int i = 0; i < this.spinnerOptions.length && !doWeStopTheLoop; i++) {
                if (((SpnObj) this.spinner.getItemAtPosition(i)).id == id) {
                    this.spinner.setSelection(i);
                    doWeStopTheLoop = true; //you can use break; too
                }
            }
        }
    
        // this method should be used for set the selection on a item with the id => maybe that was what you're asking for
        // surcharge for 'some' column
        public void preSelectItem(String some) {
            boolean doWeStopTheLoop = false;
    
            for (int i = 0; i < this.spinnerOptions.length && !doWeStopTheLoop; i++) {
                if (((SpnObj) this.spinner.getItemAtPosition(i)).some.equals(some)) {
                    this.spinner.setSelection(i);
                    doWeStopTheLoop = true; //you can use break; too
                }
            }
        }
    
        private SpnObj[] getSpnContent() {      // this method must be call in a thread
            SQLiteDatabase db = this.curAct.openOrCreateDatabase("database", 0, null);
            String query = "select * from table";
            Cursor ps = db.rawQuery(query, null);
    
            SpnObj[] spnContent = new SpnObj[ps.getColumnCount()];
    
            if (ps.getColumnCount() > 0) {
                int iterator = 0;
                while (ps.moveToNext()) {
                    spnContent[iterator] = new SpnObj(
                            ps.getInt(ps.getColumnIndex("Id")),
                            ps.getString(ps.getColumnIndex("Some"))
                    );
    
                    iterator++;
                }
            } else {
                // no rows : insert code here if you want manage this
            }
    
            db.close();
            ps.close();
    
            return spnContent;
        }
    }
    
    // this object is only encapsulate the DTB result in a java class, so we can work easily with
    class SpnObj {
        public int id;
        public String some;
    
        public SpnObj(int id, String some) {
            this.id = id;
            this.some = some;
        }
    
        @Override
        public String toString() {          // important ! this method will be used for display the value in the dropdown list from the spinner
            return this.some;
        }
    }
    

    【讨论】:

      【解决方案2】:

      检查下面的代码以获得答案,您可以通过下面的代码获得位置以及选定的项目。

         SimpleAdapter spinnerAdapter = new SimpleAdapter(this, tablelist,  R.layout.row_spinner, new String[] {"Id", "Some"},  new int[] {android.R.id.text1, android.R.id.text1});
         spinnerAdapter.notifyDataSetChanged();
         spinnerAdapter.setDropDownViewResource(R.layout.row_spinner_list);
         spinner.setAdapter(spinnerAdapter);
      
         spinner.setOnItemSelectedListener(new OnItemSelectedListener()
          {
              @Override
              public void onItemSelected(AdapterView<?> parent, View v,int position, long id) 
              {
                  //Here position is selected item position
                   Map<String, String> datanum = tablelist.get(position);
                  // now you can use this datanum for further use
              }
      
              @Override
              public void onNothingSelected(AdapterView<?> parent) {
                  // TODO Auto-generated method stub
      
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多