2012-07-05 91 views
1

我創建了ListView從應用程序數據庫獲取數據,並且當我只想在一個條目中顯示一個信息(如團隊名稱)時,它工作正常。使用SimpleCursorAdapter自定義列表視圖

現在我試圖顯示更多的信息,如團隊名稱,日期和城鎮(所有數據從數據庫,由光標提供)。我不能使它工作,Intent顯示ListView,但沒有任何條目中的文本......我不知道如何更清楚地解釋它。

意向代碼:

public class Games extends Activity implements OnClickListener { 

Button bNewGame; 
ListView gameList; 
GameDayTableAdapter db; 
Cursor c; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.games); 
    bNewGame = (Button) findViewById(R.id.bNewGame); 
    bNewGame.setOnClickListener(this); 
    gameList = (ListView) findViewById(R.id.lvGameList); 
    db = new GameDayTableAdapter(this); 
    db.open(); 
    c = db.getAll(); 
    startManagingCursor(c); 
    setListView(); 
} 

private void setListView() { 
    // TODO Auto-generated method stub 
    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_expandable_list_item_1, c, 
      new String[] { AbstractDbAdapter.TEAM_NAME, AbstractDbAdapter.GAME_DAY_HOME_GAME, AbstractDbAdapter.GAME_DAY_DATE }, 
      new int[] { R.id.game_entry_team, R.id.game_entry_home, R.id.game_entry_date }); 
    gameList.setClickable(true); 
    gameList.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { 

     } 
    }); 

    gameList.setAdapter(mAdapter); 
    gameList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.bNewGame: 
     Intent intent = new Intent(Games.this, NewGame.class); 
     startActivity(intent); 
     db.close(); 
     finish(); 
     break; 
    } 
} 

game_list_entry.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/game_entry_team" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/game_entry_home" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    <Space 
     android:layout_width="50dp" 
     android:layout_height="match_parent" /> 

    <TextView 
     android:id="@+id/game_entry_date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right"/> 
</LinearLayout> 

這就是我管理的基礎上一些在線提供的教程做的,但沒有,它不工作,我不知道如何解決它。我發現其他教程中人們創建的新課程擴展了SimpleCursorAdapter,但這看起來像很多額外的工作,我想知道是否有更簡單的方法來做到這一點。

回答

2

您正在設置您的列表以使用框架附帶的expandablelist行視圖。您需要指定您的自定義一個。

替換此:

android.R.layout.simple_expandable_list_item_1 

有了這個:

R.layout.game_list_entry 
+0

謝謝趙總,我的智能手機是一個真正的痛苦複製代碼。 :P。 (是的,我沉迷於SO,想要做點什麼?)。 – Barak 2012-07-05 23:41:45