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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:50:58+00:00 2026-05-26T16:50:58+00:00

Currently I am able to populate my spinner two ways one way gives me

  • 0

Currently I am able to populate my spinner two ways one way gives me results from the database which is what I need but the text in the spinner is in JSON format, it all works but it looks bad, then if I extract the name from the JSON and use it I lose the value part of the name value pair.

I’ve been informed that I need to use a BaseAdapter subclass to be able to do what I need to do. The code below works just like I would love it to but the data is hard coded in, which is no use.

What I want to do is fill MyData below with the JSON data returned from the database.

This code:

final MyData items[] = new MyData[4];
items[0] = new MyData( "Ken's Plimbing","125738468");
items[1] = new MyData( "Peninsula Pests","3787906453");
items[2] = new MyData( "Joe's Electrical","129754354");
items[3] = new MyData( "Garderning Supplies","097803452");*/

ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(PropertyManagement.this, android.R.layout.simple_spinner_item, items );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
userSpinner.setAdapter(adapter);

userSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            MyData d = items[position];
            Toast.makeText(PropertyManagement.this, d.getValue(), Toast.LENGTH_SHORT).show();
        }
            public void onNothingSelected(AdapterView<?> parent) {
        }
    }
);

With this BaseAdapter:

class MyData {
    public MyData( String spinnerText, String value ) {
        this.spinnerText = spinnerText;
        this.value = value;
    }
    public String getSpinnerText() {
        return spinnerText;
    }
    public String getValue() {
        return value;
    }
    public String toString() {
        return spinnerText;
    }
    String spinnerText;
    String value;

Works!

But I need to fill MyData with the JSON array returned from the database. I have been doing that with the following as per the first paragraph in this post.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(PropertyManagement.this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

final MyData items[] = new MyData[4];
for (int i = 0; i < myUsers.length(); ++i)
{
    adapter.add(myUsers.getJSONObject(i).getString("BusinessName")); 
};
userSpinner.setAdapter(adapter);

The JSONArray/string looks like this,

{"BusinessName":"Petes Plumbing","BusinessPhone":"0434943743"},{"BusinessName":"Joes Electrical","BusinessPhone":"0466367279"}

Any help would be greatly appreciated.

Cheers,

Mike.

  • 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-26T16:50:59+00:00Added an answer on May 26, 2026 at 4:50 pm

    You are super close here, instead of adapter.add in your for loop you need to add it to items. Refactor like this:

    final MyData items[] = new MyData[myUsers.length()];
    for (int i = 0; i < MyData.length(); ++i){
      items[i] = new MyData(myUsers.getJSONObject(i).getString("BusinessName"), myUsers.getJSONObject(i).getString("BusinessPhone"));
    }
    ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(PropertyManagement.this, android.R.layout.simple_spinner_item, items );
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    userSpinner.setAdapter(adapter);
    

    What is different here is your finding the length of your JSONArray, creating a new array of MyItem of that size. Then you initialize the items with new MyItems based on values from your JSONArray. Finally, you are creating the adapter with items just like you did in the first example. I’m assuming in this example that myUsers in a JSONArray.

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

Sidebar

Related Questions

Currently, I'm finding a lib able to stream video from multiple sources through one
Currently, I need to be able to retrieve values from an SQL Server DB,
Currently I am able to retrieve data from a MySQL db by using HTTP
I am currently developing an application and I need to be able when pressing
I am currently writing a Employee store and I need to be able to
I am currently developing a Blackberry 5.0 application which needs to be able to
Currently, I have: outByte.writeInt(0x49492a00); outByte.writeInt(0x08000000); But i want to be able to write all
I'm currently looking for a way to provide the user with being able to
I am retrieving data from a database and trying to populate a List<>. That
Currently I have a main form, from which I call a popup that I've

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.