gpt4 book ai didi

java - 使用 SimpleCursorAdapter 自定义 listView

转载 作者:行者123 更新时间:2023-11-29 22:03:35 25 4
gpt4 key购买 nike

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

现在我正在尝试显示更多信息,例如团队名称、日期和城镇(所有数据都来自数据库,由 Cursor 提供)。而且我无法让它工作,Intent 正在显示 ListView 但任何条目中都没有文本......我不知道如何更清楚地解释它。

Intent 代码:

 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 的新类,但这看起来像是很多额外的工作,我想知道是否有更简单的方法来做到这一点。

最佳答案

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

替换这个:

android.R.layout.simple_expandable_list_item_1

有了这个:

R.layout.game_list_entry

关于java - 使用 SimpleCursorAdapter 自定义 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11353960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com