我想用HaspMap类创建一个listview活动来插入行星,但文本没有显示。 这是代码
ListView类 package com.thenextgeneration.listviewtest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Second extends Activity{
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
initList();
listview = (ListView) findViewById(R.id.listView1);
SimpleAdapter simpleAdpt = new SimpleAdapter(this, planetsList,
android.R.layout.simple_list_item_2, new String[] {"planet"}, new
int[] {android.R.id.text2});
listview.setAdapter(simpleAdpt);
}
List<Map<String, String>> planetsList = new ArrayList<Map<String,
String>>();
private void initList() {
// TODO Auto-generated method stub
planetsList.add(createPlanet("planet", "Mercury"));
planetsList.add(createPlanet("planet", "Umaru Musa Yaradua
University"));
planetsList.add(createPlanet("planet", "Jupiter"));
planetsList.add(createPlanet("planet", "Earth"));
planetsList.add(createPlanet("planet", "Mars"));
planetsList.add(createPlanet("planet", "Venus"));
planetsList.add(createPlanet("planet", "Uranus"));
planetsList.add(createPlanet("planet", "Saturn"));
planetsList.add(createPlanet("planet", "Neptune"));
}
private HashMap<String, String> createPlanet(String key, String
name) {
// TODO Auto-generated method stub
HashMap<String, String> planet = new HashMap<String, String>();
return planet;
}
}
XML布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>
它显示列表但是有一个空文本,我已经在我的设备中安装了应用程序vut仍然只显示没有任何内容的列表
答案 0 :(得分:0)
将您的方法更改为:
private HashMap<String, String> createPlanet(String key, String
name) {
// TODO Auto-generated method stub
HashMap<String, String> planet = new HashMap<String, String>();
planet.put(key,name);
return planet;
}
答案 1 :(得分:0)
您的代码遗失了。您首先将planetsList添加到适配器,然后初始化并为planetsList赋值。首先初始化它,在其中放入值,然后发送到适配器或在代码中使用带适配器的notifydatasetchanged。
答案 2 :(得分:0)
这是你的答案,你已经命名为“android.R.id.text2”而不是“android.R.id.text1”。 我建议你按顺序编码。请享用!
ListView listview;
List<Map<String, String>> planetsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
planetsList = new ArrayList<Map<String, String>>();
initList();
listview = new ListView(this);
SimpleAdapter simpleAdpt = new SimpleAdapter(this, planetsList,
android.R.layout.simple_list_item_1, new String[] { "planet" },
new int[] { android.R.id.text1 });
listview.setAdapter(simpleAdpt);
setContentView(listview);
}
private void initList() {
Map<String, String> item;
item = new HashMap<String, String>();
item.put("planet", "Mercury");
planetsList.add(item);
item = new HashMap<String, String>();
item.put("planet", "Jupiter");
planetsList.add(item);
item = new HashMap<String, String>();
item.put("planet", "Mars");
planetsList.add(item);
}