0

I am trying to use the 2nd value of each of my JSON elemnts and use it in a array of web urls. What I am trying to end of with is a array of urls that include the image names from my json data below.

JSON Data:

[["1","Dragon Neck Tattoo","thm_polaroid.jpg","polaroid.jpg"],["2","Neck Tattoo","thm_default.jpg","default.jpg"],["3","Sweet Tattoo","thm_enhanced-buzz-9667-1270841394-4.jpg","enhanced-buzz-9667-1270841394-4.jpg"]]

MainActivity:

 Bundle bundle = getIntent().getExtras();
 String jsonData = bundle.getString("jsonData");


                try {

//THIS IS WHERE THE VALUES WILL GET ASSIGNED
                    JSONArray jsonArray = new JSONArray(jsonData);


        private String[] mStrings=
            {
                 for(int i=0;i<jsonArray.length();i++)
                    {
                       "http://www.mywebsite.com/images/" + jsonArray(i)(2),
                     }
             }


    list=(ListView)findViewById(R.id.list);
        adapter=new LazyAdapter(this, mStrings);
        list.setAdapter(adapter);



                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

1 Answer 1

1

As I mentioned in a previous answer, a JSONArray is just an object, not a numerically-indexable array. It looks like you're having trouble with basic Java syntax, as well.

If you actually want to use a String[], not a List<String>:

private String[] mStrings = new String[jsonArray.length()];

for (int i=0; i<jsonArray.length(); i++)
{
    String url = jsonArray.getJSONArray(i).getString(2);
    mStrings[i] = "http://www.mywebsite.com/images/" + url;
}

If the LazyAdapter you're using can take a List, that'll be even easier to work with:

private List<String> mStrings = new ArrayList<String>();

for (int i=0; i<jsonArray.length(); i++)
{
    String url = jsonArray.getJSONArray(i).getString(2);
    mStrings.add("http://www.mywebsite.com/images/" + url);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I am still trying to wrap my head around the syntax among many other basic things. +1 Could you help me with an example using List<Sting> Or should I ask on another post.
Already edited my answer to include List<String>. BTW, try to be consistent and clean with your indentation - it will improve code readability significantly and make things easier in general.
Will do. Wish the tab key didn't focus on another window each time though.

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.