1

我已经创建了ListView从应用程序数据库获取数据,并且当我只想在一个条目中显示一个信息(例如团队名称)时,它工作正常。

现在我正在尝试显示更多信息,例如团队名称、日期和城镇(所有数据来自 db,由 Cursor 提供)。而且我无法让它工作,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>

这就是我根据一些在线可用的教程设法做到的,但不是,它不起作用,我不知道如何修复它。我发现了其他教程,人们在其中创建了 extends 的新类SimpleCursorAdapter,但这看起来需要做很多额外的工作,我想知道是否有更简单的方法来做到这一点。

4

1 回答 1

2

您正在将列表设置为使用框架附带的可扩展列表行视图。您需要指定您的自定义。

替换这个:

android.R.layout.simple_expandable_list_item_1

有了这个:

R.layout.game_list_entry
于 2012-07-05T23:36:42.437 回答