简体   繁体   English

如何使用 json object 从上下文处于 mainactivity 的对话框的 editText 发送数据?

[英]how can i send data from editText of dialog box whose context is in mainactivity using json object?

While submitting data from editTexts of Dialog box whose context is in Home activity, the submitted button doing nothing.从上下文在 Home 活动中的对话框的 editTexts 提交数据时,提交的按钮什么也不做。

host is a button on home activity,when clicked opens up a dialog box in same activity consists of two editText field (ename,eemail) and submit, cancel button. host 是 home Activity 上的一个按钮,单击时会在同一 Activity 中打开一个对话框,该对话框由两个 editText 字段(ename、eemail)和提交、取消按钮组成。 On submit getFeedback() functions called (used from ApiInterface method)在提交时调用 getFeedback() 函数(从 ApiInterface 方法使用)

host.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view)
    {
        final Dialog d=new Dialog(context);
        d.setCanceledOnTouchOutside(false);
        d.setContentView(R.layout.hostdialog);
        d.getWindow().setBackgroundDrawableResource(R.drawable.bgdialog);
        final EditText ename=(EditText)d.findViewById(R.id.ename);
        final EditText eemail=(EditText)d.findViewById(R.id.eemail);
        final Button submit=(Button)d.findViewById(R.id.submit);
        final Button clear=(Button)d.findViewById(R.id.clear);

        submit.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View view) {
                try {
                    getFeedbacks(ename.getText().toString(),eemail.getText().toString());
                    d.dismiss();
                    Toast t = Toast.makeText(getApplicationContext()," ✓ Thank You, We'll contact you soon",Toast.LENGTH_LONG);
                    View v = t.getView();
                    v.setBackgroundResource(R.drawable.clear);
                    t.show();

                 } catch (Exception e) {
                   e.printStackTrace();
                 }
             }
         });

         clear.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 d.dismiss();
             }
         });

         d.show();
     } 
});

and my ApiInterface code is:我的 ApiInterface 代码是:

@GET("myurl")
Call<JsonElement> getFeedbacks(@Body RequestBody requestBody);

