Skip to content Skip to sidebar Skip to footer

Android Listview Position Is 0 On Item Selection

I have a custom adapter for my list view (extends ArrayAdapter). The list loads fine. However, when I click on an item in my list, i.e. the 5th item in the list, the position in ge

Solution 1:

getView() is supposed to produce the view to be displayed. To get the click events, you'll want set on OnItemClickListener such as this:

new OnItemClickListener() {
    // @OverridepublicvoidonItemClick(AdapterView<?> a, View v, int position, long id) {
         Toast.makeText(ListRecords.this,"Clicked item: " + position, Toast.LENGTH_LONG).show();
     }
     });

... and ignore convertView for the time being.

Solution 2:

Hm, don't know where Sam's answer went but he was correct about overriding those two methods getItemViewType() and getViewTypeCount(). I also got help from this page: http://androidtrainningcenter.blogspot.com/2012/03/android-listview-with-section-header.html

Here's the code that ended up working for me:

public View getView( int position, View convertView, ViewGroup parent ) {
        ViewHolderholder=null;
        inttype= getItemViewType( position );

        if ( convertView == null ) {
            holder = newViewHolder();
            switch( type ) {
                case TYPE_ITEM:
                    convertView = inflater.inflate( R.layout.sec_item, null );
                    holder.tv = ( TextView ) convertView.findViewById( R.id.listCreatureNameTV );
                    break;
                case TYPE_SEPARATOR:
                    convertView = inflater.inflate( R.layout.sec_header, null );
                    holder.tv = ( TextView ) convertView.findViewById( R.id.listHeaderTV );
                    break;
            }
            convertView.setTag( holder );
        } else {
            holder = ( ViewHolder ) convertView.getTag();
        }

        CatalogItemitem= getItem( position );
        holder.tv.setText( item.name );

        return convertView;
    }

    publicstaticclassViewHolder {
        public TextView tv;
    }

Post a Comment for "Android Listview Position Is 0 On Item Selection"