自定义列表视图中的OnClicklistener不能写入

时间:2014-10-23 11:11:46

标签: java android listview android-listview

我尝试使用自定义列表视图中的点击按钮。 但它不能正常工作。 当我单击按钮来执行(例如,将文本更改为另一个按钮)时,它的工作方式,但它也适用于listview中的其他行! 这是我在课堂上的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;
                   }
                }
            });

和我的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;
    }
}

请帮帮我。

2 个答案:

答案 0 :(得分:0)

您需要返回您单击的行并为该位置设置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

            }
        });

答案 1 :(得分:0)

在你的xml文件中添加以下内容,你已经声明了按钮......这种情况正在发生,因为按钮点击监听器无法工作它实际上是整个listview的工作......

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

希望它有所帮助...