I've made my own ArrayAdapter but I'm having issues with the getView method - it's never being called. My Logcat is only showing the log from the constructor and never from getView. I've tried changing to a BaseAdapter and tried overriding getCount but with no luck, getView won't get called. I've ensured that the list that's passed in is not empty, but still no luck. This is my code:
public class ProgramSelectionSchoolAdapter extends ArrayAdapter<String> {
private ArrayList<String> schools;
private Activity context;
private static final String TAG = "ProgSelSchoolAdapter";
public ProgramSelectionSchoolAdapter(Activity context, ArrayList<String> schools) {
super(context, R.layout.spinner_program_selection_row, schools);
this.schools = schools;
this.context = context;
Log.v(TAG, "ProgramSelectionSchoolAdapter was created");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
Log.v(TAG, "getView was called");
ViewHolder holder;
if (rowView == null) {
holder = new ViewHolder();
// Inflate the row layout
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.spinner_program_selection_row, parent, false);
// Use of view holder to save performance
holder.spinnerItem = (TextView) rowView.findViewById(R.id.tvSpinnerItem);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
// Set the text for the view holder
String school = schools.get(position);
holder.spinnerItem.setText(school);
Log.v(TAG, school + " added to spinnerItem");
return rowView;
}
static class ViewHolder {
public TextView spinnerItem;
}
}
And here is how I use the adapter:
private void setUpSpinner() {
// Construct a list to be used for storing unique school values for the adapter
schools = new ArrayList<String>();
// Query for all organizers
ParseQuery<Organizer> query = ParseQuery.getQuery(Organizer.class);
query.whereEqualTo(COLUMN_APP, APP_NAME);
query.orderByAscending(COLUMN_SCHOOL);
query.findInBackground(new FindCallback<Organizer>() {
@Override
public void done(List<Organizer> organizers, ParseException e) {
if(e == null) {
// All organizers for the chosen app was received successfully
Log.v(TAG, "Organizers received successfully");
// Go through all organizers and look for unique school values
for (Organizer organizer : organizers) {
String school = organizer.getSchool();
if(!schools.contains(school)) {
// Add the unique school to the ArrayList
schools.add(school);
Log.v(TAG, school + " added to list");
}
}
// Create the adapter for the schools
adapterSchools = new ProgramSelectionSchoolAdapter(ProgramSelection.this, schools);
} else {
// Something went wrong
e.printStackTrace();
}
}
});
// Create spinner and set adapter
spinnerPrograms = (Spinner) findViewById(R.id.spinnerPrograms);
spinnerPrograms.setAdapter(adapterSchools);
}
I'm using a similar adapter for another activity in the application, and that one works just fine. What am I missing? Any help is appreciated!
You are calling
spinnerPrograms.setAdapter(adapterSchools);
whileadapterSchools
is null. Although it looks like you are setting the variable to a new ProgramSelectionSchoolAdapter, that code is not actually called until after the background task completes. Try calling setAdapter in thedone
method.