views:

51

answers:

1

I have a custom listview row that contains a number of textView components. Instead of the "standard" single text item, I have created a item where each listview row contains several bits of information. For this example, I have a record id, a name, and a description for each row in the listview.

I have my listview populated via

this.mDbHelper = new RecordDBAdapter(this);
this.mDbHelper.open();
Cursor c = this.mDbHelper.fetchAllRecords();
startManagingCursor(c);

String[] from = new String[] {RecordDBAdapter.ROW_ID, RecordDBAdapter.NAME, RecordDBAdapter.DESCRIPTION};

int[] to = new int[] {R.id.recordId, R.id.lblName, R.id.lblDescription};

// Now create an array adapter and set it to display using our row
SimpleCursorAdapter records =  new SimpleCursorAdapter(this, R.layout.record_row, c, from, to);

this.list.setAdapter(dives);

Now what I want is to be able to access the recordId value within each clicked item. I've tried to follow the Android tutorials to no avail. They do something like

Object o = adapter.getItemAtPosition(position);

but that still doesn't help either. What I REALLY want is to get the value of the recordId WITHIN each selected listItem. Does anyone know how this would be accomplished? Here is my onItemClick event:

     protected OnItemClickListener onListItemClick = new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long rowId) {

// My goal is either to grab the value of the
// recordId value from the textView, or from the adapter.
                //Object o = adapter.getItemAtPosition(position); //Tried this, no joy
                Intent intent = new Intent(NeatActivity.this, SomeOtherActivity.class);             
        //      intent.putExtra("selectedRecordId",val); //val here is a numerical record row id being passed to another activity.
                startActivity(intent);
            }       
        };
A: 

You're trying to do too much out of the default adapter class. You need to write your own adapter class, extend simplecursoradapter, and then you can make any method you want to get whatever you want at any position you want.

Falmarri
Thanks for the response. I'm still rather new to java and haven't figured out all the intricacies. So you are saying that there is no way to access the values in either the adapter or the textview without subclassing? It would seem to me those values would be accessible since we are passing both the adapter and the view to the onListItemCLick handler. When I place a watch expression, I can see the columns in the adapter, but no way to get the values held in each column. Kinda confused as to why we need the adapter and view as parameters if they can't be used to retrieve data values?
Shawn Yale
I'm sure they're acccessable. It just you'll probably have to do more than the class was meant to do. The base classes are there as a framework. You're pretty much expected to subclass the adapters when doing anything more than simply displaying values
Falmarri