填充微调器 android 和 Asynchronous-Http-Client

Populate spinner android and Asynchronous-Http-Client

我是 LoopJ 的新手 Android Asynchronous-Http-Client.I 弄清楚了如何使用我的 PHP Web 服务从数据库中获取数据。

我的服务处理器:

public class ServiceHandler {
    private static String BASE_URL="http:/my/url/";
    private static String GET_CAT= BASE_URL+"get_cat.php";
    private  static AsyncHttpClient client= new AsyncHttpClient();
    public  static void getAllCat(RequestParams params,AsyncHttpResponseHandler asyncHttpResponseHandler
    ){
        client.get(GET_CAT,params,asyncHttpResponseHandler);};}

我的 AddBookActivity:在这个 class 中,我使用 getAllCat 从数据库中获取类别,但现在如何填充我的微调器?

public class AddBookActivity extends AppCompatActivity {
ServiceHandler client = new ServiceHandler();
    private Spinner spCat;
    @Override
    protected void onResume() {
        super.onResume();
        getAllCat();
    }
    private void getAllCat(){

        client.getAllCat(null, new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode,cz.msebera.android.httpclient.Header[] headers, JSONArray response) {

                        try {

                            for (int i=0; i<response.length();i++){
                                Categories cat = new Categories();

                                JSONObject obj =response.getJSONObject(i);
                                cat.setName(obj.getString("name"));
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }}} ); }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        spCat=(Spinner)findViewById(R.id.spinnercatname);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

如果您只想填充类别名称列表 - 而不是 JSONArray - 那么您可以将 getAllCat() 更改为如下内容:

private void getAllCat(){

    client.getAllCat(null, new JsonHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode,cz.msebera.android.httpclient.Header[] headers, JSONArray response) {
            try {
                List<String> categoryNames =  new ArrayList<String>();
                for (int i=0; i<response.length();i++){
                    JSONObject obj =response.getJSONObject(i);
                    String categoryName = obj.getString("name");
                    categoryNames.add(categoryName);
                }
                //now that you have populated the array of category names - you can create the adapter for the spinner
                ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoryNames);
                spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spCat.setAdapter(spinnerAdapter);               
                spinnerAdapter.notifyDataSetChanged();                

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }); 
}

但是,如果您想使用 JSONArray - 那么您将需要一个 "special" 适配器。我最近回答了一个类似的问题。请检查 。 希望对您有所帮助。

将此代码写入您的 onSuccess 方法

try {
    ArrayList<String> category = new ArrayList<String>();
    for (int i=0; i<response.length();i++){
       JSONObject obj = response.getJSONObject(i);
       category.add(obj.getString("name"));
    }

   ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(AddBookActivity.this, android.R.layout.simple_list_item_1, category);
   spCat.setAdapter.setAdapter(dataAdapter);              

}catch (Exception e){
   e.printStackTrace();
}