0
package com.fyp.khursani;


import java.util.ArrayList;

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

import org.apache.http.NameValuePair;

import test.fyp.khursani.library.JSONParser;

import com.fyp.khursani.activity.SingleListItem;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ViewDishes extends ListFragment {

    JSONParser jsonParser = new JSONParser();
    private static String url_menu = "http://192.168.56.1/khursaani/get_menu_details.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "menuItemName";
    private static final String TAG_PRICE = "menuItemPrice";
    private static final String TAG_DESC = "menuItemDescription";
    private static final String TAG_UNIT = "menuItemUnit";
    private static final String TAG_IMG = "menuItemImage";
    private static final String TAG_MENU = "menu";

    //progress dialogue

    private ProgressDialog pDialog;

    JSONArray events = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_viewdishes, container, false);

        // storing string resources into Array
        //            String[] adobe_products = getResources().getStringArray(R.array.adobe_products);

        // Binding resources Array to ListAdapter
        //        this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, adobe_products));
        new asyncmenu().execute();
        return rootView;
    }

    class asyncmenu extends AsyncTask<String, String, String[]> {

        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected String[] doInBackground(String... params) {
            ArrayList<NameValuePair> postparameters2send = new ArrayList<NameValuePair>();
            JSONObject json = jsonParser.makeHttpRequest(url_menu, "POST", postparameters2send);

            Log.d("response from php", json.toString());

            int status = 0;
            try {

                int success = json.getInt(TAG_SUCCESS);
                JSONArray menu = null;
                menu = json.getJSONArray(TAG_MENU);
                String[] menuarray = new String[100];

                for (int i = 0; i < menu.length(); i++) {
                    JSONObject c = menu.getJSONObject(i);
                    menuarray[i] = c.getString(TAG_NAME);
                    Log.d("menu", menuarray[i]);
                }

                return menuarray;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String[] menuarray = new String[10];
            return menuarray;
        }


        protected void onPostExecute(String[] result) {
            Log.d("onPostExecute", result[0]);

            ViewDishes.this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, result));

        }
    }

}

In this file above , there was previously an array R.array.adobe_products which looked like this :

String[] adobe_products = getResources().getStringArray(R.array.adobe_products);

xml file :

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="adobe_products">
    <item>Tandoori Chicken</item>
    <item>Chicken Tikka</item>
    <item>Sheikh Kebab</item>
    <item>Tandoori Prawn</item>
    <item>Chicken Curry</item>
    <item>Chicken Do Pizza</item>
    <item>Nepal Ice</item>
    <item>Corona Beer</item>
    <item>Coca Cola</item>
    <item>Ginger Ale</item>
    <item>Vanilla Ice Cream</item>
    <item>Mango Melba</item>
    <item>Masala Chai</item>
</string-array>

</resources>

This was output to listfragment like this:

 this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, adobe_products));

I now have an array of strings named result (in function onPostExecute) that is received after parsing a json file from a remote server, which is a plain array (as opposed to adobe_products, which is a fancy xml).

The array adobe_products is displayed as is expected. To display the new array (result), i've tried passing the new array in place of `adobe_products like so:

ViewDishes.this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_viewdishes, R.id.label, result));

which doesn't seem to work[crashes].

LogCat output:

java.lang.NullPointerException
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)

How can I display the array as a list of items?

10
  • Define "doesn't seem to work". Does it crash? (post crash logs if so) Does your list show up empty? Commented May 1, 2014 at 21:08
  • Please post your code where you create the String[] result. Commented May 1, 2014 at 21:13
  • yes. it does crash. going to update the question. Commented May 1, 2014 at 21:14
  • so it crash when you add the arrayadapter on the postexecute but not on the createView? Commented May 1, 2014 at 21:34
  • Does getActivity() return null? Commented May 1, 2014 at 22:06

1 Answer 1

0

I always prefer using an ArrayList instead of an Array (specially to populate List adapters, there's a reason they both end in "List" and not array), because they can grow after being declared.

Refer to this thread.

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.