简体   繁体   English

HashMap中的ListAdapter

[英]ListAdapter from a HashMap

I'm trying to get my ArrayList of HashMaps to try to get a working ListAdapter. 我正在尝试获取我的HashMaps的ArrayList,以尝试获取有效的ListAdapter。 When I run this, I'm getting a 当我运行它时,我得到一个

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

error. 错误。 Here's what I have: 这是我所拥有的:

gameList is where I'm getting my data from: 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}]

My ListActivity class: 我的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: XML:

game_list_item.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: 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 java.lang.RuntimeException:您的内容必须具有ID属性为android.R.id.list的ListView

So error log tells you everything 因此错误日志会告诉您一切

You need to add to your XML file of ListView android.R.id.list 您需要将XML添加到ListView android.R.id.list XML文件中

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

Exactly when your Activity extends from ListActivity , you should to use this. 确切地说,当您的ActivityListActivity扩展时,您应该使用它。

Here is info from docs 这是来自文档的信息

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. ListActivity具有默认布局,该布局由位于屏幕中央的单个全屏列表组成。 However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). 但是,如果需要,可以通过使用onCreate()中的setContentView()设置自己的视图布局来自定义屏幕布局。 To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code) 为此,您自己的视图必须包含ID为“ @android:id / list”(或列表,如果在代码中,则为List)的ListView对象

So for more information have look at ListActivity . 因此,有关更多信息,请参阅ListActivity

  • Mind that When you extends ListActivity Then your xml file must have listview which id is @android:id/list . 请注意,当您extends ListActivity Then your xml file must have listview which id is @android:id/list extends ListActivity Then your xml file must have listview which id is

USe @android:id/list 使用@android:id/list

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

You are extending ListActivity , so your view should have list defined with id "list" 您正在扩展ListActivity ,因此您的视图应具有ID为“ list”的列表

See this link for working example. 有关工作示例,请参见此链接

当您使用ListActivity... ,列表的ID为“ @ + id / gameList”应该为“ @android:id/listListActivity...

Remove the following line from your code: 从您的代码中删除以下行:

setContentView(...)

ListActivity handles the ListView for you so you access the ListView using this . ListActivity为您处理ListView ,因此您可以使用this访问ListView If you need a more complex layout subclass Activity instead. 如果您需要更复杂的布局,则可以使用子类Activity代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM