Другой возможностью является использование методов setTag / getTag для каждого элемента View в списке - это позволяет вам присоединить 'Book' или 'Category' к соответствующей строке.
Для того, чтобы это работало каждый«перечисленные» элементы должны реализовывать общий интерфейс, который имеет соответствующий метод, который вызывается при нажатии.
Например:
interface Selectable {
void onSelected(); // Add parameters if necessary...
}
В вашем адаптере списка:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView; // Logic to get the appropriate view for this row.
Selectable theObjectBeingRepresented;
//
// Logic to assign a Book/Category, etc to theObjectBeingRepresented.
// In this example assume there is a magic Book being represented by
// this row.
//
Book aMagicBook = new Book();
theObjectBeingRepresented = aMagicBook;
// If all objects that will be contained in your list implement the
// 'Selectable' interface then you can simply call setTag(...) with
// the object you are working with (otherwise you may need some conditional logic)
//
itemView.setTag( SELECTABLE_ITEM_KEY, theObjectBeingRepresented );
}
Затем при нажатии на элемент:
void onItemClick(AdapterView<?> adapterView, View view, int rowIndex, long rowId)
{
Selectable item = (Selectable)view.getTag( SELECTABLE_ITEM_KEY );
// Depending on how you are populating the list, you may need to check for null
// before operating on the item..
if( null != item ) {
item.onClick(); // Appropriate implementation called...
}
}
Немного подробнее о методе setTag: Документация по setTag
Надеюсь, это поможет ...