我在我的適配器下面的代碼在recyclerview:爲什麼我的onClicklistener無法訪問?
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;
}
}
而且我對我的列表的列布局看起來像這樣:
<?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>
我已經嘗試打印日誌上按鈕點擊兩種不同的方式,你可以看到上面,沒有運氣。可能是什麼問題呢?
你會得到什麼樣的錯誤? – 0xDEADC0DE
有時日誌不會被打印退出工作室並重新啓動再次檢查它會工作,但不重新啓動只需退出並重新啓動 –
@ 0xDEADC0DE它不會打印按鈕上的日誌。 – GaborH