集合不会显示在ListView上

时间:2014-10-15 08:03:42

标签: android listview hashmap

我在android中创建一个应用程序,它接受数据收集并将集合显示到列表视图中。我完成了代码,但视图中没有显示任何内容。这是代码:

public class Activity extends ListActivity {

protected  ArrayList<HashMap<String,String>> gameCollection = new ArrayList<HashMap<String, String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Populate view with data
    gameCollection.add(displayVideoGame("PlayStation","FIFA 14"));
    gameCollection.add(displayVideoGame("PlayStation","Thief"));
    gameCollection.add(displayVideoGame("PlayStation","Watch Dogs"));
    gameCollection.add(displayVideoGame("PlayStation","Battlefield 4"));
    gameCollection.add(displayVideoGame("PlayStation","Second Sun"));
    gameCollection.add(displayVideoGame("PlayStation","Mario Brothers"));
    gameCollection.add(displayVideoGame("PlayStation","Don't Starve"));
    gameCollection.add(displayVideoGame("PlayStation","Elder Scrolls Online Beta"));


    //Calling the Simple Adapter calling gameCollection List and setting the predefined layout
    SimpleAdapter adapter = new SimpleAdapter(this,this.gameCollection,android.R.layout.simple_list_item_1, new String[] {"Playstation"},new int[] {android.R.id.text1});
    //set adapter to collection
    setListAdapter(adapter);
}

//Create Private Method - Returns HashMap with key-value pairs
private HashMap<String, String> displayVideoGame(String key, String value)
{
    HashMap<String, String> videoGameHashMap = new HashMap<String, String>();
    videoGameHashMap.put(key,  value);
    return videoGameHashMap;

}

0 个答案:

没有答案