I want to put a string in a EditText in a AlertDialog,which is in a onItemClick Listener with a ListVIew.Here is the part of code

dialogbuilder= new AlertDialog.Builder(this)

          .setTitle(R.string.title_connect_dialog)

          .setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))

          .setPositiveButton("Connect", new DialogInterface.OnClickListener() {

              @Override

              public void onClick(DialogInterface dialog, int which) {
              }

          })

          .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

              @Override

              public void onClick(DialogInterface dialog, int which) {
              }

          })

          .create();



  LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  final View view = inflater.inflate(R.layout.dialog_connect, null);

 edit1 = (EditText)view.findViewById(R.id.devicename);

   edit2 = (EditText)view.findViewById(R.id.deviceadd);



private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {



  public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {

        String info = ((TextView) v).getText().toString();

        name = info.substring(0,info.length() - 18);

        address = info.substring(info.length() - 17);

        dialogbuilder.show();

            edit1.setText(name);

            edit2.setText(address);

       }

};

When I run the project,there is nothing in the EditText!It does not work!But there is no exception or error.I tried only set text in the XML file can work.I replaced EditText with TextView ,but is not work too. Are there something wrong in logical?Thank you for reading.

有帮助吗?

解决方案

Use as view instance for accessing Views form Dialog layout which you are passing in setView method of AlertDialog.Builder change:

  .setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))

to

  .setView(view)

EDIT:

or instead of inflate layout again to access views you should use dialogbuilder instance to initialize EditText as:

edit1 = (EditText)dialogbuilder.findViewById(R.id.devicename);
edit2 = (EditText)dialogbuilder.findViewById(R.id.deviceadd);

其他提示

You are showing the Dialog before setting the text

dialogbuilder.show();

edit1.setText(name);
edit2.setText(address);

Try changing that around

edit1.setText(name);
edit2.setText(address);
dialogbuilder.show();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top