Not Getting OnItemClick Event of ListView

Chirag picture Chirag · Mar 15, 2011 · Viewed 12.6k times · Source

I'm using one custom listview . When I'm click on listview I didn't getting onClick Event .

Here is my code .

        lvlList = (ListView)findViewById(R.id.lvlList);
        lvlList.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> a, View v,int position, long id) 
            {
                Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
            }
        });

lvlList.setAdapter(new OrderAdapter(getBaseContext()));

OrderAdapter

private class OrderAdapter extends BaseAdapter
{
    private LayoutInflater mInflater;

    public OrderAdapter(Context context) 
    {
        mInflater = LayoutInflater.from(context);
    }

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

        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.example, null);
            holder = new ViewHolder();

            holder.txtTest = (TextView) convertView.findViewById(R.id.txtTest);

            convertView.setTag(holder);
        } 
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtTest.setText(Util.SampleTest.get(position));
                    return convertView;
    }

    public class ViewHolder 
    {
        public TextView txtTets;
    }

    public int getCount(){return Util.SampleTest.size();}

    public Object getItem(int position){return position;}

    public long getItemId(int position){return position;}
}

Answer

Nezam picture Nezam · Aug 9, 2012

You need to set android:descendantFocusability="blocksDescendants" in your custom xml layout file for the LinearLayout or whatever layout you've been using. (for defining your custom row)

That should solve your problem.Because it did solve mine.If it solves kindly mark my post as your answer.

You can also refer the comments here