user773737
user773737

Reputation:

Android AlertDialog buttons unresponsive

So I have this dialog box that pops up.

The buttons, however, are unresponsive. The debug lines inside of the onClick methods aren't being reached.

What am I doing wrong or not doing?

Here's my code, followed by my XML for the dialog box.

    lv = new ListView(this);
    lv.setAdapter(aa);
    lv.setId(getTaskId());
    setContentView(lv);

    lv.setOnItemClickListener(new  OnItemClickListener() {         
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos,
                long id) {
             TextView v=(TextView) view.findViewById(R.id.info);
             String str = (String) v.getText();
             String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
             AlertDialog.Builder b = new AlertDialog.Builder(context);
             b.setView(getLayoutInflater().inflate(R.layout.loginbox,(ViewGroup) view));
             b.setPositiveButton(R.id.loginbutton, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                      Log.i("Button","Log in");
                   }
               });
               b.setNegativeButton(R.id.cancelbutton, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       Log.i("Button","Cancel");
                       dialog.dismiss();
                   }
               });

             Log.i("Click",ssid);
        }       
        });

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="horizontal" >

<EditText
    android:id="@+id/username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="16dp"
    android:hint="@string/ssidlabel"
    android:inputType="textEmailAddress"
    android:text="@string/ssidlabel" />

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/username"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:fontFamily="sans-serif"
    android:hint="@string/password"
    android:inputType="textPassword"
    android:text="@string/password" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/loginbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/username"
    android:layout_below="@+id/password"
    android:text="@string/login" />

<Button
    android:id="@+id/cancelbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/loginbutton"
    android:layout_alignBottom="@+id/loginbutton"
    android:layout_alignRight="@+id/username"
    android:text="@string/cancel" />

Upvotes: 1

Views: 185

Answers (2)

TronicZomB
TronicZomB

Reputation: 8747

You could try the following:

lv = new ListView(this);
lv.setAdapter(aa);
lv.setId(getTaskId());
setContentView(lv);

lv.setOnItemClickListener(new  OnItemClickListener() {         
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos,
            long id) {
         TextView v=(TextView) view.findViewById(R.id.info);
         String str = (String) v.getText();
         String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
         AlertDialog.Builder b = new AlertDialog.Builder(context);
         b.setView(getLayoutInflater().inflate(R.layout.loginbox,(ViewGroup) view));
         b.setPositiveButton("Login", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                  Log.i("Button","Log in");
               }
           });
           b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   Log.i("Button","Cancel");
                   dialog.dismiss();
               }
           });

         b.show();
         Log.i("Click",ssid);
    }       
    });

And then you can delete your <Button>s from your xml. The .setPositiveButton and .setNegativeButton will create the buttons for you and there is no need to create them in the xml and reference them with R.id.

Upvotes: 0

user901309
user901309

Reputation:

         TextView v=(TextView) view.findViewById(R.id.info);
         String str = (String) v.getText();
         String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
         AlertDialog.Builder b = new AlertDialog.Builder(context);
         b.setView(getLayoutInflater().inflate(R.layout.loginbox, null, false));
         b.setPositiveButton(R.id.loginbutton, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                  Log.i("Button","Log in");
               }
           });
           b.setNegativeButton(R.id.cancelbutton, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   Log.i("Button","Cancel");
                   dialog.dismiss();
               }
           });

         AlertDialog alertDialog = b.create();
         alertDialog.show();

         Log.i("Click",ssid);

Upvotes: 1

Related Questions