android filter
![此博文包含图片](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
标签: 杂谈 |
一、何谓Android的过滤机制?
- public
static final class Location implements BaseColumns { -
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "location" ); -
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/weather_location" ; -
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/weather_location" ; -
public static final String CONTENT_DIRECTORY = "location" ; -
-
public static final String LOCATION_NAME = "location_name" ; -
public static final String LONGITUDE = "longitude" ; -
public static final String ALTITUDE = "altitude" ; - }
- private
static class LocationAdapter extends CursorAdapter { -
private LayoutInflater mInflater; -
private Context mContext; -
private Cursor mCursor; -
public LocationAdapter(Context context, Cursor c) { -
super (context, c); -
mContext = context; -
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); -
} -
-
@Override -
public void bindView(View view, Context context, Cursor cursor) { -
TextView textView = (TextView) view; -
String location = cursor.getString(cursor.getColumnIndex(Location.LOCATION_NAME)); -
textView.setText(location); -
} -
-
@Override -
public View newView(Context context, Cursor cursor, ViewGroup parent) { -
return mInflater.inflate(android.R.layout.simple_dropdown_item_1line, null ); -
} -
-
@Override -
public Cursor runQueryOnBackgroundThre ad(CharSequence constraint) { -
String selection = Location.LOCATION_NAME + " LIKE '%" + constraint + "%'" ; -
return mContext.getContentResolver().query(Location.CONTENT_URI -
, null , selection, null , null ); -
} -
-
-
@Override -
public CharSequence convertToString(Cursor cursor) { -
return cursor.getString(cursor.getColumnIndex(Location.LOCATION_NAME)); -
} - }
- private
void initialTextView() { -
mTextView = new AutoCompleteTextView(mContext); -
mTextView.setThreshold(0 ); -
-
Cursor cursor = mContext.getContentResolver().query(Location.CONTENT_URI -
, null , null , null , null ); -
LocationAdapter adapter = new LocationAdapter(mContext, cursor); -
mTextView.setAdapter(adapter); - }