简体   繁体   中英

Why is my onClicklistener unreachable?

I have the following code in my Adapter in a recyclerview:

public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.ViewHolder> {

    private Context context;
    Cursor dataCursor;

    public MyMovieAdapter(Context context, Cursor cursor,) {
        this.context = context;
        this.dataCursor = cursor;

    }

    @Override
    public MyMovieAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_movies_listrow, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

        dataCursor.moveToPosition(i);
        viewHolder.my_movie_delete_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.e("AAAA", "It doesn't print that log on a button click");
            }
        });

        viewHolder.tv_original_title.setText(dataCursor.getString(1));
        Picasso.with(context).load(dataCursor.getString(9)).resize(150,225).into(viewHolder.img_my_movie);

    }

    @Override
    public int getItemCount() {
        return dataCursor.getCount();
    }



    public class ViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener{
        TextView tv_original_title;
        ImageView img_my_movie;
        Button my_movie_delete_button;

        public ViewHolder(View view) {
            super(view);


            tv_original_title = (TextView)view.findViewById(R.id.tv_original_title);
            img_my_movie = (ImageView)view.findViewById(R.id.img_my_movie);
            my_movie_delete_button = (Button)view.findViewById(R.id.my_movies_delete_button);
            my_movie_delete_button.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {
            Log.e("AAAA", "Neither like that");

        }
    }
    public Cursor swapCursor(Cursor cursor) {
        if (dataCursor == cursor) {
            return null;
        }
        Cursor oldCursor = dataCursor;
        this.dataCursor = cursor;
        if (cursor != null) {
            this.notifyDataSetChanged();
        }
        return oldCursor;
    }
 }

And my layout for a row of my list looks like that:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    card_view:cardCornerRadius="5dp">

    <RelativeLayout
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="150dp"
            android:layout_height="225dp"
            android:id="@+id/img_my_movie"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="20dp"/>

        <TextView
            android:layout_marginTop="10dp"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_original_title"
            android:textStyle="bold"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/img_my_movie" />

        <Button
            android:id="@+id/my_movies_delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="delete"
            android:layout_alignBaseline="@+id/tv_original_title"
            android:layout_alignBottom="@+id/tv_original_title"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

I have try to print a log on a button click two different way as you can see above, without luck. What could be the problem?

Set this to parent of your row layout android:descendantFocusability="blocksDescendants" and Replace your code with following code,

public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.ViewHolder> {

    private Context context;
    Cursor dataCursor;

    public MyMovieAdapter(Context context, Cursor cursor,) {
        this.context = context;
        this.dataCursor = cursor;

    }

    @Override
    public MyMovieAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_movies_listrow, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

        dataCursor.moveToPosition(i);
        viewHolder.my_movie_delete_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.e("AAAA", "It doesn't print that log on a button click");
            }
        });

        viewHolder.tv_original_title.setText(dataCursor.getString(1));
        Picasso.with(context).load(dataCursor.getString(9)).resize(150,225).into(viewHolder.img_my_movie);

    }

    @Override
    public int getItemCount() {
        return dataCursor.getCount();
    }



    public class ViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener{
        TextView tv_original_title;
        ImageView img_my_movie;
        Button my_movie_delete_button;

        public ViewHolder(View view) {
            super(view);


            tv_original_title = (TextView)view.findViewById(R.id.tv_original_title);
            img_my_movie = (ImageView)view.findViewById(R.id.img_my_movie);
            my_movie_delete_button = (Button)view.findViewById(R.id.my_movies_delete_button);


        }

            }
    public Cursor swapCursor(Cursor cursor) {
        if (dataCursor == cursor) {
            return null;
        }
        Cursor oldCursor = dataCursor;
        this.dataCursor = cursor;
        if (cursor != null) {
            this.notifyDataSetChanged();
        }
        return oldCursor;
    }
 }

As many said in the comments above, the problem was with restarting the app. First I went to Monitors in the logcat and closed the running app, and run the app again. The log still didn't appear after restarting the app. Then I uninstalled the app from my device, and run again. The app worked after.

Thanks for the help and your time to help to solve my problem.

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