这里介绍简单适配器和ListView控件的结合使用。
一、建立工程,如图
一、Activity_main.xml中代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/pname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:textSize="15sp" android:text="产品名称" /> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:text="产品价格" android:textSize="15sp" /> <TextView android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:text="产品产地" android:textSize="15sp" /> </LinearLayout> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview" ></ListView> </LinearLayout>
二、MyDataSource.java中代码
package com.study.listview2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MyDataSource { public static List<Map<String, String>> getDataSource(){ List<Map<String, String>> data = new ArrayList<Map<String,String>>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("pname", "西瓜"); map1.put("price", "¥2.3"); map1.put("address", "海南"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("pname", "香蕉"); map2.put("price", "¥6.3"); map2.put("address", "广西"); Map<String, String> map3 = new HashMap<String, String>(); map3.put("pname", "苹果"); map3.put("price", "¥5.1"); map3.put("address", "贵州"); data.add(map1); data.add(map2); data.add(map3); return data; } }
三、MainActivity.java中代码
package com.study.listview2; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView listView; private SimpleAdapter adapter; private List<Map<String, String>> data = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)this.findViewById(R.id.listview); data = MyDataSource.getDataSource(); adapter = new SimpleAdapter(MainActivity.this, data, R.layout.activity_main, new String[]{"pname","price","address"}, new int[]{R.id.pname,R.id.price,R.id.address}); listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
四、效果图