2012-04-21 38 views
1

カスタマイズされたArrayAdapterのgetViewは何らかの理由で呼び出されず、アンドロイドは空のリストを表示します。私は既に私のクエリが空でないことを確認するために、私のデータソースクラスにcursor.getCount()を挿入しました。カスタマイズされたArrayAdapterのgetViewは呼び出されません

アクティビティー:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d(TAG, "onCreate called"); 

    setContentView(R.layout.conference_list_layout); 

    Log.d(TAG, "create ConferenceDataSource"); 
    datasource = new ConferenceDataSource(this); 

    Log.d(TAG, "open ConferenceDataSource"); 
    datasource.open(); 

    Log.d(TAG, "getAllUpcomingConferences"); 
    List<Conference> conferences = datasource.getAllUpcomingConferences(); 

    Log.d(TAG, "set ConferenceArrayAdapter"); 
    setListAdapter(new ConferenceArrayAdapter(this, conferences)); 
} 

データソース:

public List<Conference> getAllUpcomingConferences() { 
    Log.d(TAG, "getAllUpcomingConferences called"); 

    List<Conference> conferences = new ArrayList<Conference>(); 

    Cursor cursor = database.query(
      DatabaseConstants.TABLE_CONFERENCES, 
      fields, 
      DatabaseConstants.CONFERENCE_START + ">" + (Calendar.getInstance()).getTimeInMillis(), 
      null, 
      null, 
      null, 
      null); 

    cursor.moveToFirst(); 

    while(!cursor.isAfterLast()) { 
     Conference conference = cursorToConference(cursor); 
     conferences.add(conference); 
     cursor.moveToNext(); 
    } 

    cursor.close(); 

    return conferences; 
} 

private Conference cursorToConference(Cursor cursor) { 
    Log.d(TAG, "cursorToConference called"); 

    Conference conference = new Conference(); 

    conference.setId(
      cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_ID))); 

    conference.setStart(
      cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_START))); 

    conference.setEnd(
      cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_END))); 

    conference.setTopic(
      cursor.getString(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_TOPIC))); 

    conference.setCreator(
      cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_CREATOR))); 

    return conference; 
} 

ArrayAdapter:

public ConferenceArrayAdapter(Context context, List<Conference> conferences) { 
    super(context, R.layout.conference_list_item); 
    Log.d(TAG, "constructor called"); 
    this.context = context; 
    this.conferences = conferences; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Log.d(TAG, "getView called"); 

    LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View view = inflator.inflate(R.layout.conference_list_item, parent, false); 

    Date conferenceDate = new Date(conferences.get(position).getStart()); 

    TextView dayOfWeek = (TextView) view.findViewById(R.id.conference_list_dayofweek); 

    dayOfWeek.setText(new SimpleDateFormat("E").format(conferenceDate)); 

    TextView date = (TextView) view.findViewById(R.id.conference_list_date); 

    date.setText(new SimpleDateFormat("dd.MM.yyyy").format(conferenceDate)); 

    TextView time = (TextView) view.findViewById(R.id.conference_list_time); 

    time.setText(new SimpleDateFormat("HH:mm").format(new Date(conferences.get(position).getStart())) + 
      new SimpleDateFormat("HH:mm").format(new Date(conferences.get(position).getEnd()))); 

    return view; 
} 

conference_list_layout.xml

<include layout="@layout/actionbar_layout" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

conference_list_item.xml

<TextView 
    android:id="@+id/conference_list_dayofweek" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" /> 

<TextView 
    android:id="@+id/conference_list_date" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/conference_list_dayofweek" /> 

<TextView 
    android:id="@+id/conference_list_time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/conference_list_date" /> 

それはエラーがXMLファイルであること、である可能性があります。私はいくつかのカスタマイズされたArrayAdaptersを使用しています。それらはすべて正常に動作し、ほぼ同じように見えます。私はwrap_contentの代わりにmatch_parentを使用することについて何かを読んだが、うまくいかなかった。

答えて

8

と呼ばれる方法を知りません。

public int getCount() { 
    return conferences.size(); 
} 
+0

これは実際にうまくいった多くのおかげで... getCount関数をオーバーライドせずに他のArrayAdaptersと連携するのはなぜですか? –

+0

@ sebbl.sche他のアダプターでは、親クラス 'ArrayAdapter'の他のスーパーコンストラクターを使用します(アダプターが' getCount'で何をするかを知っているリストからデータを表すオブジェクトのリスト/配列を取るでしょう)方法)。あなたのアダプタでは、 'Context'とリソースIDだけを取るコンストラクタを使用して、このアダプタにはデータセットがないので、' getCount'メソッドが0を返し、アダプタが空で、getView isn 'と呼ばれる)。ドキュメントで 'ArrayAdapter'クラスをチェックし、他のコンストラクタをチェックしてください。 – Luksprog

0

あなたは、アレイアダプタクラスのextends ArrayAdapterに必要 - それ以外の場合は、(あなたはおそらくArrayAdapterを拡張)getCountメソッドを実装し、Conferencesサイズのリストを返すカスタムアダプタで

+0

私はそれを拡張しました。私のクラス定義は次のようになります。パブリッククラスConferenceArrayAdapterはArrayAdapterを拡張します

関連する問題