问题
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 :)
回答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
来源:https://stackoverflow.com/questions/29453267/android-spinner-will-not-launch-onitemselected-and-current-selected-item-is-not