-2

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

Yuchun Cui
  • 69
  • 2
  • 7
  • Where are you getting datalist and arg2? – ElefantPhace Oct 13 '13 at 20:14
  • Oh sorry I didn't specify that. These two parameters are from the `onItemClick()` of a `OnItemClickListener`: `listView2.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, final int arg2, long arg3) {...}}` – Yuchun Cui Oct 13 '13 at 20:18
  • Then that arg2 does not pass to the dialog's onclick. You're also trying to set that text only when you click the add button. – ElefantPhace Oct 13 '13 at 20:23
  • Actually it passed to onclick... sorry but what do you mean by "I'm trying to set that text only when you click the add button"? You suggest to set the text outside the onClick function? Thanks! – Yuchun Cui Oct 13 '13 at 20:39
  • 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 – ElefantPhace Oct 13 '13 at 20:43
  • Oh yes, you are right, just a programming mistake... Thanks so much for the patient help @ElefantPhace. I don't know how to make your answer the correct one for the public so I just put it downside with my update.. – Yuchun Cui Oct 13 '13 at 21:04

3 Answers3

1

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

ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
0

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.

Prashant Patel
  • 238
  • 2
  • 12
  • Hi Prashant thanks for your reply. As you said, I changed `inputNumber.setText(dataList.get(arg2).get("number"))` to `inputNumber.setText(dataList.get(arg2).get("number").toString())`, but it still didn't work... – Yuchun Cui Oct 13 '13 at 19:56
  • There must be a liitle codeing problem besides converting it in string. I'll check the whole code in eclipse and then reply you soon. Keep working – Prashant Patel Oct 14 '13 at 15:25
  • Hey Prashant I finally solved it with ElefantPhace's advice below. You're right that's a programmatic mistake. Thanks for your help! – Yuchun Cui Oct 14 '13 at 15:37
0

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!

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Yuchun Cui
  • 69
  • 2
  • 7