0

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

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.

Then I came across of this 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);  
            }  
        });  

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 null and I get a NullPointerException...

Any ideas why? And how I can solve this?

Thanks in advance! :)

Beasly
  • 1,517
  • 4
  • 20
  • 30

2 Answers2

1

How do you create your instance of ClickableListAdapter ?

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;

Below, the code inflate the xml layout passed to the constructor and call createHolder.

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

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

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>

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);
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • **Actually I think I am doing it with:** adapter = new MyClickableChannelListAdapter(this, android.R.layout.simple_list_item_1, channelList); setListAdapter(adapter); **isn't it?** – Beasly Jan 02 '11 at 15:03
  • Or do I have to do something else to get my own (XML) layout? – Beasly Jan 02 '11 at 15:14
  • Oh my god... I found the issue... so trivial... Creating the adapter here it was the wrong list: dapter = new MyClickableChannelListAdapter(this, R.layout.mylistrow, channelList); setListAdapter(adapter); I mean I used the wrong xml. Thank you for your help... I was searching hours for such a smal thing... – Beasly Jan 02 '11 at 15:29
  • Ok I have one more thing which is related to this... When I select lets say Item 1 and I scroll down and up again Item 2 is selected... I'm investigating it but if anyone knows this issue I'm thankful for any suggestion :) – Beasly Jan 02 '11 at 15:38
  • I havent really looked in details to the implementation but check the different values of `position` passed to `getItemId` (just an idea, otherwise, you should post a new question|) - Good luck. – ccheneson Jan 02 '11 at 15:47
0

List items should be clickable right out of the box. You can check how lists are coded by looking at ApiDemos project code. It should be present on your local machine since it is a part of the SDK. I have it at <root_sdk_folder>\platforms\android-2.0.1\samples\ApiDemos.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
  • As I was writting before... Here: http://www.developer.android.com/resources/articles/touch-mode.html ListViews are not clickable by them selves in Touch mode... I still don't have a solution... Log.d("TEST...","v.getClass(): "+v.getClass().toString()); shows me: TextView Log.d("TEST...","v.getParent().getClass(): "+v.getClass().toString()); Shows Nothing... I'm stuck on this :( – Beasly Jan 02 '11 at 13:37
  • @Beasly: You can also check how list are coded here - http://developer.android.com/intl/zh-CN/resources/samples/ApiDemos/src/com/example/android/apis/view/index.html – Vit Khudenko Jan 02 '11 at 13:45
  • I'm sorry did you read what I just was writing? I have seen this before... I was looking for a solution before posting here... and of course I was going through the API – Beasly Jan 02 '11 at 13:52
  • @Beasly: Sorry for any confusion. I'd just like to say I had not do anything extra to make list items clickable in my projects. This is odd you have such an issue, so I suspect smth is coded in a wrong way. Sorry again if this is not the case for you. – Vit Khudenko Jan 02 '11 at 14:06
  • No problem... I'm open to any suggestion to get it working... The code I resued is on the link – Beasly Jan 02 '11 at 14:27
  • Sorry for being a dumb, I've just realized your needs - you want some sub-view within a list item to have some other click listener than the whole list item has by default... – Vit Khudenko Jan 02 '11 at 14:51