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...

有帮助吗?

解决方案

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

其他提示

use toString() Method for converting text into string and then place it in appropriate place. I faced a same problem and solved by this method.

I solved my second problem in this post. It was a careless mistaken by querying wrong ArrayList. This time the odd numbers don't exist. But the EditText is still not able to show the characters with what I want. Pls help if possible.

Update: I've found that though the EditView is not able to set the values visible to the users, it does get the value - I've tested that. What's odd is, whatever I input in the two blanks(should have shown the values) and press "Add" button, the app will always save the original value I try to let them show.

Specifically:

EditText inputNumber = (EditText) view.findViewById(R.id.inputNumber);
EditText inputRemark = (EditText) view.findViewById(R.id.inputRemark);
inputNumber.setText(contactList.get(arg2).get("number").toString());
inputRemark.setText(contactList.get(arg2).get("name").toString());
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);

My EditTexts are able to get the contactList.get(arg2).get("number").toString() and contactList.get(arg2).get("name").toString(), but just don't show them. And whatever else I input on the EditTexts, they will pass the two values above, instead of what I newly input, though I've specified String number = inputNumber.getText().toString() and String remark = inputRemark.getText().toString().

Finally update: Problem solved, it's not a hard tech one but a programming logical mistake. Please refer to @ElefantPhace's answer on the upper side if any one encounters same problem. Thanks all!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top