简体   繁体   中英

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 ! this is my onclick listener in class :

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 :

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

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 ...

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

Hope it helps ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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