Click event not working on button in listview item

你离开我真会死。 提交于 2019-12-14 02:42:08

问题


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.


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

   }



回答2:


 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

            }


        }
    }



   }



回答3:


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



来源:https://stackoverflow.com/questions/27743179/click-event-not-working-on-button-in-listview-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!