Ok so I am having a issue with my spinner. Its being populated with data pulled from a webservice. The issue im having is that when the spinner is not clicked instead of showing the string for the first item in the spinner its showing an object reference for it instead.
I have looked at related question but still couldn't see what im missing, is it as simple as missing a toString reference?
Here is the code for populating the spinner
private void buildDrinkDropDown() {
List<NameValuePair> apiParams = new ArrayList<NameValuePair>(1);
apiParams.add(new BasicNameValuePair("call", "drinkList"));
bgt = new BackGroundTask(MAP_API_URL, "GET", apiParams);
try {
JSONArray drinks = bgt.execute().get();
for (int i = 0; i < drinks.length(); i++) {
JSONObject d = drinks.getJSONObject(i);
String id = d.getString(TAG_ID_DRINK);
String createdAt = d.getString(TAG_CREATED_AT);
String updatedAt = d.getString(TAG_UPDATED_AT);
String price = d.getString(TAG_PRICE);
String name = d.getString(TAG_NAME);
drinkList.add(new Drink( createdAt ,id, name, price,updatedAt ));
}
drinkField = (Spinner) findViewById(R.id.countryField);
DrinkAdapter dAdapter = new DrinkAdapter(this, android.R.layout.simple_spinner_item, drinkList);
drinkField.setAdapter(dAdapter);
drinkField.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Drink selectedDrink = drinkList.get(position);
GlobalDrinkSelected = drinkList.get(position).getId().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
Here is the code for the adapter class
package com.android.main;
import java.util.ArrayList;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class DrinkAdapter extends ArrayAdapter<Drink>
{
private Activity context;
ArrayList<Drink> data = null;
public DrinkAdapter(Activity context, int resource, ArrayList<Drink> data)
{
super(context, resource, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{ // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
return super.getView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
View row = convertView;
if(row == null)
{
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.dropdown_value_id, parent, false);
}
Drink item = data.get(position);
String test = item.getName();
Log.d("test ", test);
if(item != null)
{
TextView drinkName = (TextView) row.findViewById(R.id.item_value);
if(drinkName != null){
drinkName.setText(item.getName());
Log.d("find me ", drinkName.toString());
}
}
return row;
}
}
Here is the xml for dropdown_value_id layout thats used in the adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
If any more information is needed just shout.
Any help would be greatly apprieciated
Edit: Screenshot
I was having a similar issue, however I was using the default ArrayAdapter without extending it in a separate class. After looking into it a little bit, I found this:
The default ArrayAdapter will call .toString() on each object in the array that has been passed to the Adapter. If your DrinkAdapter does nothing more than display the name of the Drink, you can override the toString() method and be finished
Adapter:
Class Object:
And you're done, no need to make a separate class for your DrinkAdapter
My quick thought: getView() method is used to represent the row. Can you implement similar coding like getDropDownView method into getView() method also.
Copy past the content from getDropDownView method into getView() method
I didnt tried this, but I had done similar one in ListView where I did similar coding in getView() method.
I will also try to replicate this locally and post if I find anything.
Ok so i solved this by doing a little more research and looking at the answer to this question Example of custom setDropDownViewResource spinner item
I extracted what i was doing in getDropDownView and called it in both getView and getDropDownView.
here is the code
In your class define Object to show in ListView, you add example line of code below
It word for me .