0

Here is a simple gif of the application in action to show what I mean: Video Gif here

I have a Spinner and here is my XML code for it:

<Spinner
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/uniSpinner"
    android:layout_weight="1.5"
    android:spinnerMode="dialog"
    android:prompt="@string/type_default"/>

I have followed a few tutorials and browsed around on here to dynamically add content to the spinner using parse.com. The content is added successfully but the OnItemSelected does not fire when selecting an item on the list, the selected item also does not show in the spinner.

Code above oncreate:

Spinner uniSpinner;
List<String> uniList;

Code in oncreate:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Get the view from main.xml
    setContentView(R.layout.activity_register);

    uniSpinner = (Spinner) findViewById(R.id.uniSpinner);

    uniList = new ArrayList<String>();

    addItemsToSpinner();
    InitialSetUpUI();

Code to create the spinner:

public void addItemsToSpinner()
{
    ParseQuery<ParseObject> query = ParseQuery.getQuery("University");

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null)
            {
                for(ParseObject university : objects){
                    uniList.add(university.getString("name"));
                }
            }
            else
            {

            }
        }
    });
}

public void InitialSetUpUI()
{
    Spinner spinner1 = (Spinner) findViewById(R.id.uniSpinner);

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,uniList);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);

    spinner1.setOnItemSelectedListener(new mySpinnerListener());
}

class mySpinnerListener implements Spinner.OnItemSelectedListener
{
    @Override
    public void onItemSelected(AdapterView parent, View v, int position,long id) {
        // TODO Auto-generated method stub
        Toast.makeText(parent.getContext(), "test: " +
                parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView parent) {
        // TODO Auto-generated method stub
        // Do nothing.
    }

}

Have no idea what is wrong, have tried many different tutorials and ways of creating the same thing. Nothing has worked, maybe I am missing something simple I'm not sure! If anyone could help that would be great :)

vmetelz
  • 87
  • 10
  • Why are you creating two references for same Spinner named 'uniSpinner' and 'spinner1'? I have tried your code its working fine for me. –  Apr 05 '15 at 02:21
  • I noticed that in your done() method, you don't call notifyDataSetChanged() on the adapter. I have seen weird things happen when the ListView wasn't synced with the adapter using notifyDataSetChanged(). I would try rewriting the code so that the adapter can be accessed from the done() callback method and notifyDataSetChanged() can be called at the end of the done() method. – kris larson Apr 05 '15 at 04:07
  • @UsmanAliButt this was because I was testing out some other code and forgot to remove the previous reference. Can I ask what emulator you used to test it? Could be the possible reason – vmetelz Apr 05 '15 at 06:43
  • I had tested it on Nexus 5 device (not on emulator) –  Apr 05 '15 at 06:47
  • @krislarson could you maybe provide an example of this? I'm not too great at all this stuff yet lol – vmetelz Apr 05 '15 at 06:52
  • I simply removed 'spinner1' and used 'uniSpinner' in your code and it worked fine. Even without doing that it was OK. –  Apr 05 '15 at 06:59
  • just try it and look if there is some other issue –  Apr 05 '15 at 07:00
  • @UsmanAliButt I've done that and tried it on my Note 3 device, still the same output as the gif. I did come across this while opening the activity that contains the spinner `E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...` This have anything to do with it? – vmetelz Apr 05 '15 at 07:40
  • Try your code without spinner property 'android:prompt' or by increasing minSdkVersion in manifest.xml or may be there is some issue if you are using Holo theme for your app. –  Apr 05 '15 at 08:25
  • @UsmanAliButt I've tried that before and it didn't help, I have narrowed it down slightly. When I manually created an ArrayList and applied it to a new spinner it worked, as soon as I copy over the uniList ArrayList which I created using Parse it stops working. – vmetelz Apr 05 '15 at 08:31
  • Debug your addItemsToSpinner() method, if your spinner is populated with right values and you have set correct listeners then it should work. Right now I can only make guesses because I can't regenerate the issue for myself so man try, try again :) –  Apr 05 '15 at 08:45
  • @UsmanAliButt Finally sorted out the issue, it was due to how you get the array data from parse. I followed an example and it turned out to be the wrong way. I assume that because i'm using an adapter I need to query parse using the ParseQueryAdapter? No idea lol. Thanks for your help though! – vmetelz Apr 05 '15 at 08:52

1 Answers1

1

Finally came across this which helped me, I changed my InitialSetupUI function to this:

public void uniSpinnerSetup()
{
    ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("University");
            return query;
        }
    };

    uniSpinner = (Spinner) findViewById(R.id.uniSpinner);

    ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(this, factory);
    adapter.setTextKey("name");
    uniSpinner.setAdapter(adapter);
    uniSpinner.setSelection(1);
    uniSpinner.setOnItemSelectedListener(new mySpinnerListener());
}

Don't ask me how it works lol, but it does...now I need to figure out how to get these values out O.o

vmetelz
  • 87
  • 10