Reputation: 35
I build custom arrayAdapter to show a progress bar into spinner, my xml for row be like this
<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" />
and my custom adapter
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;
}
}
I initialize it
transferSpinner.setAdapter(new CustomAdapter(getContext(),R.layout.default_spinner_progress,R.id.progressBar));
I got this error in stack trace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com..debug, PID: 9537
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
Upvotes: 0
Views: 366
Reputation: 23881
use a BaseAdapter
instead
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;
}
}
call it using: transferSpinner.setAdapter(new CustomAdapter(this));
Upvotes: 3