英文:
Getting the value inside the inner class
问题
如何在Android Studio 4.0.1中获取内部类中的值。我尝试过声明全局变量,但还是不起作用。我还尝试过声明一个final数组,但我仍然无法获取值。
public class FetchData {
private Context con;
public FetchData(Context con){
this.con = con;
}
public String getPatientIDFromDatabase(){
final String[] id = {""};
StringRequest strRequest = new StringRequest(Request.Method.POST, "http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
id[0] = jsonObject.getString("PatientID");
}
catch (Exception e){
Toast.makeText(con, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(con, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(con).add(strRequest);
return id[0];
}
}
我只希望这个方法返回来自JSON的值。
英文:
How will I get the value inside the inner class in Android Studio 4.0.1. I tried to declare a global variable but still it doesn't work. I also tried this one declaring a final array but I still cannot get the value
public class FetchData {
private Context con;
public FetchData(Context con){
this.con = con;
}
public String getPatientIDFromDatabase(){
final String[] id = {""};
StringRequest strRequest = new StringRequest(Request.Method.POST, "http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
id[0] = jsonObject.getString("PatientID");
}
catch (Exception e){
Toast.makeText(con, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(con, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(con).add(strRequest);
return id[0];
}
}
I just want that method to return the value comes from the JSON.
答案1
得分: 0
你可以将其声明为静态变量,
static final String[] id = {""};
英文:
You can declare that as a static variable,
static final String[] id = {""};
答案2
得分: 0
我已经将类FetchData
中的构造函数移除,并将其作为参数移到方法getPatientIDFromDatabase(Context context)
中。所以它现在的样子是这样的:
public class FetchData {
public static String getPatientIDFromDatabase(Context context) {
final String[] id = {""};
StringRequest strRequest = new StringRequest(Request.Method.POST,
"http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
id[0] = jsonObject.getString("PatientID");
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(context).add(strRequest);
return id[0];
}
}
现在让我们来获取从FetchData
类返回的值,但如果你想让其他类也能访问返回值,你需要将方法设为静态的。我已经在上面添加了。
现在让我们从名为HomeActivity
的其他类中获取字符串值:
String value = FetchData.getPatientIDFromDatabase(context);
英文:
I have removed constructor for your classFetchData
and moved it as parameter to method getPatientIDFromDatabase(Context context)
. So the looks like this.
public class FetchData {
public static String getPatientIDFromDatabase(Context context) {
final String[] id = {""};
StringRequest strRequest = new StringRequest(Request.Method.POST,
"http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
id[0] = jsonObject.getString("PatientID");
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(context).add(strRequest);
return id[0];
}
}
Now let's the value it is returning from FetchData
class, but if you want other classes to get access to the return value, you need to method as static. Which i already added above.
Now let's get string value from other class named HomeActivity:
String value = FetchData.getPatientIDFromDatabase(context)
答案3
得分: 0
我已经通过将final String[] id = {""}
声明为成员变量,并将其声明为private String id
来修复了这个问题。我记得在内部类中无法访问未声明为final的变量。
public class FetchData {
private Context con;
private String id;
public FetchData(Context con){
this.con = con;
}
public String getPatientIDFromDatabase(){
StringRequest strRequest = new StringRequest(Request.Method.POST, "http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
id = jsonObject.getString("PatientID");
}
catch (Exception e){
Toast.makeText(con, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(con, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(con).add(strRequest);
return id;
}
}
英文:
I already fixed the problem by declaring the final String[] id = {""}
into a member variable and declare it as private String id
only. I remembered that you cannot access the variable that is not declared as final inside the inner class.
public class FetchData {
private Context con;
private String id;
public FetchData(Context con){
this.con = con;
}
public String getPatientIDFromDatabase(){
StringRequest strRequest = new StringRequest(Request.Method.POST, "http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
id = jsonObject.getString("PatientID");
}
catch (Exception e){
Toast.makeText(con, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(con, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
Volley.newRequestQueue(con).add(strRequest);
return id;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论