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

  • Home
  • SEARCH
  • 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 8484389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:22:04+00:00 2026-06-10T20:22:04+00:00

I am working on android project and I am trying to show an AlertDialog

  • 0

I am working on android project and I am trying to show an AlertDialog which contains a CharSequence array, and when clicked, it supposed to return the string.

Below is the code I am using

String fileName = "";
        //Collect the files from the backup location
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/BoardiesPasswordManager";
        File f = new File(filePath);

        File[] files = f.listFiles();
        final CharSequence[] fileNames = new CharSequence[files.length];
        if (files.length > 0)
        {
            for (int i = 0; i < files.length; i++)
            {
                fileNames[i] = files[i].getName();
            }
        }

        String selectedFile = "";
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Choose Backup File");
        builder.setItems(fileNames, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int item) {
                fileName = fileNames[item].toString();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();

        return fileName;

As you can see, I am trying to set the fileName to be the selected item within the Array, but Eclipse keeps saying that String fileName needs to be final type, but obviously then I can’t set it to the value of the selected string. How can I set the variable so I can return the string.

Thanks for any help you can provide.

  • 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-10T20:22:06+00:00Added an answer on June 10, 2026 at 8:22 pm

    The problem here is a misunderstanding of when ‘return fileName’ will be executed. Obviously it would only be valid to execute this code after the user has made a selection. However, it will actually execute before. In fact, it will execute immediately after you call alert.show().

    Better would be to remove fileName from the scope of your function and add a function call inside your click event, something like:

    public void onClick(DialogInterface dialog, int item){
        String fileName = fileNames[item].toString();
        doSomethingWithTheFile(fileName);
    }
    

    Also, your original function would no longer returns anything, it would just set up your dialog.

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

Sidebar

Related Questions

i am working on a android project for my assignment. i am trying to
I am currently working on an android project and I am trying to create
I am working on an android project and I am using a spinner which
I am working on an Android project which relies on the unique UID of
I'm currently working on a project which involves optical character recognition in Android and
I'm working on an android project that involves native code and I'm trying to
I am working on an android project and I am trying to work out
I am working on an android project in which i need to send two
I am currently working on an android project. I am trying to have an
When I try to run my Android project(which was working fine yesterday) I get

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.