Textwatcher和SQLite,ListView集合的基本用法或许有用
2012-03-05 13:53
471 查看
两个xml文件
main.xml
<?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" >
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:ellipsize="marquee" android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:text="Please input the text:" />
<EditText android:id="@+id/ET"
android:hint="请输入要查询的信息"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
listadapter.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="fill_parent"
android:orientation="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="30px" >
<TextView android:id="@+id/tv12"
android:layout_width="fill_parent"
android:layout_height="15px" />
<TextView android:id="@+id/tv11"
android:layout_width="fill_parent"
android:layout_height="15px" />
</LinearLayout>
</LinearLayout>
主Activity
package ntc.textwatcher;
import java.util.ArrayList;
import java.util.List;
import ntc.textwatcher.jdbc.BaseSqliteHelper;
import ntc.textwatcher.jdbc.sql;
import ntc.textwatcher.model.Cys;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TextWatcherActivity extends Activity {
/** Called when the activity is first created. */
private TextView mTextView;
private EditText mEditText;
private BaseSqliteHelper base=new BaseSqliteHelper(this); //BaseSqliteHelper 自己写的类获取BaseSqliteHelper的实例
private SQLiteDatabase db; //获取数据库的实例
List<Cys> list=new ArrayList<Cys>(); //model 文件夹下的自己写的类Cys
ListView lv=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView)findViewById(R.id.tv);
mEditText = (EditText)findViewById(R.id.ET);
mEditText.addTextChangedListener(mTextWatcher);
db=base.getWritableDatabase();//用于调用BaseSQLiteHelper中的onCreate方法,主要是创建数据库和表格,版本的更新等等
}
private TextWatcher mTextWatcher=new TextWatcher(){
private CharSequence temp;
private int editStart ; //开始输入时
private int editEnd ; //结束输入时
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
temp=s; //输入text前s赋给temp
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mTextView.setText(s);
//输入时 s赋给Text
}
public void afterTextChanged(Editable s) {
//输入后
editStart=mTextView.getSelectionStart();
editEnd=mTextView.getSelectionEnd();
if(temp.length()>10){
Toast.makeText(TextWatcherActivity.this, "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT).show();
s.delete(editStart-1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
String m= s.toString() ;
String[] ss= m.split(":");
lv=(ListView)findViewById(R.id.lv);
list= new sql(base).query(ss);
//
//
lv.setAdapter(new adapter(TextWatcherActivity.this,list));
}
};
class adapter extends BaseAdapter{ //listview的适配器
List<Cys> list=null;
LayoutInflater inflater=null;
public adapter(Context context,List<Cys> list) //构造函数
{
this.list=list;
inflater=LayoutInflater.from(context); //获取上下文将其转换为布局
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView=inflater.inflate(R.layout.listadapter, null);
//final int id=contentview.;
TextView tv1=(TextView)convertView.findViewById(R.id.tv12);
TextView tv2=(TextView)convertView.findViewById(R.id.tv11);
tv1.setText( list.get(position).getName());
tv2.setText(list.get(position).getFirstname());
return convertView;
}
}
}
model包下的Cys类
package ntc.textwatcher.model;
public class Cys {
private String name;
private String firstname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Cys(String name,String firstname)
{
this.name=name;
this.firstname=firstname;
}
}
数据库交互的jdbc包
package ntc.textwatcher.jdbc;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class BaseSqliteHelper extends SQLiteOpenHelper {
private final static String DB_NAME="student.db";
private final static int DB_VERSION=1; //可以更新版本通过Update类
private Context context;
public BaseSqliteHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
//VERSION=2
//create table tblStudent (sid int primary key, sname varchar(10), sage int);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//VERSION=1
db.execSQL("create table tblStudent (sid int primary key, sname varchar(10))");
db.execSQL("insert into tblStudent values (1, 'Tom')");
db.execSQL("insert into tblStudent values (2, 'Sam')");
System.out.println("run init");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// db.beginTransaction();//事务注意
// try{
// db.execSQL("alter table tblStudent rename to tblStudentTemp");
// db.execSQL("create table tblStudent (sid int primary key, sname varchar(10), sage int)");
// db.execSQL("insert into tblStudent (sid, sname, sage) select sid, sname, 18 from tblStudentTemp");
// db.execSQL("drop table tblStudentTemp");
// System.out.println("run update");
// db.setTransactionSuccessful();
// }
// finally{
// db.endTransaction();}
}
}
//sql语句类
package ntc.textwatcher.jdbc;
import java.util.ArrayList;
import java.util.List;
import ntc.textwatcher.model.Cys;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class sql {
private static BaseSqliteHelper helper;
public sql(BaseSqliteHelper helper)
{
this.helper=helper;
}
public static List<Cys> query(String[] ss)
{
List<Cys> list=new ArrayList<Cys>();
Cursor cursor=null;
SQLiteDatabase db= helper.getReadableDatabase();
for(String ll:ss)
{
String s="%"+ll+"%";
cursor=db.rawQuery("select * from tblStudent where ( sname like ? or sid like ?)",new String[]{s,s});
//用or语句,必须上面这样new String【】{},其实query(String[] ss)的ss就是一个String
while(cursor.moveToNext())
{ String sname=cursor.getString(cursor.getColumnIndex("sid"));
String sfirstname=cursor.getString(cursor.getColumnIndex("sname"));
Cys cys=new Cys(sname,sfirstname);
list.add(cys);
}}
return list;
}
public Boolean insert(Cys cys)
{
try{
SQLiteDatabase db=helper.getWritableDatabase();
db.execSQL("inset into tblStudent values(?,?)",new Object[]{cys.getName(),cys.getFirstname()});
}catch(Exception e)
{
return false;
}
return true;
}
}
main.xml
<?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" >
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:ellipsize="marquee" android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:text="Please input the text:" />
<EditText android:id="@+id/ET"
android:hint="请输入要查询的信息"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
listadapter.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="fill_parent"
android:orientation="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="30px" >
<TextView android:id="@+id/tv12"
android:layout_width="fill_parent"
android:layout_height="15px" />
<TextView android:id="@+id/tv11"
android:layout_width="fill_parent"
android:layout_height="15px" />
</LinearLayout>
</LinearLayout>
主Activity
package ntc.textwatcher;
import java.util.ArrayList;
import java.util.List;
import ntc.textwatcher.jdbc.BaseSqliteHelper;
import ntc.textwatcher.jdbc.sql;
import ntc.textwatcher.model.Cys;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TextWatcherActivity extends Activity {
/** Called when the activity is first created. */
private TextView mTextView;
private EditText mEditText;
private BaseSqliteHelper base=new BaseSqliteHelper(this); //BaseSqliteHelper 自己写的类获取BaseSqliteHelper的实例
private SQLiteDatabase db; //获取数据库的实例
List<Cys> list=new ArrayList<Cys>(); //model 文件夹下的自己写的类Cys
ListView lv=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView)findViewById(R.id.tv);
mEditText = (EditText)findViewById(R.id.ET);
mEditText.addTextChangedListener(mTextWatcher);
db=base.getWritableDatabase();//用于调用BaseSQLiteHelper中的onCreate方法,主要是创建数据库和表格,版本的更新等等
}
private TextWatcher mTextWatcher=new TextWatcher(){
private CharSequence temp;
private int editStart ; //开始输入时
private int editEnd ; //结束输入时
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
temp=s; //输入text前s赋给temp
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mTextView.setText(s);
//输入时 s赋给Text
}
public void afterTextChanged(Editable s) {
//输入后
editStart=mTextView.getSelectionStart();
editEnd=mTextView.getSelectionEnd();
if(temp.length()>10){
Toast.makeText(TextWatcherActivity.this, "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT).show();
s.delete(editStart-1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
String m= s.toString() ;
String[] ss= m.split(":");
lv=(ListView)findViewById(R.id.lv);
list= new sql(base).query(ss);
//
//
lv.setAdapter(new adapter(TextWatcherActivity.this,list));
}
};
class adapter extends BaseAdapter{ //listview的适配器
List<Cys> list=null;
LayoutInflater inflater=null;
public adapter(Context context,List<Cys> list) //构造函数
{
this.list=list;
inflater=LayoutInflater.from(context); //获取上下文将其转换为布局
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView=inflater.inflate(R.layout.listadapter, null);
//final int id=contentview.;
TextView tv1=(TextView)convertView.findViewById(R.id.tv12);
TextView tv2=(TextView)convertView.findViewById(R.id.tv11);
tv1.setText( list.get(position).getName());
tv2.setText(list.get(position).getFirstname());
return convertView;
}
}
}
model包下的Cys类
package ntc.textwatcher.model;
public class Cys {
private String name;
private String firstname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Cys(String name,String firstname)
{
this.name=name;
this.firstname=firstname;
}
}
数据库交互的jdbc包
package ntc.textwatcher.jdbc;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class BaseSqliteHelper extends SQLiteOpenHelper {
private final static String DB_NAME="student.db";
private final static int DB_VERSION=1; //可以更新版本通过Update类
private Context context;
public BaseSqliteHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
//VERSION=2
//create table tblStudent (sid int primary key, sname varchar(10), sage int);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//VERSION=1
db.execSQL("create table tblStudent (sid int primary key, sname varchar(10))");
db.execSQL("insert into tblStudent values (1, 'Tom')");
db.execSQL("insert into tblStudent values (2, 'Sam')");
System.out.println("run init");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// db.beginTransaction();//事务注意
// try{
// db.execSQL("alter table tblStudent rename to tblStudentTemp");
// db.execSQL("create table tblStudent (sid int primary key, sname varchar(10), sage int)");
// db.execSQL("insert into tblStudent (sid, sname, sage) select sid, sname, 18 from tblStudentTemp");
// db.execSQL("drop table tblStudentTemp");
// System.out.println("run update");
// db.setTransactionSuccessful();
// }
// finally{
// db.endTransaction();}
}
}
//sql语句类
package ntc.textwatcher.jdbc;
import java.util.ArrayList;
import java.util.List;
import ntc.textwatcher.model.Cys;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class sql {
private static BaseSqliteHelper helper;
public sql(BaseSqliteHelper helper)
{
this.helper=helper;
}
public static List<Cys> query(String[] ss)
{
List<Cys> list=new ArrayList<Cys>();
Cursor cursor=null;
SQLiteDatabase db= helper.getReadableDatabase();
for(String ll:ss)
{
String s="%"+ll+"%";
cursor=db.rawQuery("select * from tblStudent where ( sname like ? or sid like ?)",new String[]{s,s});
//用or语句,必须上面这样new String【】{},其实query(String[] ss)的ss就是一个String
while(cursor.moveToNext())
{ String sname=cursor.getString(cursor.getColumnIndex("sid"));
String sfirstname=cursor.getString(cursor.getColumnIndex("sname"));
Cys cys=new Cys(sname,sfirstname);
list.add(cys);
}}
return list;
}
public Boolean insert(Cys cys)
{
try{
SQLiteDatabase db=helper.getWritableDatabase();
db.execSQL("inset into tblStudent values(?,?)",new Object[]{cys.getName(),cys.getFirstname()});
}catch(Exception e)
{
return false;
}
return true;
}
}
相关文章推荐
- TextWatcher基本用法
- TextWatcher基本用法
- TextWatcher基本用法
- Android中SQLite基本用法
- Android AutoCompleteTextView控件基本用法示例
- IOS CoreText.framework --- 基本用法
- C# ListView控件基本用法
- Android 操作SQLite基本用法
- Android ListView控件基本用法
- Delphi7 listview基本用法大全
- Delphi ListView基本用法大全
- Android ListView控件基本用法
- Ext.form.TextField组件的基本用法
- Android ListView 的基本用法
- [置顶] Android开发 ——线性布局文件、TextView、ListView的基本写法
- sqlite基本用法(待总结)
- Oracle集合的基本用法
- iOS开发 Coretext(文字排版)的基本用法
- ListView 基本用法
- Android 操作SQLite基本用法