Question

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);
            }       
        };
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top