Android populate spinner from php/mysql query
I am using the CustomHttpClient to connect and do queries from my android app. I was wondering if it was possible to populate a spinner from a php/mysql query and if so how it would b开发者_如何学Goe done?
So this should actually be fairly easy. You haven't really given us much to work on in terms of implementations details so I'll do my best to semi-psuedo code things out for you and give you a good general answer.
We're going to want to do the query from a separate thread so as to not block the UI thread while we're waiting for a network response. You can do this many different ways including using an AsyncTask, IntentService, or some sort of Loader. I'm thinking in your case a custom class derived form the AsyncTaskLoader class will be the best. So lets see how this would look (note you'll need access to some of the newer apis in order to use the loader, checkout the http://developer.android.com/resources/articles/backward-compatibility.html if you're writing this application for anything less than api level 11).
MyActivity extends Activity implements LoaderManager.LoaderCallbacks<List<SpinnerItem>>{
private Adapter<SpinnerItem> spinnerAdapter;
private static final int SPINNER_LOADER_ID = 0;
public void onCreate(Bundle icicle){
Spinner theSpinner = findViewById(R.id.the_spinner);
spinnerAdapater = new Adapter<SpinnerItem>(...); //create empty adapter
spinnerAdapater.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
theSpinner.setAdapter(spinnerAdapater);
getLoaderManager().initLoader(SPINNER_LOADER_ID, null, this);
}
public Loader<List<SpinnerItems>> onCreateLoader(int id, Bundle arg){
//Create and return your loader
}
public void onLoadFinish(Loader<List<SpinnerItem>> loader, List<SpinnerItem> data){
//update your adapater with the new data
}
public void onLoaderReset(Loader<List<SpinnerItem>> loader){
//clear out all the data in the adapter
}
}
This is all pretty straight-forward stuff here. You should be able to fill in the gaps by reading about loaders, adapters, etc. on the Android dev pages. The fact that we're using a "List" full of "SpinnerItem"s is of course going to be dependent on your particular implementation, as is the type of adapter you choose to use. Feel free to choose whatever actual data structures you want. Once again, consult the Android dev pages for details. As far as the loader class goes, you'll wanna do something like this:
MyNetworkLoader extends AsyncTaskLoader<List<SpinnerItem>>
public MyNetworkLoader(Context context){
super(context);
}
List<SpinnerItem> loadInBackground(){
//Get data from server
}
}
In the loadInBackground method you'll use your CustomHttpClient to query your server and convert the response into some sort of data structure (just as before, in this example we're using a "List" of "SpinnerItem"s).
As a final note, your spinner is going to be empty until your network request completes. You may want to include some sort of logic informing the user that the contents of the spinner are loading.
That's it. The new loader api's make things really simple.
Yes its possible. Retrieve the a list of items using the php/mysql query and then provide those items to the spinner using an Adapter. This question provides an exmample of how to write an SpinnerAdapter: How to correctly overwrite methods of SpinnerAdapter
精彩评论