標題:
Android App - Simple Adapter
[打印本頁]
作者:
admin
時間:
2013-9-9 22:44
標題:
Android App - Simple Adapter
ListView 採用Simple Adapter, 一行顯示一項資料。
在主程式 testActivity.java 內有以下相關程式碼
public class testActivity extends Activity {
ArrayList<HashMap<String, Object>> listItem6 = new ArrayList<HashMap<String, Object>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv3 = (ListView)findViewById(R.id.ListView1);
listItem5 = new ArrayList<String>();
final SimpleAdapter listItemAdapter3 = new SimpleAdapter
(this,listItem6,R.layout.list_items,
new String[] {"ItemTitle"},
new int[] {R.id.ItemTitle} );
lv3.setAdapter(listItemAdapter3);
lv3.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
String s1= listItem6.get(position).values().toString();
String s2= copystring.substring(1,s1.length()-1);
//remove two char: [ and ]
}
});
listItem6.clear();
// 加一行資料
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemTitle",s2);
listItem6.add(map);
listItemAdapter3.notifyDataSetChanged();
// 改一行資料
HashMap<String, Object> ro = new HashMap<String, Object>();
ro.put("ItemTitle",s2);
listItem6.set(position, ro);
listItemAdapter3.notifyDataSetChanged();
};}
複製代碼
在 main.xlm 內有 ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
<ListView
android:id="@+id/ListView1"
android:layout_width="fill_parent"
android:layout_height="59dp"
android:layout_weight="2.0"
android:transcriptMode="alwaysScroll" >
</ListView>
</LinearLayout>
複製代碼
建立 list_items.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<TextView
android:id="@+id/ItemTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="26dp"
android:text="TextView01"
android:textSize="20dip"
android:textColor="#FFFFFF"/>
</RelativeLayout>
複製代碼
歡迎光臨 How2Do (http://forum.how2do.com.hk/)
Powered by Discuz! 7.2