0
public class MainActivity extends ListActivity {

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<Map<String, String>> list = buildData();
        String[] from = { "Name", "Entry" };
        int[] to = { android.R.id.text2, android.R.id.text1 };

        SimpleAdapter adapter = new SimpleAdapter(this, list,
            android.R.layout.simple_list_item_2, from, to);
        setListAdapter(adapter);
      }

      private ArrayList<Map<String, String>> buildData() {
        ArrayList<Map<String, String>> list = 
                new ArrayList<Map<String, String>>();


        list.add(putData("Bulbasaur", "001"));
        list.add(putData("Ivysaur", "002"));
        list.add(putData("Venusaur", "003"));


        return list;
      }

      private HashMap<String, String> putData(String name, String entry) {

          HashMap<String, String> item = new HashMap<String,
                  String>();
          item.put("Name", name);
          item.put("Entry", entry);
        return item;
      }

What I aim to do is add a JSON reference in either the putData or buildData classes listed above. I have another class (below) that I will use to reference the locally stored JSON file.

public String loadJSONFromAsset() {
    String json = null;
    try {

        InputStream is = getAssets().open("pokelist.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");


    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}

The problem I'm having is that, no matter what I seem to do, the data from the JSON file will never be displayed in the ListActivity. I know the ListActivity works, but I'm not entirely sure how I should go about implementing the HashMap with data from a JSON file.

Can someone provide me with some guidance please. Please don't provide links; I've been looking everywhere all day for some form of example on the matter, to no avail. I could use some help that I could understand.

To clarify What I want to do is load the JSON file, from the HDD, and put the key values into a HashMap; which will then be displayed in my ListView (list).

JSON code:

{
  "pokemon": [
  {"Entry":"001", 
   "Name":"Bulbasaur", "Type":"1"},
  {"Entry":"002", 
  "Name":"Ivysaur" , "Type":"1"},
  {"Entry":"003",
   "Name":"Venusaur", "Type":"1"}
  ]
}

Recent Log Messages

12-04 22:00:53.652: E/Trace(2093): error opening trace file: No such file or directory (2) 12-04 22:00:53.712: W/System.err(2093): java.io.FileNotFoundException: pokelist.json 12-04 22:00:53.763: W/System.err(2093): at android.content.res.AssetManager.openAsset(Native Method) 12-04 22:00:53.763: W/System.err(2093): at android.content.res.AssetManager.open(AssetManager.java:315) 12-04 22:00:53.796: W/System.err(2093): at android.content.res.AssetManager.open(AssetManager.java:289) 12-04 22:00:53.796: W/System.err(2093): at com.example.myjsonproject.MainActivity.loadJSONFromAsset(MainActivity.java:78) 12-04 22:00:53.796: W/System.err(2093): at com.example.myjsonproject.MainActivity.buildData(MainActivity.java:44) 12-04 22:00:53.796: W/System.err(2093): at com.example.myjsonproject.MainActivity.onCreate(MainActivity.java:27) 12-04 22:00:53.796: W/System.err(2093): at android.app.Activity.performCreate(Activity.java:5104) 12-04 22:00:53.796: W/System.err(2093): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 12-04 22:00:53.796: W/System.err(2093): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 12-04 22:00:53.796: W/System.err(2093): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 12-04 22:00:53.796: W/System.err(2093): at android.app.ActivityThread.access$600(ActivityThread.java:141) 12-04 22:00:53.803: W/System.err(2093): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 12-04 22:00:53.836: W/System.err(2093): at android.os.Handler.dispatchMessage(Handler.java:99) 12-04 22:00:53.836: W/System.err(2093): at android.os.Looper.loop(Looper.java:137) 12-04 22:00:53.836: W/System.err(2093): at android.app.ActivityThread.main(ActivityThread.java:5041) 12-04 22:00:53.836: W/System.err(2093): at java.lang.reflect.Method.invokeNative(Native Method)

Thank you.

fishycrakers
  • 71
  • 3
  • 10
  • To clarify: you want to load a JSON file from disk, read in its values and then put its key:value pairs into a HashMap, correct? – mttdbrd Dec 04 '13 at 20:44
  • Can you paste json content here, Because json need to be converted in JsonArray object then iterate that array to build hashmap list. – bobby.dhillon Dec 04 '13 at 20:47
  • @mttdbrd Yes, that's exactly what I want to do. I'll update my post now. – fishycrakers Dec 04 '13 at 20:53

1 Answers1

0

Here is the what you can try:

JSONObject jj = new JSONObject(loadJSONFromAsset());

            JSONArray jsonArray = jj.getJSONArray("pokemon");

            for(int i = 0; i< jsonArray.length();i++) {
                JSONObject ii = jsonArray.getJSONObject(i);

                list.add(putData(ii.getString("Name"),   ii.getString("Entry")));

            }

Function for reading json

public String loadJSONFromAsset() {
                InputStream is = getApplicationContext().getAssets().open("pokelist.json");
        int size = is.available();
        buffer = new byte[size];
        is.read(buffer);
        is.close();
        String bufferString = new String(buffer);
            return bufferString ;
}

Its quite clear from these lines there is file missing in assets folder of your project.

12-04 22:00:53.652: E/Trace(2093): error opening trace file: No such file or directory (2) 12-04 22:00:53.712: W/System.err(2093): java.io.FileNotFoundException: pokelist.json 12-04 22:00:53.763:
bobby.dhillon
  • 319
  • 1
  • 6
  • OP: Just to clarify, this will be put into a try and catch? – fishycrakers Dec 04 '13 at 21:05
  • @Inafune yes use try catch, i have written answer without any "IDE" :) – bobby.dhillon Dec 04 '13 at 21:08
  • @Inafune edited the answer first line to load form the function you have created. – bobby.dhillon Dec 04 '13 at 21:13
  • All I'm getting when I execute is a blank-screen. That's what I had before when I tried doing it myself. – fishycrakers Dec 04 '13 at 21:13
  • @Inafune Try using break points and investigate the variable values. – bobby.dhillon Dec 04 '13 at 21:15
  • @bobby.dhillion Sorry, now it's crashing. 'Sorry, MyJSONProject has stopped working'. There is a problem with my loadJSONFromAsset function? – fishycrakers Dec 04 '13 at 21:16
  • @Inafune modfied your function, I cant test now, hope this work. – bobby.dhillon Dec 04 '13 at 21:23
  • @bobby.dhillion Sorry, this still didn't work. I changed it up a bit; as the code was giving errrors. Added a try+catch and changed buffer to a byte[]. Declared bufferString outside of the tryCatch. It crashed still. I'll keep on at it; as I'm determined to learn what's wrong with the thing. – fishycrakers Dec 04 '13 at 21:47
  • @bobby.dhillion I'm not sure I'm doing this right. How do I copy the error log for one specific project? – fishycrakers Dec 04 '13 at 22:03
  • @Inafune for help http://stackoverflow.com/questions/11608718/how-to-copy-logcat-output-to-clipboard [link](http://stackoverflow.com/questions/11608718/how-to-copy-logcat-output-to-clipboard) – bobby.dhillon Dec 04 '13 at 22:07
  • @bobby.dhillion not entirely sure what you are looking for so I've edited my post to show the most recent log messages. – fishycrakers Dec 04 '13 at 22:13
  • Time to leave, check edited answer, hope you need to work on placing file properly in directory and check file name. – bobby.dhillon Dec 04 '13 at 22:19
  • @Inafune You're getting a java.io.FileNotFoundException. The path to the JSON file must be incorrect. Are you storing it in the assets project folder? – mttdbrd Dec 04 '13 at 22:29
  • @bobby.dhillon I had it stored in: C:\Users\New\workspace\MyJSONproject\res\raw -__- Moved the file to Assets. Thank you so much. Another quick question though. If I want to implement an onListItemClick() listener then would I use that standard function and reference list[position]? somehow. – fishycrakers Dec 04 '13 at 22:32