listview布局操作----将item绑定到activity中
2014-06-02 22:49
267 查看
listview分两部分:
1.activity
2.item
将item通过setAdapter绑定到activity。
activity布局如下:
item布局
Java代码activity代码如下:
效果如果:
1.activity
2.item
将item通过setAdapter绑定到activity。
activity布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/name" /> <TextView android:layout_width="150dp" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/phonenum" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/amount" /> </LinearLayout> <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listView" /> </LinearLayout>
item布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="80dp" android:textSize="25sp" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:layout_width="200dp" android:textSize="25sp" android:layout_height="wrap_content" android:id="@+id/phonenum" /> <TextView android:layout_width="fill_parent" android:textSize="25sp" android:layout_height="wrap_content" android:id="@+id/amount" /> </LinearLayout>
Java代码activity代码如下:
package com.example.database; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.example.databaseService.contacts; import com.example.databaseService.dataOperation; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.widget.ListView; import android.widget.SimpleAdapter; import android.database.Cursor; import android.os.Bundle; public class MainActivity extends ActionBarActivity { ListView listview; dataOperation Op; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); copyDataBaseToPhone(); Op=new dataOperation(this.getApplicationContext()); listview=(ListView)this.findViewById(R.id.listView); show(); } private void copyDataBaseToPhone() { DataBaseUtil util = new DataBaseUtil(this); // 判断数据库是否存在 boolean dbExist = util.checkDataBase(); if (dbExist) { Log.i("tag", "The database is exist."); } else {// 不存在就把raw里的数据库写入手机 try { util.copyDataBase(); } catch (Exception e) { Log.e("mytag","Error copying database"); } } } 因为apk文件不带数据库.db文件,因此必须在raw目录下放入数据库文件,在程序启动时将该数据文件放复制到程序对于的目录下 private void show2() { Cursor cursor=Op.getcursorScrollData(0, 20); //SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"name","phonenum","amount"}, // new int[]{R.id.name,R.id.phonenum,R.id.amount},0); 这个方法google已经废弃了,新给的方法多了一个flag ,没搞清这个flag标志,由于google网站目前打不开,无法查看API文档,后续再研究 // listview.setAdapter(adapter); } private void show() { List<contacts> cts= Op.getScrollData(0, 16); Log.e("mytag","hello.hello22222\n"); List<HashMap<String,Object>> data= new ArrayList<HashMap<String,Object>>(); for(contacts ct : cts) { HashMap<String,Object> item = new HashMap<String,Object>(); item.put("name", ct.getName()); item.put("phonenum",ct.getphonenum()); item.put("amount", ct.getAmount()); data.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name","phonenum","amount"}, new int[]{R.id.name,R.id.phonenum,R.id.amount}); listview.setAdapter(adapter); } }
效果如果:
相关文章推荐
- 使用ListView界面布局、给每个item绑定监听器
- wpf listview的数据绑定(dataset)与selectedItem的数据
- listview不同的项目布局(item layout)
- Android之ListActivity:布局与多种数据绑定
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- android ListView布局之二(是用simpleAdapter绑定数据)
- Android之ListActivity(一):布局与数据绑定
- android listview item 加载不同的布局样式文件
- Android之ListActivity(一):布局与数据绑定
- Android之ListActivity:布局与数据绑定
- listView中多个listItem布局时,convertView缓存及使用
- android ListView布局之二(是用simpleAdapter绑定数据)
- Android之ListActivity:布局与数据绑定
- ListView 自定义item布局 android.widget.LinearLayout$LayoutParams
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(二)
- ListView项(Item)的三种布局使用例子
- Android之ListActivity:布局与数据绑定
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(一)
- ListView 自定义item布局 android.widget.LinearLayout$LayoutParams
- listView中多个listItem布局时 convertView缓存及使用(转)