0

I need to make my list item clickable instead of individual textview or Image view. My Activity code calling adapter is as below:

@Override
    public void onRequestTaskCompleted(String response) {
        // TODO Auto-generated method stub

        locList = new ArrayList<LocationData>();
        locList = LocationData.getDataFromJSONArray(response);
        list = (ListView) findViewById(R.id.locationlist_listview);

        adapter = new LocationListAdapter(this, locList);

        list.setAdapter(adapter);
    }

Adapter class is as below:

public class LocationListAdapter extends BaseAdapter  {
    Context context;
    ArrayList<LocationData> data;
    LayoutInflater inflater;
    ViewHolder holder;


    private LayoutInflater mLayoutInflater;

    public LocationListAdapter(Context context, ArrayList<LocationData> arrayList
            ) {

        this.context = context;
        data = arrayList;
        // get the layout inflater
        mLayoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        return data.size();
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    private class ViewHolder {

        TextView locName,locAddress;
        ImageView locPic;

    }


    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = mLayoutInflater.inflate(
                    R.layout.locationlist_listitem, null);

            holder.locName = (TextView) convertView
                    .findViewById(R.id.loclistitem_name);
            holder.locAddress = (TextView) convertView
                    .findViewById(R.id.loclistitem_address);
            holder.locPic = (ImageView) convertView
                    .findViewById(R.id.loclistitem_pic);

            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();

        }


        holder.locName.setText(data.get(position).getLocationName());
        holder.locName.setId(data.get(position).getLocationId());
        holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));           

        holder.locPic.setBackgroundResource(R.drawable.songs);

        return convertView;

    }

If is set onclick on any textview its working fine but I need whole list item to be clickable. I have tried list.setOnItemClickListener before call to my adapter from activity but that doesn't work. Below is what I tried

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                LocationData lc = new LocationData();
                lc = locList.get(position);
                int locId = lc.getLocationId();
                AppSession sInstance = AppSession.getInstance();
                sInstance.setLocId(locId);
            }
        });

ListItem XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://splashurl.com/m22ydvb
    android:id="@+id/itemdisplaylist_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/loclist_item_minumum_height"
    android:orientation="horizontal"
    android:weightSum="1" >

    <ImageView
        android:id="@+id/loclistitem_pic"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.2"
        android:contentDescription="@null" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.7"
        android:orientation="vertical"
        android:weightSum="1" >

        <TextView
            android:id="@+id/loclistitem_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:textColor="@color/loclistitem_text"
            android:textIsSelectable="true" >
        </TextView>

        <TextView
            android:id="@+id/loclistitem_address"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:textIsSelectable="true" >
        </TextView>             

</LinearLayout>

Changed GetView method:

@Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = mLayoutInflater.inflate(
                    R.layout.locationlist_listitem, null);

            holder.locName = (TextView) convertView
                    .findViewById(R.id.loclistitem_name);
            holder.locAddress = (TextView) convertView
                    .findViewById(R.id.loclistitem_address);

            holder.locPic = (ImageView) convertView
                    .findViewById(R.id.loclistitem_pic);



            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();

        }
        holder.locName.setTag(position);

        holder.locName.setText(data.get(position).getLocationName());

        holder.locName.setId(data.get(position).getLocationId());
        holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));

        holder.locPic.setBackgroundResource(R.drawable.songs);
        convertView.setOnClickListener(this);

        return convertView;

    }

    @Override
    public void onClick(View v) {
            int position = (Integer)v.findViewById(R.id.loclistitem_name).getTag();
            Log.d("list item clicked on :" , Integer.toString(position));

        }

Also I tried implementing callback from adapter to activity but that didn't work too. May be I wasn't doing it right way.

I have read lot many similar posts and tried numerous things but seems like missing something to implement it the right way.

Please advise.

user3647327
  • 31
  • 2
  • 11
  • Check [This Post](http://stackoverflow.com/questions/20208285/listview-itemclick-not-work/20208788#20208788) dude,i hope this will help you. – Pankaj Arora Jun 03 '14 at 05:55

3 Answers3

0
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
    // TODO Auto-generated method stub

    if (convertView == null) {
        holder = new ViewHolder();

        convertView = mLayoutInflater.inflate(
                R.layout.locationlist_listitem, null);

        holder.locName = (TextView) convertView
                .findViewById(R.id.loclistitem_name);
        holder.locAddress = (TextView) convertView
                .findViewById(R.id.loclistitem_address);
        holder.locPic = (ImageView) convertView
                .findViewById(R.id.loclistitem_pic);

        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();

    }


    holder.locName.setText(data.get(position).getLocationName());
    holder.locName.setId(data.get(position).getLocationId());
    holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));           

    holder.locPic.setBackgroundResource(R.drawable.songs);

    // apply on your getview like this for image view also

    holder.locName.setOnclickListener(){
    }     

    return convertView;

}
Akarsh M
  • 1,629
  • 2
  • 24
  • 47
  • Thanks for the reply but as said I need whole of listitem to be clickable and not just the locname. I understand we can have the whole list item clickable instead of repeating code for each child element in list. – user3647327 Jun 03 '14 at 06:14
