0

ChooseCategory.java

        public class ChooseCategory extends ListActivity {
            private ListView lv;
            //ArrayAdapter<FoodStores> arrayAdapter;
            private ArrayList<FoodStores> storefoodList;
        private String URL_STORES = "http://10.0.2.2/get_stores.php";
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                 lv = (ListView)findViewById(R.id.list);
                 storefoodList = new ArrayList<FoodStores>();
                 new GetFoodStores().execute();

        }

    private class GetFoodStores extends AsyncTask<Void,Void,Void> {
            @Override
            protected void onPreExecute(){
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                ServiceHandlerFood jsonParserFood = new ServiceHandlerFood();
                String json = jsonParserFood.makeServiceCall(URL_STORES, ServiceHandlerFood.GET);
                Log.e("Response: ", " > " + json);
                if(json != null){
                    try{
                        JSONObject jsonObj = new JSONObject(json);
                        if(jsonObj != null){
                            JSONArray storeListFood = jsonObj.getJSONArray("storelistfood");
                            for(int i = 0; i < storeListFood.length(); i++){
                                JSONObject storeFoodObj = (JSONObject) storeListFood.get(i);
                                FoodStores foodStores = new FoodStores(storeFoodObj.getInt("id"),storeFoodObj.getString("STORENAME"));
                                storefoodList.add(foodStores);

                            }
                        }
                    }catch(JSONException e){
                        e.printStackTrace();
                    }
                }else{
                    Log.e("JSON Data", "No data received from server");
                }
                return null;
            }
@Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);
            populateListView();
        }
    }

Here is the function of my populateListView

private void populateListView(){
        List<String> labels = new ArrayList<String>();
        for(int i = 0; i < storefoodList.size(); i++){
            labels.add(storefoodList.get(i).getName());
        }
        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,R.layout.restaurant_list,labels);
        Log.d("Labels:", labels.toString());         
        lv.setAdapter(listAdapter);    
    }

What I am trying to do here is to get the list of stores from PHP to Android via JSON and store them in ArrayList and then populate them into ListView. I have tried displaying labels, it is displaying the correct stuff.

The error from Emulator is that it just shows blank screen and then it crashes. The error from logcat is

java.lang.NullPointerException
            at call.rocket.user.callarocket.ChooseCategory.populateListView
1
  • Try to check storefoodList data before get value from specific index. Commented Jun 8, 2015 at 11:08

1 Answer 1

2

Do like

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,R.layout.restaurant_list,labels);
setListAdapter(listAdapter);

as you directly extends ListActivity also remove

 lv = (ListView)findViewById(R.id.list);

and make sure your ListView id is android:id="@android:id/list"

Go to Tutorial

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.