2 ssummeraw ssummeraw 于 2013.06.19 14:49 提问

在 Spinner 中使用 BaseAdapter

这个程序的任务是使用POJO object中的填充spinner。在这之前,我使用普通的字符串的值和BaseAdapter检查过。
activity group中的 activity:

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>()));
}

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>

adapter class:

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
}
}

当我点击spinner时,有下面的错误:

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

这是什么问题呢?

1个回答

Jave_
Jave_   2013.06.19 14:55

你确定你这个activity是这个group里的吗

Csdn user default icon
上传中...
上传图片
插入图片