Renjith
Renjith

Reputation: 3617

Use BaseAdapter for Spinner

The mission is to fill the spinner with values from a POJO object. Before doing that, I was checking with normal String values and BaseAdapter.

I have an activity in an activity group.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date_filter);

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

    startMonth.setAdapter(new CustomSpinnerAdapter(getParent(), new ArrayList<String>()));
}

And the layout_spinner_row.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/spinnerValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

And my adapter class is:

public class CustomSpinnerAdapter extends BaseAdapter{ 

private List<String> alertList;
private LayoutInflater mInflater;

public CustomSpinnerAdapter(Context context, Object results) {
    alertList = (List<String>) results;
    //just check if it works. 
   //in real, actual object fits in
   alertList.add("January");
    alertList.add("Feburary");
    alertList.add("March");
    mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return alertList.size();
}

@Override
public Object getItem(int position) {
    return alertList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.layout_spinner_row, null);
        holder = new ViewHolder();
        holder.spinnerValue = (TextView) convertView.findViewById(R.id.spinnerValue);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.spinnerValue.setText(alertList.get(position));
    return convertView;
}




static class ViewHolder {
    TextView spinnerValue; //spinner name
}
}

And when I click on the spinner, I get the following error:

E/AndroidRuntime(1686): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4135fa38 is not valid; is your activity running?

I could not find any valid answer for the issue. Any thoughts?

Thanks in advance!

Upvotes: 2

Views: 9946

Answers (2)

Paresh Mayani
Paresh Mayani

Reputation: 128428

From your adapter code, i can say you haven't initialized LayoutInflater and without doing it you are trying to inflate layout. So inside your constructor, use:

 this.mInflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Upvotes: 1

Greezer
Greezer

Reputation: 515

You need to set your view to corresponding view objects, in order to get the spinner working. To do you will need to change the oncreate of your activity:

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_date_filter, null); 
this.setContentView(viewToLoad); 

For complete explanation: http://developer.android.com/reference/android/view/LayoutInflater.html

Upvotes: 3

Related Questions