单击自动完成列表

如何解决单击自动完成列表

我有一个自动完成功能,可查询数据库。...单击自动完成功能中显示的条目之一时,我想将我单击的项目显示出来...为此,我实现了onClickListener: 这是我的代码:
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());
            }

        });
但是,这是我的System.out显示的内容:
Click la autocomplet pe :android.database.sqlite.SQLiteCursor@43c33e18


Click la autocomplet pe :android.database.sqlite.SQLiteCursor@43c33e18
任何人都不知道该怎么做,因为这显然对我不起作用! 这是我的整个代码: 我正在做的是将自动完成功能与数据库绑定.....一旦我开始输入自动完成功能,就会显示数据库中2列的内容。...当然,要过滤掉我开始输入的内容: 公共类Server8扩展了MapActivity {     DBAdapter db;     CharSequence约束1;     MapView mapView;     私有MapController mc;     私人ProgressDialog进度;     InitTask init_task = null;     字符串user_id;
@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,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,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,LayoutParams.WRAP_CONTENT));
        ret.addView(horizontal,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;
}
} 单击后,这就是我的自动填充行为: textView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0,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);

        }

    });
    

解决方法

        @embry此textview的onItemclickListener将返回光标,而不是光标内的项目。 在自定义光标适配器(ContactListCursorAdapter)中,您将实现一个名为covertToString(Cursor)的方法。此方法指示用户单击自动完成列表中的每个条目时显示的内容。在这里可以获取所选的值。
@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;
 }
我在convertToString()中获得ID。以及用于返回ID的方法getId()。所以在主要方法中
 AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_from);
 ContactListCursorAdapter adapter = new ContactListCursorAdapter(this,cursor);
int Id = adapter.getDrugId();
这对我有用。我不确定这是否是正确的方法。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-