开发者

Android Spinners, say I pull spinner selections from SQL, can I also add their SQL _id's to the spinner values?

开发者 https://www.devze.com 2023-03-26 11:30 出处:网络
I query my SQL dat开发者_运维知识库abase 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

I query my SQL dat开发者_运维知识库abase 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!


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


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.


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

Here is a tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消