getFeedback method consist of:- getFeedback 方法包括:-

    private void getFeedbacks(String user_name, String user_email){

    JSONObject paramobject = new JSONObject();

    try {
        paramobject.put("user_name",user_name);
        paramobject.put("user_email",user_email);

    } catch (JSONException e) {
        e.printStackTrace();

    }
    RequestBody body = RequestBody.create(MediaType.parse("application/json,charset=utf-8"),paramobject.toString());
    Call<JsonElement> call = retrofitAPI.getFeedbacks(body);
    call.enqueue(new Callback<JsonElement>() {
        @Override
        public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

            try {
                JSONArray jsonArray = new JSONArray(response.body().toString());
                JSONObject jsonObject = new JSONObject();

                for (int n = 0; n < jsonArray.length(); n++){
                    jsonObject = jsonArray.getJSONObject(n);
                    String successval =  jsonObject.optString("success");

                    if(successval.equals("0")){
                        Toast t=Toast.makeText(getApplicationContext()," Request Failed, Try Again",Toast.LENGTH_LONG);
                        View v=t.getView();

                        v.setBackgroundResource(R.drawable.clear);
                        t.show();


                    }else {
                        Toast t=Toast.makeText(getApplicationContext()," ✓ Thank You, We'll contact you soon",Toast.LENGTH_LONG);
                        View v=t.getView();

                        v.setBackgroundResource(R.drawable.clear);
                        t.show();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<JsonElement> call, Throwable t) {
        Toast.makeText(Home.this,"Error,Try Again",Toast.LENGTH_LONG).show();
        }
    });

}

Button does not works.按钮不起作用。 Please provide appropriate solution.请提供适当的解决方案。

now i alter a little bit code as i change the call anonymously within method.现在我更改了一些代码,因为我在方法中匿名更改了调用。 but again no work但又没有工作

          host.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Dialog d = new Dialog(context);
            d.setCanceledOnTouchOutside(false);
            d.setContentView(R.layout.hostdialog);
            d.getWindow().setBackgroundDrawableResource(R.drawable.bgdialog);
            final EditText ename = (EditText) d.findViewById(R.id.ename);
            final EditText eemail = (EditText) d.findViewById(R.id.eemail);
            final Button submit = (Button) d.findViewById(R.id.submit);
            final Button clear = (Button) d.findViewById(R.id.clear);
            submit.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("WrongConstant")
                @Override
                public void onClick(View view) {
                    try {
                        sendDataToServer();
                        d.dismiss();
                        Toast t = Toast.makeText(getApplicationContext(), " ✓ Thank You, We'll contact you soon", Toast.LENGTH_LONG);
                        View v = t.getView();
                        v.setBackgroundResource(R.drawable.clear);
                        t.show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                private void sendDataToServer() {
                    JSONObject paramobject = new JSONObject();

                    try {
                        paramobject.put("comment", ename);
                        paramobject.put("email", eemail);

                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                    RequestBody body = RequestBody.create(MediaType.parse("application/json,charset=utf-8"), paramobject.toString());
                    Call<JsonElement> call = retrofitAPI.getFuturehosts((RequestBody) body);
                    call.enqueue(new Callback<JsonElement>() {
                        @Override
                        public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

                            try {
                                JSONArray jsonArray = new JSONArray(response.body().toString());
                                JSONObject jsonObject = new JSONObject();

                                for (int n = 0; n < jsonArray.length(); n++) {
                                    jsonObject = jsonArray.getJSONObject(n);
                                    String successval = jsonObject.optString("success");

                                    if (successval.equals("0")) {
                                        Toast t = Toast.makeText(getApplicationContext(), " Request Failed, Try Again", Toast.LENGTH_LONG);
                                        View v = t.getView();

                                        v.setBackgroundResource(R.drawable.clear);
                                        t.show();


                                    } else {
                                        Toast t = Toast.makeText(getApplicationContext(), " ✓ Thank You, We'll contact you soon", Toast.LENGTH_LONG);
                                        View v = t.getView();

                                        v.setBackgroundResource(R.drawable.clear);
                                        t.show();
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(Call<JsonElement> call, Throwable t) {
                            Toast.makeText(Home.this, "Error,Try Again", Toast.LENGTH_LONG).show();
                        }
                    });


                }
            });
            clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    d.dismiss();
                }
            });
            d.show();
        }
    });

Check your hostdialog.xml file检查您的 hostdialog.xml 文件

-button id is "submit" or not -button id 是否为“提交”

may be that id is diffrent so the code is not working可能是 id 不同所以代码不起作用

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

相关问题 我如何从 Android Studio 的 Mainactivity 中的 Dialog 做出反应? - How can i react from Dialog in Mainactivity in Android Studio? 如何在对话框的EditText视图中捕获信息? - How do I capture information from EditText view in a dialog box? 我如何将我的 Retrofit 数据从 MainActivity 发送到我的 Fragment? - How i can send my Retrofit data from MainActivity to my Fragment? 如何使用 java 将数据从自定义对话框传递到 android 工作室中的片段? - How can i pass data from custom dialog box to Fragment in android studio using java? Android Java:当对话框关闭时,如何防止我的对话框短暂显示 MainActivity appname? - Android Java: How do I prevent my Dialog box from showing MainActivity appname briefly when the Dialog closes? 如何在 Android Studio 中从 ApplicationTest.java 编辑 MainActivity 的 EditText 字段? - How can I edit an EditText field of MainActivity from ApplicationTest.java in Android Studio? 我如何从另一个 class 访问 MainActivity object - How can i access a MainActivity object from another class 我如何将类对象从mainActivity类传递给BroadCastReviever - How can i pass the class object into the BroadCastReviever from the mainActivity class 从 Service 向 MainActivity 发送数据 - Send data to MainActivity from Service 如何在 java 中使用 dagger2 注入 MainActivity 上下文? - how do I inject MainActivity context using dagger2 in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM