简体   繁体   English

如何在我的对象中从getter烘烤数组值?

[英]how do I toast array values from getter in my object?

My JSON Array is of this format and I am trying to toast the user_review_ids values - 63,59,62 . JSON Array是这种格式的,我想toastuser_review_ids值- 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. 我一直得到的是对值的reference ,而不是对值本身的reference -我进来toast'[Ljava.lang.String.;@41c19f...东西。

From what I read I thought methods like copyOf or clone() might do the trick but no joy. 从我的阅读中,我认为像copyOfclone()可能会成功,但没有乐趣。

My JSON Array is in this format, on my server: 我的服务器上的JSON Array是这种格式:

       [{"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. 当我尝试toast user_personal_countprivate_countpublic_count任何值时,我都没有问题,只是数组。

Here is my code in the Category object: 这是我在Category对象中的代码:

    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();
}

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. 我不知道您的应用程序是关于什么的,但是Toast绝对不是向用户显示内容的最佳方法。

Otherwise, to answer your question, yes you're displaying the reference to the array and not the array itself, that why you have @41c19f 否则,要回答您的问题,是的,您正在显示对数组的引用,而不是数组本身,这就是为什么要使用@41c19f

Use String.join(delimiter,stringArray); 使用String.join(delimiter,stringArray); . At the place of delimiter you can use comma( , ) or something else. 在的地方delimiter ,你可以用逗号( , )或别的东西。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从xlsx文件读取值以通过getter和setter将值存储到对象,最后到数组列表 - how to read values from xlsx file to store value through getter and setter to object and finally to array list 如何将我的信息连接到我的吐司消息 - How do I connect my information to my toast message Android-如何从JSON数组解析特定值并显示Toast - Android - How to parse specific values from JSON Array and display Toast Java:如何阻止人们清除我的arraylist <string> 什么时候使用吸气剂方法? - java: how do I stop people from clearing my arraylist<string> When I use a getter method? 我如何烘烤RecyclerView中的复选框数量? - how do I toast the number of checked boxes in my RecyclerView? Java:如何创建从继承的类中获取对象的getter方法? - Java: How do I create a getter method that gets an object from an inherited class? 如何将值从我的静态方法传递给我的对象数组 - How to pass values to my object array from my static method 如何在一个方法中从数组中获取值列表,并将这些值从我的main方法放入一个新数组中 - how do i take a list of values from an array in one method and put those values into a new array from my main method 如果每个数组的值等于从数据库中检索到的值,如何检查? - How do I check the values for each array if it's equal to what I retrieved from my database? 如何在迭代器中对getter方法的值求和? - How can I sum values from a getter method within an iterator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM