0

I have a ListView made up of a LinearLayout and some elements within. It's driven by a custom Adapter that extends ArrayAdapter>. For some reason it's not getting called when I click on of of the displayed items. Nothing happens in logcat. No error, no Log, nothing.

I've read a number of similar questions, and tried to implement the solutions, but that doesn't seem to be my issue. I tried onItemClickListener not firing on custom ArrayAdapter and Listview onitemclick listener is not working and OnItemClick not responding to clicks

Here's my activity code:

    ratingAdapter = new RatingAdapter(this, RatingRecord);
    ratingListView = (ListView) findViewById(R.id.ratingsListView);
    ratingListView.setAdapter(ratingAdapter);
    ratingListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.d("Ratings update", "Clicked button for " + RatingRecord.get(i).get(1));
            RateRestaurant(RatingRecord.get(i));
        }
    });

Here's my item xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:clickable="true"
    android:id="@+id/ratings_row"
    style="@android:style/Widget.Button"
    android:descendantFocusability="blocksDescendants"
    android:focusable="true">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:layout_weight="2"
            android:id="@+id/rating_name"
            android:focusable="false"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:adjustViewBounds="true"
            android:paddingRight="5dp"
            android:id="@+id/rating_icon"
            android:focusable="false"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:layout_weight="1"
            android:id="@+id/rating_date"
            android:gravity="center_horizontal"
            android:focusable="false"/>

</LinearLayout>

Here's the ListView layout from the main activity:

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/ratingsListView"/>

Here's the meat of the adapter RatingAdapter:

public RatingAdapter(Context context, ArrayList<ArrayList<String>> inValues) {
    super(context, R.layout.ratings_row, inValues);
    this.context = context;
    ratingList = CsvUtil.fromCsvTable(context.getString(R.string.Ratings_OptionsList));
}
    private ViewHolder(View c, Integer n, Integer i, Integer d) {
        NameView = (TextView)c.findViewById(n);
        IconView = (ImageView)c.findViewById(i);
        DateView = (TextView)c.findViewById(d);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    ArrayList<String> record = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder vh;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.ratings_row, parent, false);
        vh = new ViewHolder(convertView,R.id.rating_name,R.id.rating_icon,R.id.rating_date);
        convertView.setTag(vh);
    } else {
        vh = (ViewHolder)convertView.getTag();
    }

    // Lots of data calculation and populating ViewHolder elements

    return convertView;
}

Nothing happens. Like I said, no log or event or error. What have I done wrong?

Community
  • 1
  • 1
Scott
  • 3,663
  • 8
  • 33
  • 56

2 Answers2

0

set on click on your convertview in adapter getView() method.

convertView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Log.d("Ratings update", "Clicked button for " + RatingRecord.get(i).get(1));
        RateRestaurant(RatingRecord.get(i));       
    }
}
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • The adapter doesn't have access to the method or field called in the Listener from the main activity. How would I do this? – Scott Aug 08 '14 at 05:00
0

Answer provided by Haresh in the comments.

"please try to remove this line style="@android:style/Widget.Button" from your list item layout"

I used the style Widget.Button to give me the look of a button though the ListView already provided the click through functionality. However the style did more than change the look. It also overrode the clicking function. To get the effect I wanted without the side effects, I just set the background using:

android:background="@android:drawable/btn_default"
Community
  • 1
  • 1
Scott
  • 3,663
  • 8
  • 33
  • 56