对话框中的EditText值未存储

时间:2016-07-16 15:36:12

标签: android android-edittext android-alertdialog

我有一个带有列表视图的应用程序和一个打开对话框的按钮。对话框有一个EditText字段,用户可以在其中输入文本并单击“添加”。 “添加”按钮的onClickListener进行异步调用并将文本传递给api。问题是EditText值未保存。我甚至无法在日志中显示它。我确定我错过了一些简单的东西你的帮助很少。这是代码。

SignInActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

    Log.v("SignInActivity","Activity has began");

    final DancerAdapter adapter = new DancerAdapter(this,oneDancerArrayList,1);

    Log.v("SignInActivity","DancerAdapter");

    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

    getGirlList();

    nameInput = (EditText) findViewById(R.id.cr_room_name);
    loginDancer = (Button) findViewById(R.id.addDancer);
    loginDancer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment newFragment = new AddDancerDialog();
            newFragment.show(getFragmentManager(), "newDancer");
        }
    });

    static public class AddDancerDialog extends DialogFragment {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setView(R.layout.signin_dialog);

            LayoutInflater inflater = LayoutInflater.from(getContext());
            final View dialogview = inflater.inflate(R.layout.signin_dialog, null);

            //final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);

            builder.setMessage("Login New Dancer")
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            final ProgressBar newProgress = (ProgressBar)dialogview.findViewById(R.id.progressBar);
                            final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);

                            String nameSubmit = dancerName.getText().toString();
                            Log.v("SignInActivity-Dialog",nameSubmit);
                            newProgress.setVisibility(View.VISIBLE);

                            AsyncHttpClient client = new AsyncHttpClient();
                            RequestParams params = new RequestParams();
                            params.put("action", "addDancer");
                            params.put("name",nameSubmit);

                            Log.v("SignInActivity-Dialog", "Add Dancer Function");
                            Log.v("SignInActivity-Dialog",dancerName.getText().toString());

                            client.post("http://peekatu.com/apiweb/girlList.php", params,
                                    new AsyncHttpResponseHandler() {
                                        @Override
                                        public void onSuccess(String response) {
                                            Log.v("response", response);
                                            responseString2 = response;
                                            //parseDancerList(response);
                                            Log.v("SignInActivity",response);
                                        }

                                        @Override
                                        public void onFailure(Throwable error, String content) {
                                            Log.v("response", "response failed network error");
                                            //waitncall(true);   
                                        }    
                                    });
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        }
                    });
            return builder.create();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题出在这里:

在此设置视图到对话框

builder.setView(R.layout.signin_dialog);

然后,您在此视图中搜索EditText

LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);

dialogView被夸大但不是添加的视图 对话。他们是不同的..

我认为您可以修改如下:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = LayoutInflater.from(getContext());
    final View dialogview = inflater.inflate(R.layout.signin_dialog, null);

    builder.setView(dialogview);

    ...
}