2017-03-26 94 views
0

我在這裏使用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); 
      } 
     }); 
    } 

回答

1

我認爲你沒有初始化你的viewhHolder中的clickListener。 clicklistener爲空並導致崩潰。您需要通過調用其相應的set方法來設置它的值。

0

更新您的ContactViewHolder如下:

public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener 
{ 
    protected TextView vName; 
    protected TextView vSurname; 
    protected TextView vEmail; 
    protected TextView vTitle; 

    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); 
    } 


    @Override 
    public void onClick(View view) { 
     ItemClickListener.onClick(view, getAdapterPosition(),false); 
    } 

    @Override 
    public boolean onLongClick(View view) { 
     ItemClickListener.onClick(view, getAdapterPosition(), true); 
     return true; 
    } 
} 

要驗證的應用程序存在接收意圖Intent("com.example.as.a.aa_Detail"),你的意圖對象調用resolveActivity()

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"); 

      if (intent.resolveActivity(getPackageManager()) != null) { 
       startActivity(intent); 
      } 
     } 
    }); 

希望這會幫助你解決這個問題。

+0

抱歉,這不是我真正想要的..... thnx for ur help – ghost