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 sho
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
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 initView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent) {
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;
}
}
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.
In your class define Object to show in ListView, you add example line of code below
@Override
public String toString() {
return getComment();
}
It word for me .
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:
However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.
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:
ArrayAdapter<Drink> drinkAdapter = new ArrayAdapter<Drink>(getActivity(), android.R.layout.simple_spinner_dropdown_item, drinks);
Class Object:
public Drink {
String name;
// Constructor, getters, and setters for the object here
@Override
public String toString() {
return getName(); // You can add anything else like maybe getDrinkType()
}
}
And you're done, no need to make a separate class for your DrinkAdapter