Nothing is populated from ArrayList to ListView

You didn’t provide all classes so some classes are provided by me.
Here you are:

Screenshot

ChooseCategory.java:

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ChooseCategory extends ListActivity {

    private ListView lv;
    ArrayAdapter<FoodStores> arrayAdapter;

    private static final String TAG = "XXX";
    private ArrayList<FoodStores> storefoodList;
    // JSON parser class
    // JSONParser jsonParser = new JSONParser();
    // ids
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private String URL_STORES = "http://echo.jsontest.com/id/1/STORENAME/Perogi";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(android.R.id.list);
        storefoodList = new ArrayList<FoodStores>();
        new GetFoodStores().execute();

        arrayAdapter = new ArrayAdapter<FoodStores>(this,
                R.layout.restaurant_list, R.id.Itemname, storefoodList);
        lv.setAdapter(arrayAdapter);
    }

    private class GetFoodStores extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            ServiceHandlerFood jsonParserFood = new ServiceHandlerFood();
            String json = jsonParserFood.makeServiceCall(URL_STORES,
                    "TEST");
                    //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);
            lv.setAdapter(arrayAdapter);
        }
    }
}

Foodstores.java:

public class FoodStores {

    int id;
    String name;
    public FoodStores(int id, String string) {
        this.id=id;
        name=string;
    }
    @Override
    public String toString(){
        return name;
    }

}

ServiceHandlerFood.java:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

public class ServiceHandlerFood {

    public static final String GET = "GET";

    public String makeServiceCall(String uRL_STORES, String get2) {
        if ("TEST".equals(get2))
            return "{'storelistfood':[{'id':1, 'STORENAME':'Arbys'},{'id':2, 'STORENAME':'Perogi'},{'id':3, 'STORENAME':'McDonalds'}]}";

        DefaultHttpClient httpclient = new DefaultHttpClient(
                new BasicHttpParams());
        HttpPost httppost = new HttpPost(uRL_STORES);
        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            result = sb.toString();
        } catch (Exception e) {
            // Oops
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (Exception squish) {
            }
        }
        return result;
    }

}

The rest of files was untouched.

Browse More Popular Posts

Leave a Comment