获取内部类中的值

huangapple go评论75阅读模式
英文:

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 = {&quot;&quot;};
        StringRequest strRequest = new StringRequest(Request.Method.POST, &quot;http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php&quot;,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response) {
                        try{
                            JSONObject jsonObject = new JSONObject(response);
                            id[0] = jsonObject.getString(&quot;PatientID&quot;);
                        }
                        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 = {&quot;&quot;};

答案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 = {&quot;&quot;};

        StringRequest strRequest = new StringRequest(Request.Method.POST,
                &quot;http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php&quot;,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            id[0] = jsonObject.getString(&quot;PatientID&quot;);
                        } 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 = {&quot;&quot;}声明为成员变量,并将其声明为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 = {&quot;&quot;} 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, &quot;http://192.168.254.199/projects/AndroidServers/TheMedicalGCs/GetPatientID.php&quot;,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response) {
                        try{
                            JSONObject jsonObject = new JSONObject(response);
                            id = jsonObject.getString(&quot;PatientID&quot;);
                        }
                        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;
    }
}

huangapple
  • 本文由 发表于 2020年9月22日 20:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定