0

I have a spinner that needs a data source.

That data source has IDs that correspond with a unique ID in a database. I need my spinner to return the ID of the selected item as it appears in the data source.

E.G.:

╔═════════════════════════════════════════════════════╦══════════════════════════════════════════════════════════════╗
║ Data in SimpleArray<ID(An Integer),Name (A String)> ║ Data Spinner Needs<ID (An integer), TextToDisplay(a string)> ║
╠═════════════════════════════════════════════════════╬══════════════════════════════════════════════════════════════╣
║ 234, albert                                         ║ 234, albert                                                  ║
║ 1, bob                                              ║ 1, bob                                                       ║
║ 3, charly                                           ║ 3, charly                                                    ║
╚═════════════════════════════════════════════════════╩══════════════════════════════════════════════════════════════╝

Where the ID in the spinner may be out of order.

When the user selects the entry and presses a button, I want that ID returned by some function, E.G. spinner.getSelectedID();

I have seen this question: How to use SparseArray as a source for Adapter? but I would like to not create an entirely new class for this task. I would like to keep it in a method.

For refrence here is some of my current code that does not work:

public void updateDeleteSelectorItems(){
        SparseArray<String> categoryNames = timeDatabase.getColumnData("categories", 1);

        ArrayAdapter<SparseArray> spinnerAdapter = new ArrayAdapter<SparseArray>(this, android.R.layout.simple_spinner_dropdown_item, categoryNames)

        deleteSelector.setAdapter(spinnerAdapter);
    }

Is this possible and how would I go about doing this?

Thanks!

Community
  • 1
  • 1
Elliot Huffman
  • 172
  • 3
  • 18
  • 1
    The linked answers look fine. I don't understand the reluctance to create a new class. Good OOP uses encapsulation to isolate functionality in classes which makes for easier testing – David Rawson Feb 05 '17 at 00:04
  • OK, I will give it a try. I am mostly coming from a Python point of view (a lot of functions). I will give it a spin. – Elliot Huffman Feb 05 '17 at 01:07
  • Would I need to put it in its own Java file? – Elliot Huffman Feb 05 '17 at 01:32
  • 1
    I think that would be better. Sorry if the previous comment came across as a bit curt. I understand the way to do it in Python would be heaps different :) With Java, which much more verbose than Python, you'll probably find you'll end up making a lot of different class files. So when you are making your probject, you will end up getting familiar with the shortcuts in the IDE for jumping between files. On Mac you use Cmd + O to jump to another class, on PC/Linux it is Ctrl-N – David Rawson Feb 05 '17 at 01:34
  • Can you provide the answer as the proper class(es) in their respective files? Thanks! – Elliot Huffman Feb 05 '17 at 02:15
  • 1
    Never mind about making an answer like that. I figured it out based upon your input. It works great. I just needed to make two class files with your suggested input and it appears to be working now :-) thanks! – Elliot Huffman Feb 05 '17 at 19:53

0 Answers0