Android Spinners,假设我从 SQL 中提取微调器选择,我是否也可以将它们的 SQL _id 添加到微调器值中?

发布于 2024-11-28 21:02:54 字数 175 浏览 1 评论 0原文

我查询 SQL 数据库来填充我的微调器,用生成的书名的字符串数组填充微调器没有问题(这是一个库样式应用程序)。虽然将书名放入旋转器中进行选择没有问题,但将这些书名与其 SQL _id 联系起来的最佳方法是什么?我一直在寻找一种使旋转器“多维”的方法,但到目前为止我还不知道如何实现。

任何正确方向的帮助将不胜感激,谢谢!

I query my SQL database to populate my spinners, I have no problem populating the spinner with a string array of the resulting Book Titles (this is a library style app). While getting the Book Titles into the spinner for selection is no problem, what is the best way to tie these titles back to their SQL _id's? I've been looking for a way to make spinners "multi-dimensional" but so far I don't see how.

Any help in the right direction would be much appreciated, thank you!

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

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

发布评论

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

评论(3

静水深流 2024-12-05 21:02:54

您肯定需要 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
     }
}); 

You definitely want the SimpleCursorAdapter. You must include the _id in the select query. Here is an example...

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);

... This will bind the _id to the spinner id. So when you select an item in the list using the onitemclicklistener like what I posted below. You will have the correct _id's associated with each of the names in the list...

spinner.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView parent, View view, int position, long id){
           // id = the _id from the cursor
     }
}); 
霓裳挽歌倾城醉 2024-12-05 21:02:54

我所做的是为我的微调器使用多维数组。它从 string[i][0] 中获取内容,并且 id 位于 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 的图书名称。本示例中的“搜索按钮”显示了如何获取这两个值。

What I did is used a multidimensional array for my spinner. It grabs things from string[i][0] and the ids are in string[i][1]. Let me grab the code I used for it.

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;

    }
}

Okay, so what this does is uses a class variable String[][] BookResults to hold both values, the name (in [][0]) and the id (in [][1]). BookResults[10][0] would be the book name for BookResults[10][1]'s ID. The "search button" in this example shows how you'd get both values.

烟雨凡馨 2024-12-05 21:02:54

考虑将 SimpleCursorAdapter 与 Spinner 和子布局的自定义视图一起使用。 :)

这是一个教程。

Look into using a SimpleCursorAdapter with your Spinner and a custom view for the child layouts. :)

Here is a tutorial.

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