0

I am new to LoopJ Android Asynchronous-Http-Client.I figured out how to GET the data from the database with my PHP web service.

My ServiceHandler :

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

My AddBookActivity: In this class i use getAllCat to get categories from database but now how can i populate my spinner ?

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();
            }
        });
    }
}
Amal
  • 279
  • 3
  • 5
  • 21

2 Answers2

0

If you only wanted to populate the list of category names - instead of JSONArray - then you can change your getAllCat() to something like this:

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

However, if you want to use the JSONArray - then you will need a "special" adapter. I have recently answered a similar question. Please check here for possible solution. I hope this helps.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • thank you, its work by using youre solution and changing the public void onSuccess(int statusCode,cz.msebera.android.httpclient.Header[] headers, JSONArray response) to public void onSuccess(int statusCode, Header[] headers, JSONObject response) because i get i jsonObject and not a JsonArray – Amal May 06 '16 at 18:01
0

Write this code into your onSuccess method

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();
}
Masum
  • 4,879
  • 2
  • 23
  • 28