2011-09-15 41 views
1

当我运行该程序时,每行中的TextView文本正在从默认文本更改为无。 HashMap中的数据不显示。ListActivity SimpleAdapter数据显示不正确

我主要的应用程序布局(main.xml中):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

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

    <TextView android:id="@android:id/empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Empty"/> 
</LinearLayout> 

我的列表项目布局(myrow.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:id="@+id/item1" 
     android:text="row_id" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"/> 

    <TextView android:id="@+id/item2" 
     android:text="row_id" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"/> 
</LinearLayout> 

我的应用程序代码(Test.java):

public class Test extends ListActivity { 

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

     //main list 
     ArrayList<HashMap<String, String>> list = 
      new ArrayList<HashMap<String, String>>(); 

     //data stored in maps and maps added to list 
     HashMap<String, String> temp = new HashMap<String, String>(); 
     temp.put("Name", "test"); 
     list.add(temp); 

     HashMap<String, String> temp2 = new HashMap<String, String>(); 
     temp2.put("Name1", "test1"); 
     list.add(temp2); 

     String[] names = {"item1", "item2"}; 
     int[] locations = {R.id.item1, R.id.item2}; 

     SimpleAdapter adapter = new SimpleAdapter 
      (this, list, R.layout.myrow, names, locations); 
     setListAdapter(adapter); 
    } 
} 

回答

1

如果我记得对,地图的关键应该是names之一,意思是:

temp.put("Name", "test"); 
... 
String[] names = {"Name", "item2"}; 

这里的全貌

//data stored in maps and maps added to list 
    HashMap<String, String> temp = new HashMap<String, String>(); 
    temp.put("Name", "test1");//first column 
    temp.put("Name1", "test1");//second column 
    list.add(temp); 

    HashMap<String, String> temp2 = new HashMap<String, String>(); 
    temp2.put("Name", "test2");//first column 
    temp2.put("Name1", "test2");//second column 
    list.add(temp2); 

    String[] names = {"Name", "Name1"}; 
    int[] locations = {R.id.item1, R.id.item2}; 
+0

好的,谢谢你了!我不记得任何说明这种关系的文件,ty。 – rico16135

+0

我想每个地图都与列表项目相关,当关系是每列。对于任何其他人都跌倒在同一个洞里。 – rico16135

+0

每张地图都与一个列表项目相关,这是真的。每个映射条目都包含正确的列(entry key = column name)。 – ernazm