java - 将值作为对象获取如何将其转换为字符串并检索其值

标签 java android

Java Bean Class

package com.app.deallocator;

public class JavaBean {
String val1;

public String getVal1() {
    return val1;
}

public void setVal1(String val1) {
    this.val1 = val1;
}

}
 Class MainActivity
protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            MyCollectionServices sh = new MyCollectionServices();

            // Making a request to url and getting response
            ArrayList list = new ArrayList<JavaBean>();

            list = sh.makeServiceCall(url, getBaseContext());

            Log.d("Response: ", "> " + mybean);



            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            pDialog.dismiss();

            if (list != null) {
                ArrayList<String> l = new ArrayList<String>();
                for(int i=0; i<list.size(); i++){
                    String s = String.valueOf(list.get(i).toString());
                    l.add(s);
                    Log.d("GetCategoryMain",""+ s);

                }

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_spinner_item, l);

                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //set the ArrayAdapter to the spinner
                spin.setAdapter(dataAdapter);
                //attach the listener to the spinn

        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

Getting the result as,

com.app.deallocator.JavaBean@40ce3210,

com.app.deallocator.JavaBean@40ce3210,

com.app.deallocator.JavaBean@40ce3210,

How to convert it to string and retrieve its value.

最佳答案

在您的 JavaBean 类上重写 toString() 方法,如下所示,并从您的类中获取所需的数据。

    package com.app.deallocator;

    public class JavaBean {

    String val1;

    public String getVal1() {
        return val1;
    }

    public void setVal1(String val1) {
        this.val1 = val1;
    }

    @Override
    public String toString() {
        return val1;
    }

}

更多详情请参阅here .

关于java - 将值作为对象获取如何将其转换为字符串并检索其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677767/

相关文章:

java - 用于远程 JVM 的 JVisualVM CPU 分析

java - Hashcode 相等是否意味着引用基于引用的相等?

android - 建立失败的 flutter

android - Retrofit 2 下载图像并保存到文件夹

java - 如何创建一个分配多个参数的构造函数?

java - 如何在 Firebase 中检索已推送给子级的最新数据?

java - 在 JPA 规范中应用 Order By

android - 我在与另一个布局重叠的坐标布局中包含布局

android - 在 Android 中使用 Proguard 的简易模式

java - 为什么在 this 方法中添加一个 If 语句会大大降低它的速度?