Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8056051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:35:30+00:00 2026-06-05T08:35:30+00:00

I am trying to make a reusable UI class. I admit that I don’t

  • 0

I am trying to make a reusable UI class. I admit that I don’t understand how it should work. I want to have a class that I pass arguments into and when the user makes a selection it will return the selected value. This will actually be a more complex, custom dialog that will be used. For testing, I put together the following code from examples I found and it does everything but return the selected value back.

So, how can I get the user selected value back in the main routine?

Main module
package com.mine.zd;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class ZDialogActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0){
                go();
            }
        });
    }

    private void go()
    {
       final String[] vOptions = { "One", "Two", "Three" } ;

       myOptions.getmenuOptions(
        ZDialogActivity.this, "Select Mode", vOptions,
            new android.content.DialogInterface.OnClickListener(){
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Log.w("TEST", "which=" + which );
               }
            }
       );
    }
}

called module

package com.mine.zd;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;

public class myOptions {

    public static void getmenuOptions(Context context, String msg,
         String[] vOptions, OnClickListener neutralClickListener){

        new AlertDialog.Builder(context)
        .setTitle("Select Mode")
        .setItems(vOptions, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {              
                Log.w("TEST", "item=" + whichButton );
                dialog.dismiss();
            }
        })
        .setNeutralButton("Cancel", neutralClickListener )
        .create().show();
    }
}

I do get the returned “cancel” button value of -3, but I need the id of the option selected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:35:32+00:00Added an answer on June 5, 2026 at 8:35 am

    Here how about this:

    import android.R;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    
    import com.your.package.MyOptions.MyDialogClickListener;
    
    public class ZDialogActivity extends Activity implements View.OnClickListener, MyDialogClickListener {
    
        private MyOptions options;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            findViewById(R.id.btn1).setOnClickListener(this);
    
            String[] vOptions = { "One", "Two", "Three" };
            options = new MyOptions(vOptions, this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btn1:
                go();
                break;
            default:
                Log.e("TEST", "Unhandled onClick");
                break;
            }
        }
    
        private void go() {
            options.getMenuOptions(this, "Select Mode");
        }
    
        @Override
        public void onMyDialogClick(String option){
            Log.d("TEST", "item=" + option);
        }
    }
    

    And your helper class:

    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    
    public class MyOptions implements OnClickListener {
    
        private final MyDialogClickListener listener;
        private final String[] options;
    
        public interface MyDialogClickListener {
            void onMyDialogClick(String option);
        }
    
        public MyOptions(String[] options, MyDialogClickListener listener) {
            this.options = options;
            this.listener = listener;
        }
    
        public void getMenuOptions(Context context, String msg)  {
            new AlertDialog.Builder(context)
                .setTitle(msg)
                .setItems(options, this)
                .setNeutralButton("Cancel", this)
                .create()
                .show();
        }
    
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(listener != null)
                listener.onMyDialogClick(options[which]);
            dialog.dismiss();
        }
    }
    

    1) Start your class names with uppercase

    2) Dont use a static helper method, instantiate the class it’s better practice in this scenario

    3) You can then pass the non changing fields to the constructor

    4) Use an interface to send message between classes (your Activity acts as a Listener on the dialog onClick event)

    5) Don’t use annoyomous onClick listeners let your class implement it and switch on the view id to pertain which was clicked

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make Http connections made by C# HttpClient helper class reusable.
I am trying to create a Validation in a reusable fashion. Purpose: Make the
I'm trying make an entity with doctrine that has three associations with other entities
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I'm trying to make the code below reusable. I need multiple toggle buttons in
I am trying to make a reusable header. Here is my XML. <?xml version=1.0
I'm trying to make a simple accordion style menu using divs: <div class=subdomainLevel> Subdomain
I am trying make a clock. The hour is a string. I want to
What I am trying to do is create a bit of reusable code that
I'm trying to make a reusable target in my MSBuild file so I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.