Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:22:19+00:00 2026-05-22T22:22:19+00:00

I have an autocomplete which does a query to DB….when doing a click on

  • 0

I have an autocomplete which does a query to DB….when doing a click on one of the entries displayed within the autocomplete I wanna take the item I clicked on and display it…for this I implemented onClickListener:

Here is my code:

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

        });

But here is what my System.out displays:

Click la autocomplet pe :android.database.sqlite.SQLiteCursor@43c33e18


Click la autocomplet pe :android.database.sqlite.SQLiteCursor@43c33e18

Anyone has any idea of how could that be done cause this clearly is not working for me!


Here is my entire code:

What I’m doing is binding an autocomplete with a DB…..as soon as I start typing in my autocomplete there are displayed the content oof 2 columns from my DB….of course filtered with what I start typing:

public class Server8 extends MapActivity {
DBAdapter db;
CharSequence constraint1;
MapView mapView;
private MapController mc;
private ProgressDialog progress;
InitTask init_task=null;
String 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;
}

}

This is how my autocomplete acts when I click on it:

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

        }

    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:22:20+00:00Added an answer on May 22, 2026 at 10:22 pm

    @embry the onItemclickListener for this textview will return the cursor not the item within the cursor.

    In the custom cursor adaptor(ContactListCursorAdapter) you will be implementing a method called covertToString(Cursor). this method dictates what is shown when the user clicks each entry in your autocomplete list. Here is where you can get the value selected.

    @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;
     }
    

    I get the id in convertToString(). and a method getId() to return the id. so in the main method

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

    This works for me. Iam not sure whether this is the right way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app in android which uses an autocomplete for a query in
i have a textinput field which has autoComplete, i populate its dataprovider from a
So, I have an autocomplete dropdown with a list of townships. Initially I just
Is there any way to have the QCompleter to act like an autocomplete for
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a backgorund thread that extends AsyncTask and which I use in activity
We have an auto-complete list that's populated when an you send an email to
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.