我在這裏使用RecycleView
不幸的是我的應用程序崩潰,當我點擊項目從onClickListener開始內部RecyclerView活動
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private Context mContext;
private List<ContactInfo> contactList;
private static OnItemClickListener ItemClickListener;
public ContactAdapter(Context context , List<ContactInfo> contactList) {
this.contactList = contactList;
this.mContext = context;
}
@Override
public int getItemCount() {
return contactList.size();
}
@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
final ContactInfo ci = contactList.get(i);
contactViewHolder.vName.setText(ci.name);
contactViewHolder.vSurname.setText(ci.surname);
contactViewHolder.vEmail.setText(ci.title);
contactViewHolder.vTitle.setText(ci.email);
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.cardview, viewGroup, false);
return new ContactViewHolder(itemView);
}
public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
protected TextView vName;
protected TextView vSurname;
protected TextView vEmail;
protected TextView vTitle;
private OnItemClickListener clickListener;
public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vSurname = (TextView) v.findViewById(R.id.txtSurname);
vEmail = (TextView) v.findViewById(R.id.txtEmail);
vTitle = (TextView) v.findViewById(R.id.title);
v.setTag(v);
v.setOnClickListener(this);
v.setOnLongClickListener(this);
}
public void setClickListener(OnItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
@Override
public void onClick(View view) {
clickListener.onClick(view, getLayoutPosition(),false);
}
@Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getLayoutPosition() , true);
return true;
}
}
public interface OnItemClickListener {
void onClick(View view, int layoutPosition, boolean b);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.ItemClickListener = mItemClickListener;
}
}
這裏是我的MainActivity
,當我嘗試使用SetOnItemClickListener
崩潰
final ContactAdapter ca = new ContactAdapter(getApplicationContext(),createList(30));
recList.setAdapter(ca);
ca.SetOnItemClickListener(new ContactAdapter.OnItemClickListener() {
@Override
public void onClick(View view, int layoutPosition, boolean b) {
Intent intent = new Intent("com.example.as.a.aa_Detail");
startActivity(intent);
}
});
}
抱歉,這不是我真正想要的..... thnx for ur help – ghost