2015-06-15 65 views
0

我做了我自己的ArrayAdapter,但我遇到了getView方法的问题 - 它永远不会被调用。我的Logcat只显示来自构造函数的日志,而不是来自getView。我试过改用BaseAdapter并尝试覆盖getCount,但没有运气,getView不会被调用。我确保通过的列表不是空的,但仍然没有运气。这是我的代码:在ArrayAdapter中的getView没有被调用

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

}

这里是我如何使用适配器:

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

} 

我使用的应用程序中的另一个活动类似的适配器,并且一个工作得很好。我错过了什么?任何帮助表示赞赏!

+0

您可以请在您使用适配器的地方发布代码吗? –

+0

它现在在编辑:) – jahed

+2

您的适配器为空,因为查询完成,顾名思义,在后台 – njzk2

回答

0

您正在致电spinnerPrograms.setAdapter(adapterSchools);,而adapterSchools为空。虽然看起来您将该变量设置为新的ProgramSelectionSchoolAdapter,但该代码在后台任务完成之后才会实际调用。尝试在done方法中调用setAdapter。

+0

这样做,谢谢 – jahed

相关问题