简体   繁体   English

自定义列表视图中的OnClicklistener无法正常工作

[英]OnClicklistener in custom listview doesn't work write

I try to use on click button in custom listview. 我尝试使用自定义列表视图中的单击按钮。 but it doesn't work right. 但这行不通。 when I click button to do (for examlple change text an another button) , its work but it do for another rows in listview too ! 当我单击按钮来执行操作(例如,更改另一个按钮的文本)时,它的工作原理却也适用于listview中的另一行! this is my onclick listener in class : 这是我在课堂上的onclick listener

holder.btnLike.setOnClickListener( new OnClickListener() {
                int clicks = 0;
                @Override
                public void onClick(View arg0) {


                    clicks = clicks + 1;
                    if(clicks == 1){

                    }else if(clicks == 2) {
                        LikeToast.makeText(context, "Liked" , Toast.LENGTH_SHORT).show();
                        holder.btnLike.setText("Liked");
                        Drawable image = context.getResources().getDrawable( R.drawable.ic_action_liked );
                        int hlike = image.getIntrinsicHeight(); 
                        int wlike = image.getIntrinsicWidth();   
                        image.setBounds( 0, 0, wlike, hlike );
                        holder.btnLike.setCompoundDrawables( image, null, null, null );
                        //holder.btnLikes.setBackgroundDrawable(R.drawable.ic_action_liked);
                    } else{
                       clicks = 2;
                   }
                }
            });

and my VoteListAdapter.class : 和我的VoteListAdapter.class

package com.skyline.jimmy;


public class VoteListAdapter extends BaseAdapter {

    ViewHolder holder;
    protected static final String CLIPBOARD_SERVICE = null;
    private final String TAG = "*** VoteListAdapter ***";
    private Context context;
    private ArrayList<Vote> votes;
    int showHideBtn = 0;
    public VoteListAdapter(Context context,List<Vote> voteList) {
        this.context =context;
        this.votes =(ArrayList<Vote>) voteList;

    }

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

    @Override
    public Object getItem(int position) {
        return votes.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder      = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.row, null);
            holder.tvName      = (TextView) convertView.findViewById(R.id.user);
            holder.tvComment   = (TextView) convertView.findViewById(R.id.tvComment);
            holder.tvDate      = (TextView) convertView.findViewById(R.id.tDate);
            holder.tvLikes      = (TextView) convertView.findViewById(R.id.tvLikes);
            holder.ratingBar   = (RatingBar)convertView.findViewById(R.id.ratingBar);
            holder.share   = (ImageButton) convertView.findViewById(R.id.sharebtn);
            holder.copyJoke  = (Button) convertView.findViewById(R.id.copybtn);
            holder.btnLike  = (Button) convertView.findViewById(R.id.likebtn);
            holder.btnLiked  = (Button) convertView.findViewById(R.id.likedbtn);
            holder.btnLike.setTag(position);


            holder.copyJoke.setOnClickListener( new OnClickListener() {

                @Override
                public void onClick(View v) {

                    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                    String Joke = votes.get(position).getComment();
                    clipboard.setText(Joke);

                    Toast toast3 = MyToast.makeText(context, "جک مورد نظر کپی شد", Toast.LENGTH_SHORT);
                    toast3.show();

                }
            });

            holder.btnLike.setOnClickListener( new OnClickListener() {
                int clicks = 0;
                @Override
                public void onClick(View arg0) {


                    clicks = clicks + 1;
                    if(clicks == 1){

                    }else if(clicks == 2) {
                        LikeToast.makeText(context, "Liked" , Toast.LENGTH_SHORT).show();
                        holder.btnLike.setText("Liked");
                        Drawable image = context.getResources().getDrawable( R.drawable.ic_action_liked );
                        int hlike = image.getIntrinsicHeight(); 
                        int wlike = image.getIntrinsicWidth();   
                        image.setBounds( 0, 0, wlike, hlike );
                        holder.btnLike.setCompoundDrawables( image, null, null, null );
                        //holder.btnLikes.setBackgroundDrawable(R.drawable.ic_action_liked);
                    } else{
                       clicks = 2;
                   }
                }
            });


        holder.share.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
               /* Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_TEXT,votes.get(position).getComment());
                try {
                    context.startActivity(Intent.createChooser(i, "Share"));
                } catch (android.content.ActivityNotFoundException ex) {
                    ex.printStackTrace();
                }
               */

                Toast toast = MyToast.makeText(context, "این قسمت در نسخه بتا در حال طراحی است", Toast.LENGTH_LONG);
                toast.show();
                Toast toast2 = MyToast.makeText(context, "منتظر آپدیت باشید", Toast.LENGTH_SHORT);
                toast2.show();
            }
        });


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

        Typeface yaghut=Typeface.createFromAsset(context.getAssets(), "font/Far_Casablanca.ttf");
        String likes = Integer.parseInt(votes.get(position).getRate()) + " likes";
        holder.tvName.setText(votes.get(position).getName());
        holder.tvComment.setText(votes.get(position).getComment());
        holder.tvDate.setText(getFormatedDate(votes.get(position).getPublishDate()));
        holder.tvLikes.setText(likes);
        holder.ratingBar.setRating(Integer.parseInt(votes.get(position).getRate()));
        holder.tvComment.setTypeface(yaghut);


        return convertView;
    }

    private String getFormatedDate(String date) {
        String myDate = null;

        try {
            Date oldDate = new SimpleDateFormat("yy-mm-dd hh:mm:ss").parse(date);
            myDate = new SimpleDateFormat("dd MMM yyyy").format(oldDate);
        } catch (ParseException e) {
            myDate = "";
            e.printStackTrace();
        }

        return myDate;
    }



    static class ViewHolder {
        Button copyJoke;
        ImageButton share;
        Button btnLike;
        Button btnLiked;
        TextView  tvName;
        TextView  tvLikes;
        TextView  tvComment;
        TextView  tvDate;
        RatingBar ratingBar;
    }
}

please help me. 请帮我。

You need to return the row you are clicking on and set the onclick listener for that position, currently your onclick fires for all rows 您需要返回要单击的行,并为该位置设置onclick侦听器,当前您的onclick会触发所有行

l.setAdapter(bAdaptor);

        l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Batches bi = (Batches) parent.getAdapter().getItem(position);

                //Do what you want here

            }
        });

just add below in your xml file where you have declared button .... this is happening because button click listener is not working it's actually working as whole listview's ... 只需在您已声明按钮...的xml文件中添加以下内容,就会发生这种情况,因为按钮单击侦听器无法正常工作,实际上是整个列表视图的工作方式...

android:focusable="false"
android:focusableInTouchMode="false"

Hope it helps ... 希望能帮助到你 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM