【问题标题】:clicking on autocomplete list点击自动完成列表
【发布时间】:2011-09-04 19:40:57
【问题描述】:

我有一个自动完成功能,它对 DB 进行查询....单击自动完成功能中显示的条目之一时,我想获取单击的项目并显示它...为此,我实现了 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 { 数据库适配器数据库; CharSequence 约束1; 地图视图地图视图; 私有 MapController mc; 私人 ProgressDialog 进度; 初始化任务 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, 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;
}

}

当我点击它时,我的自动完成功能是这样的:

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);

        }

    });

【问题讨论】:

  • 错误可能在数据库中。要解决这个问题,您可以发布更多代码吗?
  • 是的....但是代码很冗长。我已经在上一个问题上发布了它....我会把链接放在这里...如果你明白,请告诉我
  • 我认为 arg0.getItemAtPosition(arg2) 返回一个游标......也许你必须从中提取数据,因为使用 .toString() 它只返回对象的名称..
  • stackoverflow.com/questions/6171023/… ......对这段代码的唯一改进是上面添加的监听器......剩下的就是这个!!!!
  • 我不知道...因为 arg0.getItemAtPosition(arg2) 它没有太多的提取方法.....它不允许我提取任何东西

标签: android sqlite autocomplete cursor


【解决方案1】:

@embry 此文本视图的 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。和一个方法 getId() 来返回 id。所以在main方法中

 AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_from);
 ContactListCursorAdapter adapter = new ContactListCursorAdapter(this,               cursor);
int Id = adapter.getDrugId();

这对我有用。我不确定这是否是正确的方法。

【讨论】:

  • 我找到了一种对我有用的方法......它有点不同。但是你是对的 onItemclickListener() 返回一个光标。无论如何你有一个观点,我接受~
  • @embry 很高兴知道它对您有用。你能分享一下你是怎么做的吗?是相似的还是不同的?
  • 我会更新我的问题......如果你不明白,请询问更多细节,我会给你!
  • 你有完整的代码......最后是你感兴趣的:)
猜你喜欢
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
相关资源
最近更新 更多