可点击的ListView [英] Clickable ListView

查看:113
本文介绍了可点击的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找现在几天的时间,可点击的项目在ListView的解决方案。

I'm looking now a few days for a solution for clickable items in a listView.

首先,我碰到这样的: developer.android.com/resources/articles/touch-mode.html 并发现它是不具有正常onListItemClick()behavouir

First I came across this: developer.android.com/resources/articles/touch-mode.html and found that it's doesn't have the "normal" onListItemClick() behavouir.

然后,我遇到的这code 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 ,而不是一个正常视图,然后当然:

While debugging I noticed the parameter View v is a TextView and not a "normal" View and then of course:

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

returnes ,然后我得到一个NullPointerException异常...

returnes null and I get a NullPointerException...

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

Any ideas why? And how I can solve this?

在此先感谢! :)

推荐答案

你如何创建你的 ClickableListAdapter 的实例?

How do you create your instance of ClickableListAdapter ?

当您创建列表的适配器,你必须通过资源ID 的ViewID ,这应该是一个布局这将在后面膨胀。

When you create your list adapter, you have to pass a resource id viewId, this should be a layout which will be inflated later.

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;

下面,code膨胀传递给构造的XML布局,并调用 createHolder

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

因此​​,请确保实例化时,你的 ClickableListAdapter ,你传递一个布局代替 ID

So make sure that when instantiating your ClickableListAdapter, you pass a layout instead of an id

修改 你必须创建与取自您所提供的链接如下的XML布局:

Edit You have to create a xml layout with the following which is taken from the link you have provided:

<?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 在布局目录中,所以你建立你的适配器:

If you call it mylistrow.xml in the layout directory, so you construct your adapter as :

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

这篇关于可点击的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