0

I am creating an image-view from JSON URL where I am pushing the URL in a array-list. Here is the code.

private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ver = (TextView) findViewById(R.id.vers);
        name = (TextView) findViewById(R.id.name);
        api = (TextView) findViewById(R.id.api);
        pDialog = new ProgressDialog(AnotherActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);

         try {
            // Getting JSON Array from URL
            android = json.getJSONArray(TAG_OS);
            for (int i = 0; i < android.length(); i++) 
                {
                map = new ArrayList<HashMap<String, String>>();
                JSONObject c = android.getJSONObject(i);
                // Storing each json item in variable
                String flag = c.getString("flag");
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("url",flag);
                arraylist.add(i,"\"" + map.toString().substring(5).replace("}", "\""));
            }
           final  String[] imageUrl= arraylist.toArray(new String[arraylist.size()]);
            Log.v("url", "Creating view..." + imageUrl);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
} 

Now I want to use this array-list in a string array.

String[] IMAGES = new String[]{

};

How can I do that?? When I tried something like this

String[] imageUrl= arraylist.toArray(new String[arraylist.size()]);

I got the the log as

Creating view...[Ljava.lang.String;@41450ea0
Illidanek
  • 996
  • 1
  • 18
  • 32
diwa
  • 169
  • 2
  • 2
  • 11

1 Answers1

0

Use Arrays.toString(array[]) method to print array as here you are getting [Ljava.lang.String;@41450ea0 Which isObject reference for array.

 String[] imageUrl= arraylist.toArray(new String[arraylist.size()]);
 Log.v("url", "Creating view..." + Arrays.toString(imageUrl));

Which will print array like this

[element1,element2...]

You can remove [ ] by the use of indexOf

String s= Arrays.toString(imageUrl);
s = s.substring(1, s.length()-1)
akash
  • 22,664
  • 11
  • 59
  • 87
  • Thank you for the reply. it worked great. and sorry for bothering you. can you help me with something else also?. Array list contains a square bracket at the beginning and and end. I tried replace which is not working.. Any other way? – diwa Jun 17 '14 at 10:34
  • 1
    Your `replaceAll` won't work. It should be `.replaceAll("^\\[(.*)\\]$","$1");` – BackSlash Jun 17 '14 at 10:43
  • Your new edit is wrong too. If there are square brackets in the array elements, it will remove them too. My regex will just remove the first and the last square bracket. Alternatively you can do `String s = Arrays.toString(imageUrl); s = s.substring(1, s.length()-1)`. There is no need for regex at all – BackSlash Jun 17 '14 at 10:47