0

you can set click listener for convertView in getView method.

use following code:

convertView.setOnClickListener(this); 
// you need implements OnClickListener on your Adapter class or you can use (new onClick.....) instead of `this`

for getting position of clicked item you can use TAG. like following code:

holder.locName.setTag(position);  // paste this after else method in getView

then in onClick method:

@Override
public void onClick(View v) {
        int position = (Integer)v.findViewById(R.id.loclistitem_name).getTag();
        Log.d("list item clicked on :" , Integer.toString(position));

    }

so your getView must be like following code:

 @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = mLayoutInflater.inflate(
                    R.layout.locationlist_listitem, null);

            holder.locName = (TextView) convertView
                    .findViewById(R.id.loclistitem_name);
            holder.locAddress = (TextView) convertView
                    .findViewById(R.id.loclistitem_address);
            holder.locPic = (ImageView) convertView
                    .findViewById(R.id.loclistitem_pic);

            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        holder.locName.setTag(position);

        holder.locName.setText(data.get(position).getLocationName());
        holder.locName.setId(data.get(position).getLocationId());
        holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));           

        holder.locPic.setBackgroundResource(R.drawable.songs);

        convertView.setOnClickListener(this);
        return convertView;

    }

with this way you don't need list.setOnItemClickListener in Activity class.

another way:

whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And so your ListView won't get the chance to accept the click event.

solution:

set your imageButton's attribute:

 android:focusable="false"

or you can add:

 android:descendantFocusability="blocksDescendants" 

to parent layout

Then you can work with list.setOnItemClickListener on activity class

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • Thanks for the reply. I tried as suggested but getting error "The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (LocationListAdapter)" on adding convertView.setOnClickListener(this) – user3647327 Jun 03 '14 at 06:07
  • you need `implements OnClickListener` on your adapter class – Shayan Pourvatan Jun 03 '14 at 06:08
  • Thanks. The error got removed but still no luck. I have edited my post with xml. Not sure where I am going wrong. – user3647327 Jun 03 '14 at 06:27
  • can you post your new code of `getView`? because this must be worked – Shayan Pourvatan Jun 03 '14 at 06:28
  • I had put breakpoint at onClick but the method is not being called on item click – user3647327 Jun 03 '14 at 06:28
  • post the code, i can't find with this way, maybe you forgot one step, check my answer another time and do all step until another way, if not helped put getView method and notify me – Shayan Pourvatan Jun 03 '14 at 06:31
  • did you remove `onItemClicke` on activity class? – Shayan Pourvatan Jun 03 '14 at 06:32
  • Ah..thanks..I removed that now and its hitting the code but only on click of ImageView and not textview. Also getting null pointer exception while getting position value. Lemme check this and come back. – user3647327 Jun 03 '14 at 06:39
  • I don't get you at all , but this must be worked, check one time more – Shayan Pourvatan Jun 03 '14 at 06:43
  • Still stuck. Could you please have a look at my getView Code and suggest. Still its working only on Image click(I have added blocksdescendants in my xml) and getting null pointer at getTag(). – user3647327 Jun 03 '14 at 07:13
  • your problem is `holder.locName.setId(data.get(position).getLocationId());` remove that and test again, as you edit id of textView you can't find in onClick so you get NPE, – Shayan Pourvatan Jun 03 '14 at 07:17
0

in your inflator layout, change the focus to false of all widget like textview or button like:

android:focusable="false"
android:focusableInTouchMode="false"

and in the parent layout of inflator,add:

android:descendantFocusability="blocksDescendants" 

now your listview item is focusable and it works when you click on listitem.

Community
  • 1
  • 1
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • Thanks for the reply. I had tried this earlier too and now again but still the same issue. As suggested in my xml I have set all child elements to focusable="false". Still the list.setOnItemClickListener is not being called on click of item. – user3647327 Jun 03 '14 at 06:12
  • put logs in onitem click and let me know its entering in method or not. – Pankaj Arora Jun 03 '14 at 06:20