标签:
主Activity布局,只需要加入一个ListView控件,特别要注意各个控件的layout_width和layout_height的设定
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <LinearLayout 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:orientation="horizontal"> 11 <TextView 12 android:text="@string/name" 13 android:layout_width="1dp" 14 android:layout_height="wrap_content" 15 android:layout_weight="3"/> 16 <TextView 17 android:text="@string/phone" 18 android:layout_width="1dp" 19 android:layout_height="wrap_content" 20 android:layout_weight="5"/> 21 <TextView 22 android:text="@string/account" 23 android:layout_width="1dp" 24 android:layout_height="wrap_content" 25 android:layout_weight="2"/> 26 </LinearLayout> 27 28 <ListView 29 android:id="@+id/listView" 30 android:layout_width="fill_parent" 31 android:layout_height="fill_parent"> 32 33 </ListView> 34 35 </LinearLayout>
示意图
条目XML布局
<?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="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="1dp" android:layout_height="wrap_content" android:layout_weight="3"/> <TextView android:id="@+id/phone" android:layout_width="1dp" android:layout_height="wrap_content" android:layout_weight="5"/> <TextView android:id="@+id/account" android:layout_width="1dp" android:layout_height="wrap_content" android:layout_weight="2"/> </LinearLayout>
private void showListView(Uri uri) { ContentResolver resolver = MainActivity.this.getContentResolver(); cursor = resolver.query(uri, null, null, null, "_id desc limit 0,20"); //给listView绑定适配器 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"name", "phone", "account"}, new int[]{R.id.name, R.id.phone, R.id.account}, 1); listView.setAdapter(adapter); }
private void otherShowListView(Uri uri) { ContentResolver resolver = MainActivity.this.getContentResolver(); cursor = resolver.query(uri, null, null, null, "_id desc limit 0,50"); //数据存储形式为List< ?extendsMap< String,? > > data; List< HashMap<String,Object> > data = new ArrayList<HashMap<String,Object> >(); while(cursor.moveToNext()){ HashMap<String, Object> item = new HashMap<String, Object>(); item.put("name", cursor.getString(cursor.getColumnIndex("name"))); item.put("phone", cursor.getString(cursor.getColumnIndex("phone"))); item.put("account", cursor.getString(cursor.getColumnIndex("account"))); data.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name", "phone", "account"}, new int[]{R.id.name, R.id.phone, R.id.account}); listView.setAdapter(adapter); }
1 private void showListView3(){ 2 final List<Person> dataList = pService.getScrollDate1(0, 30); 3 listView.setAdapter(new BaseAdapter() { 4 @Override 5 public View getView(int position, View convertView, ViewGroup parent) { 6 ViewCache vCache; 7 //如果该View控件为空,初始化 8 if(convertView == null){ 9 convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null); 10 vCache = new ViewCache(); 11 /** 12 * 每次findView会降低性能,所以希望只在第一次创建View对象时执行find操作 13 */ 14 vCache.name = (TextView) convertView.findViewById(R.id.name); 15 vCache.phone = (TextView) convertView.findViewById(R.id.phone); 16 vCache.account = (TextView) convertView.findViewById(R.id.account); 17 convertView.setTag(vCache); 18 } 19 vCache = (ViewCache) convertView.getTag(); 20 vCache.name.setText(dataList.get(position).getName()); 21 vCache.phone.setText(dataList.get(position).getPhone()); 22 vCache.account.setText(dataList.get(position).getAccount()+""); 23 24 return convertView; 25 } 26 @Override 27 public long getItemId(int position) { 28 return position; 29 } 30 @Override 31 public Object getItem(int position) { 32 return dataList.get(position); 33 } 34 35 @Override 36 public int getCount() { 37 return dataList.size(); 38 } 39 }); 40 }
标签:
原文地址:http://www.cnblogs.com/lya-nju/p/4228955.html