Android Spinners,我说我从SQL中提取微调器选项,我还可以将它们的SQL _id添加到微调器值吗?

时间:2011-08-08 14:18:48

标签: android spinner

我查询我的SQL数据库以填充我的微调器,我没有问题使用生成的书籍标题的字符串数组填充微调器(这是一个库样式的应用程序)。虽然将书籍标题纳入微调器以供选择是没有问题的,将这些标题与SQL _id相关联的最佳方法是什么?我一直在寻找一种让旋转器“多维”的方法,但到目前为止我还没有看到。

非常感谢任何正确方向的帮助,谢谢!

3 个答案:

答案 0 :(得分:1)

我所做的是为我的微调器使用了一个多维数组。它从string[i][0]抓取内容,而ids在string[i][1]中。让我抓住我用过的代码。

public class BNYDirectory extends Activity {

    public static String[] Fields = new String[6];

    public static String[][] BookResults;
    public ArrayAdapter<CharSequence> booksAdapter;

    public ProgressDialog dialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchscreen);

        new getAllData().execute(this);

        final EditText fname = (EditText) findViewById(R.id.fname);
        final EditText lname = (EditText) findViewById(R.id.lname);
        final EditText aim = (EditText) findViewById(R.id.aim);
        final EditText phone = (EditText) findViewById(R.id.phone);
        final Spinner books = (Spinner) findViewById(R.id.book);

       //Sets up and fills the department spinner
        booksAdapter = new ArrayAdapter<CharSequence>(BNYDirectory.this, android.R.layout.simple_spinner_item);
        booksAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        books.setAdapter(booksAdapter);

      //Search button
        findViewById(R.id.search).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

              //This grabs the ID from the String[][] for the selected spinner item
                String bookName = BookResults[(int)books.getSelectedItemId()][0];
                String bookId = BookResults[(int)books.getSelectedItemId()][1];

            }
        });
    }


    private class getAllData extends AsyncTask<Context, Void, Cursor> {
        protected void onPreExecute () {
            dialog = ProgressDialog.show(BNYDirectory.this, "", 
                    "Loading. Please wait...", true);
        }

        @Override
        protected Cursor doInBackground(Context... params) {
            setBooks();
            return null;
        }

        protected void onPostExecute(Cursor c) {
            for(int i = 0 ; i < BookResults.length ; i++){
                booksAdapter.add(BookResults[i][0]);            
            }
            dialog.dismiss();
        }
    }

    public static void setBooks(){

        //Basically this sets the String[][] DeptResults so that DeptResults[n][0] is the names, and DeptResults[n][1] is the ID that matches for n.
        //books[n][0] = book name
        //books[n][1] = book ID

        BookResults = books;

    }
}

好的,所以这样做是使用类变量String[][] BookResults来保存两个值,名称(在[] [0]中)和id(在[] [1]中)。 BookResults[10][0]将成为BookResults[10][1] ID的书名。此示例中的“搜索按钮”显示了如何获得这两个值。

答案 1 :(得分:1)

你肯定想要SimpleCursorAdapter。您必须在选择查询中包含_id。这是一个例子......

Spinner spinner = (Spinner) dialog.findViewById(R.id.mm_spinner);

// DatabaseInterface would be your data access class...
DatabaseInterface db = new DatabaseInterface(this);
db.open();

Cursor c = db.getNames(); // This would contain _id, name from a database for example.

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_spinner_item,
      c,
      new String[] {DatabaseInterface.KEY_ID, DatabaseInterface.KEY_NAME}, 
      new int[] {android.R.id.text1});

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

...这会将_id绑定到微调器id。因此,当您使用onitemclicklistener选择列表中的项目时,就像我在下面发布的那样。您将拥有与列表中的每个名称相关联的正确_id

spinner.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView parent, View view, int position, long id){
           // id = the _id from the cursor
     }
}); 

答案 2 :(得分:0)

使用SimpleCursorAdapter和Spinner以及子布局的自定义视图。 :)

Here is a tutorial.