当前位置: 代码迷 >> Android >> 2011.09.07(五)——— android 跨进程通信之content provider + AutoCompleteTextView
  详细解决方案

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

热度:389   发布时间:2016-05-01 18:54:26.0
2011.09.07(5)——— android 跨进程通信之content provider + AutoCompleteTextView
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);      }  @Override	public 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() {						@Override			public void onTextChanged(CharSequence s, int start, int before, int count) {				// TODO Auto-generated method stub							}						@Override			public void beforeTextChanged(CharSequence s, int start, int count, int after) {				// TODO Auto-generated method stub							}						@Override			public 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对象来显示当前的列表项

  相关解决方案