Android ListView shows only last item
I'm working on project with listview,but I have a little problem. I'm using SimpleAdapter so I can change the view of my listview,but it shows only the last element.Here is the code :
pri开发者_Python百科vate 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);
}
Here is 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>
And this is 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>
As a result I get a listview with only the last item added : 22:30
.
Any ideas how to fix that?
You keep overwriting your hashmap every time you add it, you need to make a new hashmap for each entry:
{
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);
}
etc.
you can not use the same key for every entry in your hashmap..
take a look at HashMap put java doc here
your TIME variable is constant, so your hashMap will finally have only 1 item at the end of adding all items. so i suggest you to keep changing the TIME variable, for every item you add.
hm.put(TIME + "time1", "06:00");
items.add(hm);
hm.put(TIME + "time2", "06:30");
items.add(hm);
use something like given above, you should change the key value.
精彩评论