-3

I've already tried using clickable and focusable but nothing seems to allow list items to be clickable.

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/add_recipe_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/recipe_activity_add"
    android:background="@color/colorPrimary"
    android:textColor="#fff"/>

<ListView
    android:id="@+id/recipe_list"
    android:layout_width="match_parent"
    android:focusable="false"
    android:clickable="true"
    android:layout_height="match_parent" />

java code

recipeListView = (ListView)findViewById(R.id.recipe_list);


    //make viewRecipe Work
    recipeListView.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("MainActivity", "ListView item clicked.");
        }
    });
  • 2
    List items are clickable by default. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Selection/List). – CommonsWare May 07 '17 at 20:33
  • @CommonsWare its not for me – Mcseth Antwi May 07 '17 at 20:34
  • 1
    Try the sample project that I linked to. If the sample project works in your test environment, then you can determine what is different about my project compared to yours. If the sample project does not work in your test environment, then there is some issue with the test environment. – CommonsWare May 07 '17 at 20:37

1 Answers1

0

Android sometimes shows a weird behavior when you are trying to setOnItemClickListner on Listview item while you have set onClickListner on the sub-items of Listview row. Try to check you "Adapter" class and make sure everything is fine and well managed according to the standards. Use ViewHolder to access the sub-items in Adapter class. Here is the sample class of Adapter. Use this one, hope it'll work.

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.TextView;



import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

 /**
 * Created by Zohaib Hassan on 11/28/2016.
 */

 public class InboxAdapter extends ArrayAdapter<InboxRow> {

ArrayList<InboxRow> items;
Context context;


public InboxAdapter(Context context, int resource, ArrayList<InboxRow> items) {
    super(context , resource , items);
    this.context = context;
    this.items = items;

}



@Override

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

    // Get the data item for this position

    InboxRow rowItem = getItem(position);

    // Check if an existing view is being reused, otherwise inflate the view

    ViewHolder viewHolder; // view lookup cache stored in tag

    if (convertView == null) {

        viewHolder = new ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(context);

        convertView = inflater.inflate(R.layout.inbox_row, null);

        viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_user_name_inbox);
        viewHolder.tvMessage = (TextView) convertView.findViewById(R.id.tv_message_inbox);
        viewHolder.tvTimeCount = (TextView) convertView.findViewById(R.id.tv_time_count_inbox);
        viewHolder.userProfilePic = (CircleImageView) convertView.findViewById(R.id.inbox_profile_image);

        convertView.setTag(viewHolder);

    } else {

        viewHolder = (ViewHolder) convertView.getTag();

    }


    /*CircleImageView ivProfileImage Set Background with Picasso*/

    return convertView;

}

private static class ViewHolder {

    TextView tvUserName , tvMessage , tvTimeCount;
    CircleImageView userProfilePic;

}

}

One last thing, please make sure the the "ListView" in your layout class is set to "match-parent" for both height and width.

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11