gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:31:18 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com