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 6048733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:29:47+00:00 2026-05-23T07:29:47+00:00

The dialogue and the array displays just fine, I just want to be able

  • 0

The dialogue and the array displays just fine, I just want to be able to set the static variable from the originating class within the onClick that is in a method that is in a different class. All of the try, catch and
<?> were things that I put in at the insistence of the compiler:

public class Setter
    {
    public void myList(Context context, Class<?> thisclass, int arrayid, String choice)
        {
        return new AlertDialog.Builder(context)
        .setItems(arrayid, new OnClickListener()
            {
            @Override
            public void onClick(DialogInterface dialog, int which)
                {
                setChoice(thisclass, context, arrayid, which, choice);
                }
            })
        .create();
        }           
    public void setChoice(Class<?> thisclass, Context context, int arrayid, int which, String choice)
        {
        String[] array = context.getResources().getStringArray(arrayid);
        try
            {
            Field f = thisclass.getDeclaredField(choice);
            f.set(null, array[which]);
            }
        catch (SecurityException e)
            {
            e.printStackTrace();
            }
        catch (NoSuchFieldException e)
            {
            e.printStackTrace();
            }
        catch (IllegalArgumentException e)
            {
            e.printStackTrace();
            }
        catch (IllegalAccessException e)
            {
            e.printStackTrace();
            }
        }
    }

public class ClassA extends Activity
    {
    static String stringa;
    Setter setted = new Setter();
    ...
    public void onCreate()
        {
        super.onCreate();
        ...
        AlertDialog thinga = setted.myList(this, getclass(), R.array.thinga, stringa).show();
        ...
        }
    }

When I select an item from the list, I get this from debugger:

ClassCache.findFieldByName(Field[], String) line: 438   
Class.getDeclaredField(String) line: 666    
Setter.setChoice(Class, Context, int, int, String) line: 45 // the line with the Field

I think I’m passing it the class wrong but this is a bit out of my current depth.

I have a number of different classes each with their own static Strings. I am passing the method below the name of the String (in choice) and the context of what I had hoped was the original class that called a method that called a method that led to the code below. I was hoping I could call context.choice = something and the machine would read that as ClassA.stringa = something; how do I do that?

Briefly, I want to have a list of items that the user can choose from be the content of a dialogue, and have their selection be saved and accessible to the class that called for the creation of the dialogue. Perhaps I’m going about this all wrong but I got tired of dealing with other ‘kludges’ involving using spinners to do the same thing.

Because onClick can’t have non-final objects declared elsewhere (at least that is my understanding) I thought maybe I could get around that by calling to another method, setChoice that would store the value of whatever was chosen. I would definitively say this is a kludge and would love to be shown the light as to how you are supposed to deal with these things.

  • 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-05-23T07:29:48+00:00Added an answer on May 23, 2026 at 7:29 am

    Java does not have closures, but you can get close with anonymous inner classes.

    String output;
    public void onCreate() {
        Setter.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                output  = "selected";
            }
        });
    }
    

    See also this swing tutorial: http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

    Edit:
    In spirit of your example, this should look like this:

    public class Setter
    {
    public void setChoice(IsetString setter, String something)
        {
        setter.setString(something);
        }
    }
    
    
    class ClassA extends Activity implements setString
    {
    static String stringa;
    string polka = "dots";
    Setter setted = new Setter();
    ...
    public void onCreate()
        {
        super.onCreate();
        ...
        setted.setChoice(new IsetString() {
    
            @Override
            public void setString(String s) {
                stringa = s;
            }
        }, polka);
        ...
        }
    }
    
    interface IsetString {
        void setString(String s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to launch a dialogue from a non activity java class. Can
I want to make a dialogue box that looks similar to this in css3
I want to show a login dialogue and login error dialogue if necessary. I
I've made an alert dialogue page and I want to have the button on
I want to create a jQuery dialogue and overwrite the color setting for this
I'm trying to extract snippets of dialogue from a book text. For example, if
I want to present a modal mail dialogue like so in the iPad app:
I want to have a single button that when clicked opens a file dialogue,
I want a welcome dialogue box to appear -- but only the first time
I am creating a simple dialogue window in C#, and want to remember where

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.