我构建自定义arrayAdapter以向spinner显示进度条,我的xml for row是这样的
<ProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
和我的自定义适配器
public class CustomAdapter extends ArrayAdapter
{
private Context mContext;
public CustomAdapter(@NonNull Context context, int resource,int textViewResourceId) {
super(context, resource,textViewResourceId);
mContext = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View row = inflater.inflate(R.layout.default_spinner_progress,parent,false);
return row;
}
@Override
public int getCount() {
return 1;
}
}
我初始化它
transferSpinner.setAdapter(new CustomAdapter(getContext(),R.layout.default_spinner_progress,R.id.progressBar));
我在堆栈跟踪中遇到此错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com..debug, PID: 9537
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
答案 0 :(得分:3)
使用BaseAdapter
代替
public class CustomAdapter extends BaseAdapter {
private Context mContext;
public CustomAdapter(@NonNull Context context) {
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.default_spinner_progress, parent, false);
}
return view;
}
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
使用:transferSpinner.setAdapter(new CustomAdapter(this));