Sauron Sauron - 28 days ago 18
Android Question

Linear Layout Cannot be Cast to RecyclerView onClickItemListener

I am attempting to implement a onClickListener to allow users to click on the RecyclerView list of their friends. However, upon clicking the user list item, it simply returns the error:

01-18 20:39:37.317 3413-3413/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.feastappv2, PID: 3413
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:3411)
at android.support.v7.widget.RecyclerView.getChildLayoutPosition(RecyclerView.java:3445)
at com.example.android.feastappv2.feastapp.Friends.FriendFragment$ContactViewHolder.onClick(FriendFragment.java:73)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21173)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6840)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)


The line of issue is within:

@Override
public void onClick(View view) {
int itemPosition = mRecyclerView.getChildLayoutPosition(view);
Toast.makeText(getContext(),
"Position is: " + String.valueOf(itemPosition),
Toast.LENGTH_LONG).show();
}


within my class:

public class FriendFragment extends Fragment {

RecyclerView mRecyclerView;
ArrayList<Friend> mContacts;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.recycler_layout, container, false);

mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_layout);
mRecyclerView.setHasFixedSize(true);

mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

mContacts = FriendProvider.getInstance(getActivity()).getFriends();
mRecyclerView.setAdapter(new ContactAdapter(mContacts));

return view;
}

// Inner Class

private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private final TextView mName;
private final TextView mNumber;
private Friend mContact;

public ContactViewHolder(View itemView) {
super(itemView);

mName = (TextView) itemView.findViewById(R.id.tv_name);
mNumber = (TextView) itemView.findViewById(R.id.tv_number);

mName.setOnClickListener(this);
}

public void bindContact(Friend contact){
mContact = contact;
mName.setText(contact.getFirstName().toString() + " " + contact.getLastName());
mNumber.setText(contact.getFriendChannelID().toString());
}

@Override
public void onClick(View view) {
int itemPosition = mRecyclerView.getChildLayoutPosition(view);
Toast.makeText(getContext(), "Position is: " + String.valueOf(itemPosition), Toast.LENGTH_LONG).show();
}

}

class ContactAdapter extends RecyclerView.Adapter<ContactViewHolder> {
ArrayList<Friend> contactList;

ContactAdapter(ArrayList<Friend> object){
this.contactList = object;
}

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_item, parent, false);

return new ContactViewHolder(view);
}

@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
Friend contact = contactList.get(position);
holder.bindContact(contact);
}

@Override
public int getItemCount() {
return contactList.size();
}
}
}

Answer

If you want to get the position of the item clicked, you can get the position of the adapter by using

int itemPosition = getAdapterPosition();

inside your OnClick method

Comments