user903601
user903601

Reputation:

Android name value pairs from an ArrayList

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

Upvotes: 0

Views: 4337

Answers (1)

scotinus
scotinus

Reputation: 176

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

Upvotes: 1

Related Questions