2011.09.07(5)——— android android 跨进程通信之content provider + AutoCompleteTextView

参考:http://blog.csdn.net/yan8024/article/details/6444368
http://www.blogjava.net/nokiaguy/archive/2010/07/31/327623.html

在上一节的基础上 增加了自动提示框的功能 就是将上一届的EditText改为AutoCompleteTextView

1、自定义provider需要修改 因为我们需要根据前几个字符来得到name
private  static  final  String AUTHORITY = "com.helloword.myprovider" ;private  static  UriMatcher uriMatcher;  private  static  final  int  ONE = 1 ;  private  static  final  int  MORE = 2 ;  private  static  final  int  MORE2 = 3 ;  static       {          //  添加访问ContentProvider的Uri           uriMatcher = new  UriMatcher(UriMatcher.NO_MATCH);          uriMatcher.addURI(AUTHORITY, "one" , ONE);          uriMatcher.addURI(AUTHORITY, "more/*" , MORE);          uriMatcher.addURI(AUTHORITY, "more2/*" , MORE2);      }  @Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {Cursor cursor = null;System.out.println("query");switch(uriMatcher.match(uri)){case ONE:cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);break;case MORE:{String word = uri.getPathSegments().get(1);cursor = db.rawQuery("select * from "+TABLE_NAME+" where displayname like ?", new String[]{word+"%"});break;}case MORE2:String word = uri.getPathSegments().get(1);cursor = db.rawQuery("select displayname as _id from "+TABLE_NAME+" where displayname like ?", new String[]{word+"%"});break;default:throw new IllegalArgumentException("无效参数");}return cursor;}


注意看:case MORE2 时
select displayname as _id from 

必须更名为_id

2、从另一个应用调用:

main.xml

<AutoCompleteTextView android:id="@+id/autoComplete"           android:layout_width="fill_parent"           android:layout_height="wrap_content">    </AutoCompleteTextView>  


auto.xml 下拉框的布局文件

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/tvWordItem"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:textAppearance="?android:attr/textAppearanceLarge"    android:gravity="center_vertical"    android:paddingLeft="6dip"    android:textColor="#000"        android:minHeight="?android:attr/listPreferredItemHeight"/>



Activity

actv = (AutoCompleteTextView)findViewById(R.id.autoComplete);        //将AutoCompleteTextView与ArrayAdapter进行绑定          //actv.setAdapter(adapter);          //设置AutoCompleteTextView输入1个字符就进行提示          actv.setThreshold(1);          actv.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {// TODO Auto-generated method stub}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {// TODO Auto-generated method stub}@Overridepublic void afterTextChanged(Editable s) {if  ("" .equals(s.toString()))          return ;  System.out.println(s.toString());    Uri uri = Uri.parse(MORE2 + "/"  + s.toString());      //  从ContentProvider中获得以某个字符串开头的所有单词的Cursor对象       Cursor cursor = getContentResolver().query(uri, null , null , null , null );      adapter = new  TelListAdapter(helloWorldActivity.this ,              cursor, true );      actv.setAdapter(adapter);  }});


public class TelListAdapter extends CursorAdapter    {        private LayoutInflater layoutInflater;        @Override        public CharSequence convertToString(Cursor cursor)        {            return cursor == null ? "" : cursor.getString(cursor                    .getColumnIndex("_id"));        }        //  用于将_id字段(也就是diaplayname字段)的值设置TextView组件的文本        //  view参数表示用于显示列表项的TextView组件        private void setView(View view, Cursor cursor)        {            TextView tvWordItem = (TextView) view;            tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));        }        @Override        public void bindView(View view, Context context, Cursor cursor)        {            setView(view, cursor);        }        @Override        public View newView(Context context, Cursor cursor, ViewGroup parent)        {            View view = layoutInflater.inflate(R.layout.auto, null);            setView(view, cursor);            return view;        }        public TelListAdapter(Context context, Cursor c, boolean autoRequery)        {            super(context, c, autoRequery);            //  通过系统服务获得LayoutInflater对象            layoutInflater = (LayoutInflater) context                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        }    }



在编写DictionaryAdapter类时应注意如下3点:
1. 为了将Cursor对象与AutoCompleteTextView组件绑定, DictionaryAdapter类必须从CursorAdapter类继承。
2. 由于CursorAdapter类中的convertToString方法直接返回了Cursor对象的地址,因此,在DictionaryAdapter类中必须覆盖convertToString方法,以返回当前选中的单词。CursorAdapter类中的convertToString方法的源代码。
在这里要注意一下,当选中AutoCompleteTextView组件中单词列表中某一个单词后,系统会用convertToString方法的返回值来设置AutoCompleteTextView组件中的文本。因此,必须使用Cursor的getString来获得相应的字段值。
3. 由于将Cursor对象与Adapter绑定时必须要有一个叫“_id”的字段,因此,在本例中将english字段名映射成了“_id”字段。
为了监视AutoCompleteTextView组件中的文本输入情况,需要实现android.text.TextWatcher接口。在该接口中只需要实现afterTextChanged方法即可,
4. 在DictionaryAdapter类中需要使用bindView和newView方法设置每一个列表项。bindView方法负责设置已经存在的列表项,也就是该列表项已经生成了相应的组件对象。而newView方法负责设置新的列表项,在该方法中需要创建一个View对象来显示当前的列表项

更多相关文章

  1. 无法安装ADT(无法访问https://dl-ssl.google.com/android/eclipse
  2. android 几种发送短信的方法
  3. Android基本组件
  4. 【Android】一种提高Android应用进程存活率新方法
  5. 更改Android AVD模拟器创建路径位置的方法
  6. Android Button的背景图片拉伸变形解决方法
  7. Android常用组件,太全了
  8. Android解析json数组对象

随机推荐

  1. Flutter 环境第一次运行问题 ProcessExce
  2. Android的单元测试
  3. android 做报表的一些控件
  4. android tab上显示数字
  5. Windows下 BlackBerry & Google Android(
  6. Android(安卓)Activity界面切换添加动画
  7. 我开发的第一个Android软件
  8. Android(安卓)- listview未滚动触发onScr
  9. Android控件之Textiew的使用
  10. 横竖屏切换和contentView的三种显示方式