gpt4 book ai didi

java - HashMap 中的 ListAdapter

转载 作者:行者123 更新时间:2023-12-01 19:03:55 24 4
gpt4 key购买 nike

我正在尝试获取 HashMap 的 ArrayList,以尝试获取可用的 ListAdapter。当我运行这个时,我得到一个

java.lang.RuntimeException: Your content must have a ListView whose id attribute is android.R.id.list

错误。这是我所拥有的:

gameList 是我获取数据的地方:

gameList.toString() returns: [{turn=1, opponent=UserTwo, streak=4, hintX=1234, player=UserOne, solution=MySolution, hintY=5678}, {turn=0, opponent=UserThree, streak=12, hintX=1344, player=UserOne, solution=SolutionTwo, hintY=5428}]

我的ListActivity类:

public class GameListActivity extends ListActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_list);

String GAME_ITEM = "game";
String PLAYER_ITEM = "player";
String OPPONENT_ITEM = "opponent";
String SOLUTION_ITEM = "solution";
String HINTX_ITEM = "hintX";
String HINTY_ITEM = "hintY";
String STREAK_ITEM = "streak";
String TURN_ITEM = "turn";

ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
AppVars gameVars = ((AppVars) getApplicationContext());
gameList = gameVars.getState();

String[] keyList = new String[] { OPPONENT_ITEM, STREAK_ITEM, TURN_ITEM };

ListAdapter adapter = new SimpleAdapter(this, gameList, R.layout.game_list_item,
keyList,
new int[] {R.id.opponent, R.id.streak, R.id.turn});
setListAdapter(adapter);

}

XML:

game_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
android:id="@+id/thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:src="@android:drawable/alert_dark_frame" />

<TextView
android:id="@+id/opponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_centerHorizontal="true"
android:text="Entry"
android:textSize="30sp" />

<TextView
android:id="@+id/streak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/thumbnail"
android:layout_centerHorizontal="true"
android:text="Streak"
android:textSize="20sp" />

<TextView
android:id="@+id/turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/streak"
android:layout_centerHorizontal="true"
android:text="Your turn!"
android:textSize="20sp" />

</RelativeLayout>

game_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/blackboard" >

<ListView
android:id="@+id/gameList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>

最佳答案

java.lang.RuntimeException: Your content must have a ListView whose id attribute is android.R.id.list

所以错误日志会告诉你一切

您需要添加到 ListView android.R.id.listXML 文件中

<ListView
android:id="@android:id/list"
// next attributes
/>

当您的 ActivityListActivity 扩展时,您应该使用它。

这是来自文档的信息

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

因此,有关更多信息,请查看 ListActivity .

关于java - HashMap 中的 ListAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11056063/

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