6

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;}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Chirag
  • 56,621
  • 29
  • 151
  • 198

6 Answers6

19

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)

You can also refer to the comments here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nezam
  • 4,122
  • 3
  • 32
  • 49
5

If you have clickable items in your list, you have to play with the focus to be able to receive both list item clicked event AND list items children click events.

Call the following code on your list items as they are created:

listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

Found on http://code.google.com/p/android/issues/detail?id=3414, answer #27

Benjamin Piette
  • 3,645
  • 1
  • 27
  • 24
3

check this: ListView with clickable/editable widget

Community
  • 1
  • 1
Alex
  • 536
  • 1
  • 5
  • 13
  • i m already having this code . but i dont like to do that way . – Chirag Mar 15 '11 at 13:13
  • then you apparently won't have onclickitemlistener working :) focusable objects seem to bind onclick events and are placed over the list item. sounds quite reasonable – Alex Mar 15 '11 at 13:43
0

try this.

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

        holder.txtTest = (TextView) convertView.findViewById(R.id.txtTest);
        convertView.setClickable(true);
        convertView.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
         }
       }

        convertView.setTag(holder);

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

    holder.txtTest.setText(Util.SampleTest.get(position));
                return convertView;
}
Harshid
  • 5,701
  • 4
  • 37
  • 50
0

First, Set Adapter and then set on click listener event on listview. Then try again.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
SBJ
  • 4,089
  • 3
  • 24
  • 27
  • if not than .. @Override public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } this function writes out of create method and implements OnClickListener in your activity – SBJ Mar 16 '11 at 12:56
  • you are using xml for list view controls...? – SBJ Mar 16 '11 at 13:08
  • check clickable property is disable or editable property is set of any control of listview... – SBJ Mar 16 '11 at 13:33
  • i checked it . i m not declaring any property like this . – Chirag Mar 16 '11 at 13:55
0

Make sure your custom layout does not have a CheckBox before the would-be TextView. You can use an ImageView to fulfill the CheckBox functionality.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Miran
  • 1