listview布局操作----将item绑定到activity中

来源:互联网 发布:图片变素描软件 编辑:程序博客网 时间:2024/05/20 14:39

listview分两部分:

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);        }    }

效果如果:

0 0