Search code examples
androidviewandroid-edittextsettext

EditText.setText() not working & getting wrong values from EditText


I'm a starter on Android, mocking a contact list of a phone. Now I have a contact list, like the pic below, when I press the one item of the contact list, it pops up a dialog with two choices. And if I choose "Add to Black", another AlertDialog allows me to put this number to the blacklist. What I want to realize here is to automatically read the number of the item I picked, show it in the "number" blank, which doesn't require users to input again. But it turned out it didn't work, still nothing in the blank. The screenshots and codes of showing the add-black-dialog are below.

enter image description here enter image description hereenter image description here

final View view = getLayoutInflater().inflate(R.layout.add_person, null);
                            AlertDialog alertDialog = new AlertDialog.Builder(MyTabHost.this).setTitle("Add a Black Number")
                                    .setView(view).setPositiveButton("Add", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            EditText inputNumber = (EditText) view.findViewById(R.id.inputNumber);
                                            EditText inputRemark = (EditText) view.findViewById(R.id.inputRemark);
                                            inputNumber.setText(dataList.get(arg2).get("number"));
                                            String number = inputNumber.getText().toString();
                                            String remark = inputRemark.getText().toString();
                                            Map<String, String> map = new HashMap<String, String>();
                                            map.put("remark", remark);
                                            map.put("number", number);
                                            map.put("display", Utils.formatPhoneNumber(number));
                                            if (MyTabHost.blackList.get(0).containsValue("You don't have any black number")) {
                                                MyTabHost.blackList.removeAll(MyTabHost.blackList);
                                            }
                                            MyTabHost.blackList.add(map);
                                            int i = mySharedPreference.getBlackSize() + 1;
                                            mySharedPreference.saveBlack(remark, number, i);
                                            mySharedPreference.saveBlackSize(i);
                                            dialog.dismiss();
                                            new AlertDialog.Builder(MyTabHost.this)
                                                    .setMessage("Black number succesfully added")
                                                    .setPositiveButton("OK", null).show();
                                        }
                                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    }).create();
                            alertDialog.show();

Actually I'm thinking whether I am using wrong view. As the codes state above, I use final View view = getLayoutInflater().inflate(R.layout.add_person, null);to get the new view of the dialog, which add_person.xml here is my layout.

The reason why I wondering about the wrong view is that something weirder happened: when I manually inputed the number and remark(say I pressed the contact "Jack"'s number:8871203459 and "Jack" as a remark) and pressed "Add", meanwhile the things in the blanks suddenly change to some numbers else(some numbers I got in other activities), like below, and the black data stored was also the odd wrong number.

enter image description here

That's odd because I did write the codes of getText(), and saved it:

EditText inputNumber = (EditText) view.findViewById(R.id.inputNumber);
                                            EditText inputRemark = (EditText) view.findViewById(R.id.inputRemark);
                                            inputNumber.setText(dataList.get(arg2).get("number"));
                                            String number = inputNumber.getText().toString();
                                            String remark = inputRemark.getText().toString();
                                            Map<String, String> map = new HashMap<String, String>();
                                            map.put("remark", remark);
                                            map.put("number", number);
                                            map.put("display", Utils.formatPhoneNumber(number));

This is a long and boring problem. Thanks for your reading and help...


Solution

  • Just moved relevant comment down into an answer.

    That setText is only being run once you click the add button on your dialog. Move both find views and the setText out of the dialogs on click