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
- listview布局操作----将item绑定到activity中
- ListView中Item多布局
- android 如何将listview的每个item中的值传到另一个Activity中
- 使用ListView界面布局、给每个item绑定监听器
- ListView中Item布局示例二
- ListView中Item布局示例一
- ListView中item的不同布局
- Android中ListView多种Item布局
- android 将Service绑定到Activity
- ListView实现下拉刷新-2-将顶部布局加载到ListView中
- ListView控件Item布局
- 如何点击listView的item跳转到指定的Activity
- ListView中Item鼠标右键操作
- 关于ListView中Item的操作
- Activity中操作xml布局
- Android中ListView的自定义Adapter监听Item中Button,实现跳转到一个新的Activity
- listview中点击item进行Item中的控件更新操作。
- listview中点击item进行Item中的控件更新操作
- asp.net 视频教程
- Thinkphp 无法使用->order() 排序的两种解决办法!
- 远程线程注入技术(3)之DLL以数据形式注入
- Fisher vector学习笔记
- 无奈很多,前进却是必须的
- listview布局操作----将item绑定到activity中
- 让iis支持下载apk,ipa文件
- 未能启用Hyper-V
- 理解对象序列化文件格式
- 测试之路3——对比XML文件2
- VS2013里面新建项目未找到Windows Phone选项
- 计算机系统:深入理解计算机系统--第三章摘抄
- bzoj1618[Usaco2008 Nov]Buying Hay 购买干草
- python3 学习-1(实现简单的服务器与客户端)