【问题标题】:Custom listView with SimpleCursorAdapter使用 SimpleCursorAdapter 自定义 listView
【发布时间】:2012-07-05 23:32:29
【问题描述】:

我创建了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>

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

【问题讨论】:

    标签: java android listview simplecursoradapter


    【解决方案1】:

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

    替换这个:

    android.R.layout.simple_expandable_list_item_1
    

    有了这个:

    R.layout.game_list_entry
    

    【讨论】:

    • 谢谢 Sam,在我的智能手机上复制代码真的很痛苦。 :p。 (是的,我沉迷于 SO。想要有所作为吗?)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多