最好的方法是什么?
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);解决方法 我将回答这个问题:“获取setSelection()方法所需项目位置的最佳方法”我希望这是你所要求的.
我用ArrayAdapter更改了SimpleAdapter.
我首选为每个结果创建一个对象,因为你是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",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 withclass 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; }}总结
以上是内存溢出为你收集整理的android – 如何使用SimpleAdapter从Spinner获取选定的项目位置全部内容,希望文章能够帮你解决android – 如何使用SimpleAdapter从Spinner获取选定的项目位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)