Android 点击自动完成列表
我有一个autocomplete,它可以查询DB…当点击autocomplete中显示的其中一个条目时,我想获取我点击的条目并显示它…为此,我实现了onClickListener: 这是我的密码:Android 点击自动完成列表,android,sqlite,autocomplete,cursor,Android,Sqlite,Autocomplete,Cursor,我有一个autocomplete,它可以查询DB…当点击autocomplete中显示的其中一个条目时,我想获取我点击的条目并显示它…为此,我实现了onClickListener: 这是我的密码: AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_from); ContactListCursorAdapter adapter = new ContactListCursorAd
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_from);
ContactListCursorAdapter adapter = new ContactListCursorAdapter(this,
cursor);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3){
System.out.println("Click la autocomplet pe :" +arg0.getItemAtPosition(arg2).toString());
}
});
任何人都知道怎么做,因为这显然不适合我
这是我的全部代码: 我正在做的是将自动完成与数据库绑定…一旦我开始在自动完成中键入内容,就会显示来自我的数据库的2列内容…当然,会使用我开始键入的内容进行过滤: 公共类Server8扩展了MapActivity{ DBAdapter-db; 字符序列1; 地图视图; 专用地图控制器; 私人进展对话进展; InitTask init_task=null; 字符串用户标识
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server8);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_from);
progress = new ProgressDialog(this);
progress.setIndeterminate(true);
progress.setMessage("I am thinking");
db = new DBAdapter(this);
db.createDatabase();
db.openDataBase();
Cursor cursor = db.getAllData2();
textView.setHint("type route");
textView.setThreshold(2);
startManagingCursor(cursor);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapView.setSatellite(true);
mc = mapView.getController();
mc.setZoom(10);
ContactListCursorAdapter adapter = new ContactListCursorAdapter(this,
cursor);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Cursor c = (Cursor) arg0.getItemAtPosition(arg2);
int nameCol = c.getColumnIndex(db.KEY_ROWID_2);
user_id = c.getString(nameCol);
init_task = new InitTask();
init_task.execute(db);
}
});
Button b=(Button)findViewById(R.id.stop);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg1) {
System.out.println("Click pe butonul stop!");
init_task.cancel(true);
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.server8);
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autocomplete_from);
进度=新建进度对话框(此对话框);
progress.setUndeterminate(true);
progress.setMessage(“我在思考”);
db=新的DBAdapter(本);
db.createDatabase();
openDataBase();
Cursor=db.getAllData2();
setHint(“类型路由”);
设置阈值(2);
开始管理游标(游标);
mapView=(mapView)findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
mapView.setStreetView(真);
mapView.setSatellite(真);
mc=mapView.getController();
mc.setZoom(10);
ContactListCursorAdapter=新的ContactListCursorAdapter(此,
光标);
setAdapter(适配器);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
游标c=(游标)arg0.getItemAtPosition(arg2);
int nameCol=c.getColumnIndex(db.KEY\u ROWID\u 2);
user_id=c.getString(nameCol);
init_task=新的InitTask();
init_task.execute(db);
}
});
按钮b=(按钮)findViewById(R.id.stop);
b、 setOnClickListener(新视图。OnClickListener(){
公共void onClick(视图arg1){
System.out.println(“单击pe butonul停止!”);
初始化任务。取消(true);
}
}))
}
public void theroutdraw(地质点p){
司马迁(p),;
mc.setZoom(17);
mapView.setSatellite(真);
mapView.setStreetView(真);
mapView.invalidate();
}
类myOverlay扩展覆盖{
地质点gp1;
地质点gp2;
公共myOverlay(地质点gp1、地质点gp2){
这是gp1=gp1;
这是gp2=gp2;
}
公共空白绘制(画布、地图视图、地图视图、布尔阴影){
Projection=mapView.getProjection();
Paint mPaint=新油漆();
点源=新点();
投影。toPixels(gp1,来自);
mPaint.setColor(Color.BLUE);
指向=新点();
投影。toPixels(gp2,to);
mPaint.设定行程宽度(9);
mPaint.setAlpha(120);
画布绘制线(from.x,from.y,to.x,to.y,mPaint);
super.draw(画布、地图视图、阴影);
}
}
公共类InitTask扩展了异步任务{
List geoPointsArray=new ArrayList();
DBAdapter-db;
国际纬度;
国际经度;
地质点p;
字符串表3;
受保护的void onPreExecute(){
progress.show();
}
受保护的Void doInBackground(DBAdapter…db){
试一试{
//db[0].openDataBase();
游标c=db[0]。getCursor3(db[0]。表3,用户id);
if(c.moveToFirst()){
做{
经度=(int)Double.parseDouble(c.getString(1));
纬度=(int)Double.parseDouble(c.getString(2));
System.out.println(“Valoarea latitudinii”+纬度
+经度);
p=新的地质点(经度、纬度);
出版进度(p);
睡眠(2500);
}而(c.moveToNext());
}
c、 close();
db[0].close();
}捕获(例外e){
日志d(“Eroare”、“doInBackground”,e);
}
返回null;
}
受保护的void onProgressUpdate(地质点…progress1){
试一试{
如果(geoPointsArray.size()==1){
mapView.getOverlays().add(
新建myOverlay(geoPointsArray.get(0),geoPointsArray
.get(0));
theRouteDraw(progress1[0]);
}
}捕获(例外e){
e、 printStackTrace();
}
如果(geoPointsArray.size()>1){
int i=geoPointsArray.size();
List overlays=mapView.getOverlays();
叠加。添加(新的myOverlay(地质点阵列)获取(i-1),
(1[0]);
theRouteDraw(progress1[0]);
}
geoPointsArray.add(progress1[0]);
}
}
公共类ContactListCursorAdapter扩展了CursorAdapter实现
可过滤{
私人语境;
私有文本视图mName,mNumber;
公共联系人列表游标或适配器(上下文,游标){
超级(上下文、游标);
this.context=上下文;
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
游标c=getCursor();
最终线性布局ret=新线性布局(上下文);
最终LayoutFlater充气机=LayoutFlater.from(上下文);
mName=(文本视图)充气器。充气(
android.R.layout.simple\u下拉列表\u项目\u 1行,父项,false);
mNumber=(文本视图)充气器。充气(
android.R.layout.simple\u下拉列表\u项目\u 1行,宾夕法尼亚州
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server8);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_from);
progress = new ProgressDialog(this);
progress.setIndeterminate(true);
progress.setMessage("I am thinking");
db = new DBAdapter(this);
db.createDatabase();
db.openDataBase();
Cursor cursor = db.getAllData2();
textView.setHint("type route");
textView.setThreshold(2);
startManagingCursor(cursor);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapView.setSatellite(true);
mc = mapView.getController();
mc.setZoom(10);
ContactListCursorAdapter adapter = new ContactListCursorAdapter(this,
cursor);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Cursor c = (Cursor) arg0.getItemAtPosition(arg2);
int nameCol = c.getColumnIndex(db.KEY_ROWID_2);
user_id = c.getString(nameCol);
init_task = new InitTask();
init_task.execute(db);
}
});
Button b=(Button)findViewById(R.id.stop);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg1) {
System.out.println("Click pe butonul stop!");
init_task.cancel(true);
}
public void theRouteDraw(GeoPoint p) {
mc.animateTo(p);
mc.setZoom(17);
mapView.setSatellite(true);
mapView.setStreetView(true);
mapView.invalidate();
}
class myOverlay extends Overlay {
GeoPoint gp1;
GeoPoint gp2;
public myOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint mPaint = new Paint();
Point from = new Point();
projection.toPixels(gp1, from);
mPaint.setColor(Color.BLUE);
Point to = new Point();
projection.toPixels(gp2, to);
mPaint.setStrokeWidth(9);
mPaint.setAlpha(120);
canvas.drawLine(from.x, from.y, to.x, to.y, mPaint);
super.draw(canvas, mapView, shadow);
}
}
public class InitTask extends AsyncTask<DBAdapter, GeoPoint, Void> {
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
DBAdapter db;
int latitude;
int longitude;
GeoPoint p;
String TABLE_3;
protected void onPreExecute() {
progress.show();
}
protected Void doInBackground(DBAdapter... db) {
try {
//db[0].openDataBase();
Cursor c = db[0].getCursor3(db[0].TABLE_3, user_id);
if (c.moveToFirst()) {
do {
longitude = (int) Double.parseDouble(c.getString(1));
latitude = (int) Double.parseDouble(c.getString(2));
System.out.println("Valoarea latitudinii" + latitude
+ longitude);
p = new GeoPoint(longitude, latitude);
publishProgress(p);
Thread.sleep(2500);
} while (c.moveToNext());
}
c.close();
db[0].close();
} catch (Exception e) {
Log.d("Eroare", "doInBackground", e);
}
return null;
}
protected void onProgressUpdate(GeoPoint... progress1) {
try {
if (geoPointsArray.size() == 1) {
mapView.getOverlays().add(
new myOverlay(geoPointsArray.get(0), geoPointsArray
.get(0)));
theRouteDraw(progress1[0]);
}
} catch (Exception e) {
e.printStackTrace();
}
if (geoPointsArray.size() > 1) {
int i = geoPointsArray.size();
List overlays = mapView.getOverlays();
overlays.add(new myOverlay(geoPointsArray.get(i - 1),
progress1[0]));
theRouteDraw(progress1[0]);
}
geoPointsArray.add(progress1[0]);
}
}
public class ContactListCursorAdapter extends CursorAdapter implements
Filterable {
private Context context;
private TextView mName, mNumber;
public ContactListCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.context = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LinearLayout ret = new LinearLayout(context);
final LayoutInflater inflater = LayoutInflater.from(context);
mName = (TextView) inflater.inflate(
android.R.layout.simple_dropdown_item_1line, parent, false);
mNumber = (TextView) inflater.inflate(
android.R.layout.simple_dropdown_item_1line, parent, false);
ret.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout horizontal = new LinearLayout(context);
horizontal.setOrientation(LinearLayout.HORIZONTAL);
int nameCol = c.getColumnIndex(db.KEY_SURSA);
String name = c.getString(nameCol);
String number = c.getString(c.getColumnIndex(db.KEY_DATE));
mName.setText(name);
mNumber.setText(number);
horizontal.addView(mName, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ret.addView(mNumber, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ret.addView(horizontal, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return ret;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int nameCol = cursor.getColumnIndex(db.KEY_SURSA);
String name = cursor.getString(nameCol);
String number = cursor
.getString(cursor.getColumnIndex(db.KEY_DATE));
((TextView) ((LinearLayout) view).getChildAt(0)).setText(number);
LinearLayout horizontal = (LinearLayout) ((LinearLayout) view)
.getChildAt(1);
((TextView) horizontal.getChildAt(0)).setText(name);
}
public CharSequence convertToString(Cursor cursor) {
int numcol = cursor.getColumnIndexOrThrow(db.KEY_SURSA);
String name = cursor.getString(numcol);
return name;
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
String filter = "";
if (constraint == null)
filter = "";
else
filter = constraint.toString();
Cursor cursor = db.getCursor(filter);
return cursor;
}
}
public void onDestroy(){
super.onDestroy();
db.close();
}
protected void onPause() {
super.onPause();
if(init_task!=null){
init_task.cancel(true);
init_task=null;
}
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Cursor c = (Cursor) arg0.getItemAtPosition(arg2);
int nameCol = c.getColumnIndex(db.KEY_ROWID_2);
user_id = c.getString(nameCol);
init_task = new InitTask();
init_task.execute(db);
}
});
@Override
public String convertToString(Cursor cursor) {
// this method dictates what is shown when the user clicks each entry in your autocomplete list
String name="";
name = cursor.getString(cursor.getColumnIndex("column1"))
Id = cursor.getInt(cursor.getColumnIndex("_id"));
return name;
}
public int getId(){
return Id;
}
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_from);
ContactListCursorAdapter adapter = new ContactListCursorAdapter(this, cursor);
int Id = adapter.getDrugId();