开发者

Not Getting OnItemClick Event of ListView

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 mInflat开发者_开发问答er;

    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;}
}


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.


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


check this: ListView with clickable/editable widget


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.


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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