项目选择时Android Listview Position为0

时间:2013-02-28 00:18:26

标签: android listview adapter

我的列表视图有一个自定义适配器(扩展ArrayAdapter)。列表加载正常。但是,当我单击列表中的项目(即列表中的第5项)时,getView中的位置始终为0而不是5.它崩溃,因为convertView不再是null如果是0,header变量应初始化为R.id.listHeaderTv TextView,但它无法找到它并最终为空。

如何解决position问题和convertView不再为空?

编辑:检查onItemClick方法后,位置是正确的。唯一的问题是,在onItemClick方法之后,getView被调用并在那里崩溃,因为视图没有被夸大。

logcat的:

02-27 16:35:25.280: E/AndroidRuntime(6330): FATAL EXCEPTION: main
02-27 16:35:25.280: E/AndroidRuntime(6330): java.lang.NullPointerException
02-27 16:35:25.280: E/AndroidRuntime(6330):     at moflow.adapters.CatalogListAdapter.getView(CatalogListAdapter.java:66)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.ListView.fillSpecific(ListView.java:1272)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.ListView.layoutChildren(ListView.java:1558)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.os.Looper.loop(Looper.java:123)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at java.lang.reflect.Method.invoke(Method.java:521)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-27 16:35:25.280: E/AndroidRuntime(6330):     at dalvik.system.NativeStart.main(Native Method)

适配器:

@Override
        public int getCount() {
            return catalog.size();
        }

    @Override
            public View getView( int position, View convertView, ViewGroup parent ) {
                View v = convertView;
                CatalogItem item = getItem( position );

                if ( v == null ) {
                    LayoutInflater inflater = ( LayoutInflater ) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

                    if ( item.header )
                        v = inflater.inflate( R.layout.sec_header, null );
                    else
                        v = inflater.inflate( R.layout.sec_item, null );
                }

                if ( item.header ) {
                    TextView header = ( TextView ) v.findViewById( R.id.listHeaderTV );
                    header.setText( item.name );
                    header.setEnabled( false );
                } else {
                    TextView sectionItem = ( TextView ) v.findViewById( R.id.listCreatureNameTV );
                    sectionItem.setText( item.name );
                }

                return v;
            }

2 个答案:

答案 0 :(得分:2)

getView()应该生成要显示的视图。要获取单击事件,您需要在OnItemClickListener上设置,例如:

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

...并暂时忽略convertView

答案 1 :(得分:1)

嗯,不知道Sam的答案去了哪里,但他正确地重写了这两个方法getItemViewType()和getViewTypeCount()。我也从这个页面得到了帮助:http://androidtrainningcenter.blogspot.com/2012/03/android-listview-with-section-header.html

以下是最终为我工作的代码:

public View getView( int position, View convertView, ViewGroup parent ) {
        ViewHolder holder = null;
        int type = getItemViewType( position );

        if ( convertView == null ) {
            holder = new ViewHolder();
            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();
        }

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

        return convertView;
    }

    public static class ViewHolder {
        public TextView tv;
    }