Click event not working on button in listview item
Asked Answered
I

3

1

I am using a custom Listview with Custom views as list elements, each list item is a separate Custom view. The problem which I am facing is , click event on button in 1st item of ListView is not getting fired when we click on it. After clicking on it if we click somewhere else in the screen the click event get fires. I am not able to find the solutions for it and I am struggling with it. Any help will be highly appreciated.

Updated with code: Here is the getview method

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (position == 0)
        {
            convertView = this.context.LayoutInflater.Inflate(Resource.Layout.home_hero_container, null);
            this.heroSection = convertView.FindViewById<FrameLayout>(Resource.Id.heroContainer);
            this.setHeroCard();
        }
        else
        {
            convertView = (View)GetItem(position - 1);
        }
        return convertView;
    }

GetItem returns CustomView. 1st item will be the hero layout, after that all the Customviews will be added to Convertview. Click event is not working on the 1st item after hero.

Update with my answer: Instead of assigning Customview directly on to Convertview I inflated a FrameLayout and add Customview to FrameLayout. Now I don't have click issue.

Itinerary answered 2/1, 2015 at 13:43 Comment(6)
Can you please post some codes ?Varied
post code and logcat if you errorsMobley
Try using ViewHolder in your CustomAdapter, This will give Guaranteed click at appropriate positionDivulgence
Where you are setting click listener ?Clara
Click listener will be set inside custom view class itself.Itinerary
Yes @AdityaVyas-LakhanItinerary
M
1

try this it will work

       public class ContactsAdapter extends BaseAdapter {

ArrayList<ContactInfo> mlist;
Context mcontext;


 public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {      
    mlist =  mchtlist;
    mcontext = context;

}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public Object getItem(int postion) {
    return mlist.get(postion);
}

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

@Override
    public View getView(int position, View convertview, ViewGroup viewgroup){
        View view = null;
        if(convertview == null){
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.contactrow, null);

            ContactHolder holder = new ContactHolder();

            holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
            holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
            holder.chkselected = (CheckBox)view.findViewById(R.id.check);

            setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {
        // to open the selected file in resp

              // do your work here
             }});


chkselected .setOnClickListener(new OnClickListener() {
    @Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
        if (chkselected.isChecked())          
                   {            

                    // do your work here
        } else {

 // do your work here                               
        }
    }
});



        view.setTag(holder);

    }
        else{
            view = convertview;
        }
        ContactHolder holder2 = (ContactHolder) view.getTag();
        holder2.txtviewfirstname.setText(list.get(position).firstname);
        holder2.txtviewphone.setText(list.get(position).phonenumber);
        holder2.chkselected.setChecked(list.get(position).selected);
        return view;
    }

   }
Mobley answered 2/1, 2015 at 13:52 Comment(0)
C
1
 public class ContactsAdapter extends BaseAdapter {

ArrayList<ContactInfo> mlist;
Context mcontext;


 public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {      
    mlist =  mchtlist;
    mcontext = context;

}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public Object getItem(int postion) {
    return mlist.get(postion);
}

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

@Override
    public View getView(int position, View convertview, ViewGroup viewgroup){
        View view = null;
        if(convertview == null){
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.contactrow, null);

            ContactHolder holder = new ContactHolder();

            holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
            holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
            holder.chkselected = (CheckBox)view.findViewById(R.id.check);

            holder.chkselected.setOnCheckChangeListener(new CheckchangeListener() );



        view.setTag(holder);

    }
        else{
            view = convertview;
        }
        ContactHolder holder2 = (ContactHolder) view.getTag();
        holder2.txtviewfirstname.setText(list.get(position).firstname);
        holder2.txtviewphone.setText(list.get(position).phonenumber);
        holder2.chkselected.setChecked(list.get(position).selected);
        return view;
    }

    class CheckchangeListener implements OnCheckedChangeListener {


        public CheckchangeListener() {
            // TODO Auto-generated constructor stub



        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                 // do your work here

            } else {
                 // do your work here

            }


        }
    }



   }
Caprine answered 2/1, 2015 at 14:28 Comment(0)
C
-1

You can try to set onClick event in your Custom adapter and if you have time then check this tutorial for reference - http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html

Clara answered 2/1, 2015 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.