官方NotePad实例学习--ListActivity的使用
来源:互联网 发布:ip网络广播系统设计厂 编辑:程序博客网 时间:2024/05/22 13:26
ListActivity用来显示一个项目列表,数据来源可以是数组或者游标。
先使用xml定义一个屏幕布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"><ListView android:id="@id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_notes"/> </LinearLayout>
- ListView的id必须是 @id/android:list 或者 @android:id/list 。
- TextView用来显示没有数据时的信息,id是 @id/android:empty或者"@android:id/empty"。
下面是每行的布局:
<?xml version="1.0" encoding="utf-8"?><TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" />
- 可以自己写,如上,也可以用android已经定义好的布局,比如:android.R.layout.simple_list_item_1。
- 要设置id,数据绑定的时候会用到。
- 可以是多个TextView,数据绑定的时候对应就可以。
数据绑定的代码:
package com.hebaijun.notepad;import android.app.ListActivity;import android.database.Cursor;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleCursorAdapter;import android.widget.TextView;public class NotepadActivity extends ListActivity {private static final String TAG = "NotepadActivity";private NoteDbAdapter mDbHelper;private Cursor mNotesCursor; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notepad_list); mDbHelper = new NoteDbAdapter(this); mDbHelper.open(); fillData(); //fillDataArray(); } /** * 数组方式添加List数据 */ private void fillDataArray() {final String[] DATA = new String[] {"a笔记一","b笔记二","c笔记三"};ArrayAdapter<String> notes = new ArrayAdapter<String>(this, R.layout.notes_row, DATA);setListAdapter(notes);ListView lv = getListView(); //根据键盘输入的字符在List中筛选,比如按键盘的a,会把a开头的item筛选出来显示。lv.setTextFilterEnabled(true);lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String textString = ((TextView)view).getText().toString();Log.v(TAG, DATA[position]);Log.v(TAG, textString); } });}/** * 为list添加数据,光标方式 */private void fillData() {mNotesCursor = mDbHelper.fetchAllNotes();//使用这个方法的目的是把获取的Cursor对象交给Activity管理,这样Cursor的生命周期便能和Activity自动同步,省去自己手动管理。 startManagingCursor(mNotesCursor); String[] from = new String[] { NoteDbAdapter.KEY_TITLE }; int[] to = new int[] { R.id.text1 }; SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to); setListAdapter(notes);}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);Log.v(TAG, "position:"+position+",id:"+id);Cursor c = mNotesCursor; c.moveToPosition(position);Log.v(TAG, c.getString(c.getColumnIndexOrThrow(NoteDbAdapter.KEY_TITLE)));}}
- fillData方法是光标方式,fillDataArray方法是数组方式。
- 响应每个item的点击有两种方式,一个是 setOnItemClickListener,一个是实现 onListItemClick方法。
- onListItemClick中的l是ListView,v是TextView,position是列表的位置,从0开始,id对应的光标中row的id,在数组中id是数组的下标。
- 官方NotePad实例学习--ListActivity的使用
- 官方NotePad实例学习--Intent的使用
- 官方NotePad实例学习--读取数据Cursor的使用
- 官方NotePad实例学习--标准菜单onCreateOptionsMenu的使用
- 官方NotePad实例学习--上下文菜单ContextMenu的使用
- 官方NotePad实例学习--标准菜单onCreateOptionsMenu的使用
- 官方NotePad实例学习--SQLiteOpenHelper的用法
- 官方NotePad实例学习--显式的Intent实例
- 官方NotePad实例学习--SQLiteDatabase类的常用方法
- 官方NotePad实例学习--保存数据ContentValue
- android的listactivity实例
- 官方NotePad实例学习--带附加信息并且有返回值的显式Intent
- ListActivity使用的一些学习笔记
- ListActivity的使用
- ListActivity的使用介绍
- ListActivity的使用
- ListActivity的使用
- ListActivity的使用
- 全球智能手机使用差异汇总:13个不可思议的事实
- Linux 编译安装 mongoDB
- Oracle中查找和删除重复记录方法简介
- 数据驱动组件
- HDUOJ Minimum Inversion Number
- 官方NotePad实例学习--ListActivity的使用
- tableView的一些函数
- 18.windbg-临界区leak案例
- 浅谈系统设计中的抽象和具体
- Php设计模式之工厂模式(一)简单工厂模式
- stackless python的疑问
- nginx:expires控制页面缓存以及加头
- 仅通过崩溃地址找出源代码的出错行
- Php设计模式之工厂模式(二)【工厂方法模式 Factory Method】