views:

127

answers:

3

Hi,

I have a ListView that uses different XML files to create Views and make items out of. One of these XML files contains a RatingBar. Everything displays and looks excellent.

I'm trying to attach an onClick handler to the RatingBar to launch a new Activity. My RatingBar is of style ?android:attr/ratingBarStyleSmall; so it's just an indicator (I want the small RatingBar click to take the user to an Activity where they can do various ratings).

My problem is the onClick handler for the RatingBar never gets executed. What makes it more interesting is that I've used the same code to make a LinearLayout clickable and it works fine. Could anyone tell me why?

My Adapter's getView looks as such:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    int type = getItemViewType(position);

    // get the View for this list item
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch (type) {
            // ...
            case TYPE_LOOKUP:
                v = vi.inflate(R.layout.layout_itemlist_itemlookup, parent, false);
                LinearLayout vLookup = (LinearLayout)v.findViewById(R.id.itemlist_lookup);
                if (vStore != null) {
                    vStore.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER WORKS FINE
                                Intent intentLaunchLookup = new Intent(ActivityItemList.this, ActivityLookup.class);
                                startActivity(intentLaunchLookup);
                            }
                        });
                }
                break;
            case TYPE_SEPARATOR:
                v = vi.inflate(R.layout.layout_itemlist_itemseparator, parent, false);
                RatingBar r = (RatingBar)v.findViewById(R.id.itemlist_rating);
                if (r != null) {
                    r.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER DOES NOT GET EXECUTED (r IS NOT NULL; SO THIS SHOULD HAVE BEEN CREATED)
                                Intent intentLaunchRating = new Intent(ActivityItemList.this, ActivityRating.class);
                                startActivity(intentLaunchRating);
                            }
                        });
                }
                break;
            // ...
        }
    }
    // …

  // return the created view
    return v;
}
A: 

In a ListView, you generally have to decide if you want the list view items to be clickable, or an item contained within the listview. Does r.isClickable() return true?

You might have better luck setting clickable to false on the RatingBar, and instead using onClickListener of the listView to deal with the click events on the listView row.

Mayra
I don't want the list item in its entirety to be clickable, but I want individual items inside a list item to be clickable. I've already achieved this with a LinearLayout item. The code is exactly the same and the onClick handler executes perfectly. I don't understand why the RatingBar one doesnt. I'll update my main post to reflect this, 1 moment
Andrew
A: 

I've hacked around this problem by wrapping the RatingBar in a LinearLayout and attaching an onClick listener to the LinearLayout. I don't like doing this, but it works. For some mysterious reason onClick will not execute for a RatingBar.

Andrew
+1  A: 

The reason for setOnClickListener() not working is that RatingBar overrides onTouchEvent() (actually its super class, AbsSeekBar, does) and never let View take care of it, so View#performClick() is never called (which would have called the OnClickListener).

Two possible workarounds:

    derive from RatingBar and override onTouchEvent()
    use OnTouchListener instead, like so:
    ratingBar.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // TODO perform your action here
            }
            return true;
        }
    

HTH, Jonas

jonasb