0

My JSON Array is of this format and I am trying to toast the user_review_ids values - 63,59,62. I keep getting what I believe is a reference to the values, rather than the values themselves - I get in toast, '[Ljava.lang.String.;@41c19f... stuff like that.

From what I read I thought methods like copyOf or clone() might do the trick but no joy.

My JSON Array is in this format, on my server:

       [{"cat_name":"name1","user_review_ids":[63,59,62],
    "private_review_ids":[],"public_review_ids":["9999"],"user_personal_count":3,
"private_count":0,"public_count":1}]

When I try to toast any of the values for user_personal_count, private_count, public_count, I have no problems, just the array.

Here is my code in the Category object:

    String cat_name;
    String [] user_review_ids;
    String user_personal_count;
    String private_count;
    String public_count;

    public Category() {
    }

    public String getName() {
        //return the value of the JSON key named cat_name in php file
        return cat_name;
    }


    public String[] getUserReviewIds() {
        //return the value of the JSON key named user_personal_count in php file
       // return user_personal_count;
        return user_review_ids;
    }

    public String getUserPersonalCount() {
        //return the value of the JSON key named user_personal_count in php file
        return user_personal_count;
    }

    public String getPrivateCount() {
        //return the value of the JSON key named private_count in php file
        return private_count;
    }

    public String getPublicCount() {
        return public_count;
    }
}

And the function that toasts the value, which is in another class:

@Override
public void onContactSelected(Category category) {
    Toast.makeText(getApplicationContext(), "Selected: " + category.getUserReviewIds(), Toast.LENGTH_LONG).show();
}
CHarris
  • 2,693
  • 8
  • 45
  • 71

2 Answers2

1

Do:

Arrays.toString(category.getUserReviewIds())

in your Toast as explained here.

String[] array = new String[] {"John", "Mary", "Bob"};
System.out.println(Arrays.toString(array));

So your method should be like that:

@Override
public void onContactSelected(Category category) {
    Toast.makeText(getApplicationContext(), "Selected: " + Arrays.toString(category.getUserReviewIds()), Toast.LENGTH_LONG).show();
}

I don't know what your app is about but Toasts are definitively not the nicest way to display something to the user.

Otherwise, to answer your question, yes you're displaying the reference to the array and not the array itself, that why you have @41c19f

Eselfar
  • 3,759
  • 3
  • 23
  • 43
1

Use String.join(delimiter,stringArray);. At the place of delimiter you can use comma(,) or something else.

@Override
    public void onContactSelected(Category category) {
        Toast.makeText(getApplicationContext(), "Selected: " + String.join(",",category.getUserReviewIds()), Toast.LENGTH_LONG).show();
    }
Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17