2012-07-05 92 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