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

  • Home
  • SEARCH
  • 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 6960785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:26:45+00:00 2026-05-27T15:26:45+00:00

In the following code I am able to retrieve the _id value of each

  • 0

In the following code I am able to retrieve the _id value of each record and display it along with the text in a ListView but when I select an item from the list the returned value is 0 to N dependent on how the results are laid out in the list.

How can I get the _id value, I guess as a named value pair so that when 0 or 1… is selected it outputs the _id field and not 0 or 1… for my OnItemClickListener

This is my method, it’s messy, once I get it working I’ll try to refine it!

private void GetCoordinates(double currentLatitude, double currentLongitude) {

    List<String> ar = new ArrayList<String>();

    dbBookHelper = new DatabaseHelper(this);
    ourCursor = dbBookHelper.getCoordinates();

    int counta = 0; 
    ourCursor.moveToFirst();                    
    do {                    
        id = ourCursor.getInt(ourCursor.getColumnIndex("_id"));
        BeachName = ourCursor.getString(ourCursor.getColumnIndex("BeachName"));
        beachLatitude = ourCursor.getDouble(ourCursor.getColumnIndex("latitude"));
        beachLongitude = ourCursor.getDouble(ourCursor.getColumnIndex("longitude"));

        distence = ConvertDistance(beachLatitude, beachLongitude);

        if (distence <= 5) {
            ar.add(id + " " + BeachName + " - " + distence + "Kms");
            counta++;               
        }

    } while (ourCursor.moveToNext());

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row2, R.id.beachListText, ar);
    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(onListClick);

    Toast.makeText(getBaseContext(), "There are " + String.valueOf(counta) + " beaches within a 5km radius!", Toast.LENGTH_LONG).show();        
}

And this is my OnItemClickListener method

private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {           
        Toast.makeText(getBaseContext(), String.valueOf(id) + " selected", Toast.LENGTH_LONG).show();
    }
};

Any help would be greatly appreciated,

Cheers,

Mike.

Edit: Thanks guys, I was hoping for a slicker way too!

But I now have a second array holding just the id values with,

ar1.add(String.valueOf(id));

So the positions are the same, but how do I get them into the OnItemClickListener? I guess somewhere in here???

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row2, R.id.beachListText, ar);
setListAdapter(adapter);

ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(onListClick);
  • 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-27T15:26:46+00:00Added an answer on May 27, 2026 at 3:26 pm

    The basic problem is the ArrayAdapter does not know anything about the Cursor or rowId. I think you have 2 choices. The first is to manage the mapping of array position to rowId yourself. For example, create a second array to map the ArrayList position to the rowId, and do a simple lookup in the listener.

    If that is not appropriate for some reason then you could create a custom adapter with knowledge of the Cursor, by extending CursorAdapter. It involves over-riding 2 methods newView() and bindView() to allocate and populate the views (with your custom string) that will be displayed in each row. It also provides filtering hooks that would allow you to implement the < 5KM filter you need.

    I haven’t gone through this particular case myself, but did recently have to extend an ArrayAdapter to implement a SectionIndexer for a very long list. While it was a valuable exercise, I think in your case a custom adapter is possibly overkill. A second array look-up may be simpler and more appropriate.

    1) Make your new array a class member so it is accessible in the listener

    ArrayList<Long> mIdArr = null;
    

    2) Create this in a similar way to your String array

    mIdArr = new ArrayList<Long>();
    

    3) Store the rowId at the same point you add to your String array

    ar.add(id + " " + BeachName + " - " + distence + "Kms");
    mIdArr.add(new Long(id));
    

    4) Retrieve the Id in your listener like this

    private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {           
            Long rowId = mIdArr.get(position);
            Toast.makeText(getBaseContext(), String.valueOf(rowId) + " selected", Toast.LENGTH_LONG).show();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code through which i am able to retrieve phone numbers.
Using the following code, I am able to display up to 10 images. What
I seem to be able to retrieve the value of the cookie but the
Using the following code, I'm able to successfully open a raw disk on my
I have a drop down in a jsp page having the following code <s:select
I am able to retrieve all contacts from android in .vcf file using following
For chrome, firefox, and safari the following code seems to work and be able
I have the follwing code, I m able to create a drop down list
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to

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.