Android从php/mysql查询中填充spinner

时间:2021-01-07 00:13:07

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 be done?

我正在使用CustomHttpClient来连接和查询我的android应用。

2 个解决方案

#1


4  

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.

这其实很简单。在实现细节方面,您并没有给我们太多的帮助,所以我将尽我所能,为您编写半psuedo代码,并给出一个很好的通用答案。

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

我们将从一个单独的线程执行查询,以便在等待网络响应时不会阻塞UI线程。您可以使用许多不同的方法,包括使用AsyncTask、IntentService或某种加载器。我认为在您的例子中,从AsyncTaskLoader类派生的自定义类是最好的。因此,让我们来看看会是什么样子(注意,为了使用加载程序,您需要访问一些更新的api,如果您要为任何低于api级别11的应用程序编写此应用程序,那么请检查http://developer.android.com/resources/articles/backwar-compatibility.html)。

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:

这是非常简单的东西。您应该能够通过阅读Android开发页面上的加载程序、适配器等内容来填补空白。事实上,我们正在使用一个满是“SpinnerItem”s的“列表”,这当然将取决于您的特定实现,以及您选择使用的适配器的类型。您可以随意选择您想要的任何实际数据结构。再次,请参阅Android开发页面了解详细信息。至于装入器类,你会想要做这样的事情:

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

在loadInBackground方法中,您将使用CustomHttpClient查询服务器并将响应转换为某种数据结构(与前面一样,在本例中我们使用的是“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.

就是这样。新的加载器api使事情变得非常简单。

#2


0  

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

是的它的可能。使用php/mysql查询检索项目列表,然后使用适配器向微调器提供这些项目。这个问题提供了如何编写吐丝器适配器的实例:如何正确覆盖吐丝器适配器的方法

#1


4  

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.

这其实很简单。在实现细节方面,您并没有给我们太多的帮助,所以我将尽我所能,为您编写半psuedo代码,并给出一个很好的通用答案。

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

我们将从一个单独的线程执行查询,以便在等待网络响应时不会阻塞UI线程。您可以使用许多不同的方法,包括使用AsyncTask、IntentService或某种加载器。我认为在您的例子中,从AsyncTaskLoader类派生的自定义类是最好的。因此,让我们来看看会是什么样子(注意,为了使用加载程序,您需要访问一些更新的api,如果您要为任何低于api级别11的应用程序编写此应用程序,那么请检查http://developer.android.com/resources/articles/backwar-compatibility.html)。

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:

这是非常简单的东西。您应该能够通过阅读Android开发页面上的加载程序、适配器等内容来填补空白。事实上,我们正在使用一个满是“SpinnerItem”s的“列表”,这当然将取决于您的特定实现,以及您选择使用的适配器的类型。您可以随意选择您想要的任何实际数据结构。再次,请参阅Android开发页面了解详细信息。至于装入器类,你会想要做这样的事情:

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

在loadInBackground方法中,您将使用CustomHttpClient查询服务器并将响应转换为某种数据结构(与前面一样,在本例中我们使用的是“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.

就是这样。新的加载器api使事情变得非常简单。

#2


0  

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

是的它的可能。使用php/mysql查询检索项目列表,然后使用适配器向微调器提供这些项目。这个问题提供了如何编写吐丝器适配器的实例:如何正确覆盖吐丝器适配器的方法