Not Getting OnItemClick Event of ListView
Asked Answered
L

6

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;}
}
Laodicea answered 15/3, 2011 at 12:39 Comment(6)
Are there any items in the list. You are setting onItemClick. Can you post the code for OrderAdapter.Dortch
yes there are more than 20 items in list . Here i add my OrderAdapter .Laodicea
list is display in listview..?Sew
yes it is display in listview .Laodicea
getItem should return Util.SampleTest.get(position) but I don't think that's the issue.Dortch
If that answer of mine solved your problem,mark it as an answer please.Ytterbite
Y
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.

Ytterbite answered 9/8, 2012 at 6:7 Comment(0)
C
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

Cheroot answered 13/12, 2011 at 17:23 Comment(0)
R
3

check this: ListView with clickable/editable widget

Robrobaina answered 15/3, 2011 at 13:9 Comment(2)
i m already having this code . but i dont like to do that way .Laodicea
then you apparently won't have onclickitemlistener working :) focusable objects seem to bind onclick events and are placed over the list item. sounds quite reasonableRobrobaina
S
0

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

Sherrell answered 16/3, 2011 at 12:52 Comment(4)
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 activitySherrell
you are using xml for list view controls...?Sherrell
check clickable property is disable or editable property is set of any control of listview...Sherrell
i checked it . i m not declaring any property like this .Laodicea
Y
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.

Yumuk answered 8/2, 2012 at 10:34 Comment(0)
A
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;
}
Abbacy answered 3/1, 2013 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.