MainActivity.java :
package com.main; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.example.sample_listview.R; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { ListView lv; SimpleAdapter sa; ArrayList<Map<String, Object>> mapArray = new ArrayList<Map<String, Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.main); //initialize the list view lv = (ListView) this.findViewById(R.id.listView1); sa = new SimpleAdapter(this, mapArray, R.layout.main_list, new String[]{"DisplayString"}, new int[]{R.id.textView1}); lv.setAdapter(sa); lv.setStackFromBottom(true);//display from bottom //generate the test string array String[] testString = new String[5000]; for(int i=0;i<testString.length;i++){ testString[i] = "test string "+i; } //put string in the map of list for(int i=0;i<testString.length;i++){ Map<String, Object> map = new HashMap<String, Object>(); map.put("DisplayString", testString[i]); mapArray.add(map); } } }main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>main_list.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TestString" android:textSize="30sp" /> </LinearLayout>
沒有留言 :
張貼留言