Android ListView仅显示最后一项

时间:2011-09-25 16:48:15

标签: android listview

我正在使用listview开展项目,但我遇到了一些问题。我正在使用SimpleAdapter,所以我可以更改listview的视图,但它只显示最后一个元素。这是代码:

private ArrayList <HashMap<String, Object>> items;
    private final String TIME = "";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plovdiv);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ListView schedule = (ListView) findViewById(R.id.pld_schedule);
        items = new ArrayList<HashMap<String,Object>>();

        HashMap<String, Object> hm = new HashMap<String, Object>();


        hm.put(TIME, "06:00");
        items.add(hm);

        hm.put(TIME, "06:30");
        items.add(hm);

        hm.put(TIME, "07:00");
        items.add(hm);

        hm.put(TIME, "07:30");
        items.add(hm);

        hm.put(TIME, "08:00");
        items.add(hm);

        hm.put(TIME, "09:15");
        items.add(hm);

        hm.put(TIME, "10:00");
        items.add(hm);

        hm.put(TIME, "10:45");
        items.add(hm);

        hm.put(TIME, "11:30");
        items.add(hm);

        hm.put(TIME, "12:15");
        items.add(hm);

        hm.put(TIME, "13:00");
        items.add(hm);

        hm.put(TIME, "13:45");
        items.add(hm);

        hm.put(TIME, "14:30");
        items.add(hm);

        hm.put(TIME, "15:15");
        items.add(hm);

        hm.put(TIME, "16:00");
        items.add(hm);

        hm.put(TIME, "16:30");
        items.add(hm);

        hm.put(TIME, "17:00");
        items.add(hm);

        hm.put(TIME, "17:30");
        items.add(hm);

        hm.put(TIME, "18:00");
        items.add(hm);

        hm.put(TIME, "18:40");
        items.add(hm);

        hm.put(TIME, "19:30");
        items.add(hm);

        hm.put(TIME, "20:00");
        items.add(hm);

        hm.put(TIME, "20:40");
        items.add(hm);

        hm.put(TIME, "21:40");
        items.add(hm);

        hm.put(TIME, "22:30");
        items.add(hm);

        SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
                new String[]{TIME}, new int[]{ R.id.text});

        schedule.setAdapter(adapter);
        schedule.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

这是plovdiv.xml:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">

    <DigitalClock 
        android:id="@+id/clock"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center" 
        android:textSize="40dp"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"/>

    <ListView 
        android:id="@+id/pld_schedule"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/clock"
        android:cacheColorHint="#FFFFFF"  />

</RelativeLayout>

这是main_listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FFFFFF">
    <TextView 
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="45dp"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_alignParentLeft="true" />
</RelativeLayout>

因此,我得到的列表视图中只添加了最后一项:22:30。 任何想法如何解决?

3 个答案:

答案 0 :(得分:4)

每次添加时都会覆盖你的hashmap,你需要为每个条目创建一个新的hashmap:

{
    HashMap<String, Object> hm = new HashMap<String, Object>();
    hm.put(TIME, "06:00");
    items.add(hm);
}
{
    HashMap<String, Object> hm = new HashMap<String, Object>();
    hm.put(TIME, "06:30");
    items.add(hm);
}

答案 1 :(得分:1)

你的TIME变量是常量,所以你的hashMap在添加所有项目的最后只有一个项目。所以我建议你为你添加的每个项目不断更改TIME变量。

hm.put(TIME + "time1", "06:00");
        items.add(hm);

        hm.put(TIME + "time2", "06:30");
        items.add(hm);

使用上面给出的内容,你应该改变键值。

答案 2 :(得分:1)

您不能对hashmap中的每个条目使用相同的密钥..

take a look at HashMap put java doc here