可点击的ListView

时间:2011-01-02 12:37:03

标签: java android listview nullpointerexception

我现在正在寻找一个解决listView中可点击项目的解决方案。

首先我遇到了这个: 的 developer.android.com/resources/articles/touch-mode.html 并发现它没有“正常”onListItemClick()behavouir。

然后我遇到了此代码http://www.androidsnippets.org/snippets/125/

// LINE 296-321

    @Override  
    protected ViewHolder createHolder(View v) {  
        // createHolder will be called only as long, as the ListView is not filled  
        // entirely. That is, where we gain our performance:  
        // We use the relatively costly findViewById() methods and  
        // bind the view's reference to the holder objects.  
        TextView text = (TextView) v.findViewById(R.id.listitem_text);  
        ImageView icon = (ImageView) v.findViewById(R.id.listitem_icon);  
        ViewHolder mvh = new MyViewHolder(text, icon);  

        // Additionally, we make some icons clickable  
        // Mind, that item becomes clickable, when adding a click listener (see API)  
        // so, it is not necessary to use the android:clickable attribute in XML  
        icon.setOnClickListener(new ClickableListAdapter.OnClickListener(mvh) {  

            public void onClick(View v, ViewHolder viewHolder) {  
                // we toggle the enabled state and also switch the icon  
                MyViewHolder mvh = (MyViewHolder) viewHolder;  
                MyData mo = (MyData) mvh.data;  
                mo.enable = !mo.enable; // toggle  
                ImageView icon = (ImageView) v;  
                icon.setImageBitmap(  
                        mo.enable ? ClickableListItemActivity.this.mIconEnabled  
                                : ClickableListItemActivity.this.mIconDisabled);  
            }  
        });  

调试时我注意到参数查看v TextView 而不是“普通”视图,然后当然:

TextView text = (TextView) v.findViewById(R.id.listitem_text);

返回 null ,我得到一个NullPointerException ...

任何想法为什么?我怎么解决这个问题?

提前致谢! :)

2 个答案:

答案 0 :(得分:1)

如何创建ClickableListAdapter的实例?

创建列表适配器时,您必须传递资源ID viewId,这应该是layout,稍后会被充气。

public ClickableListAdapter(Context context, int viewid, List objects) {  

        // Cache the LayoutInflate to avoid asking for a new one each time.  
        mInflater = LayoutInflater.from(context);  
        mDataObjects = objects;  
        mViewId = viewid;

下面,代码会夸大传递给构造函数的xml布局并调用createHolder

view = mInflater.inflate(mViewId, null);  
// call the user's implementation  
holder = createHolder(view); 

因此,请确保在实例化ClickableListAdapter时,您传递了layout而不是id

修改 您必须使用以下内容创建xml布局,该布局取自您提供的链接:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:orientation="horizontal"  
  android:gravity="center_vertical"  
  >  

<TextView android:text="Text" android:id="@+id/listitem_text"  
            android:layout_weight="1"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"  
            ></TextView>  
<ImageView android:id="@+id/listitem_icon"  
            android:src="@drawable/globe2_32x32"  
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"  
            android:maxWidth="32px"  
            android:maxHeight="32px"  
            >  
</ImageView>  
</LinearLayout>

如果在布局目录中将其命名为mylistrow.xml,那么将适配器构造为:

adapter = new MyClickableChannelListAdapter(this, R.layout.mylistrow, channelList); 
setListAdapter(adapter);

答案 1 :(得分:0)

列表项应该可以直接点击。您可以通过查看ApiDemos项目代码来检查列表的编码方式。它应该出现在本地计算机上,因为它是SDK的一部分。我在<root_sdk_folder>\platforms\android-2.0.1\samples\ApiDemos