1

請幫我在以後的關於如何使用viewholder或如何使用if and else聲明禁用的EditText的Android

我有一個回收視圖,它做的另一種方式來禁用editext我的噩夢列出所有名稱及其消息。 (使者就像)。不知怎的,回收視圖包括姓名,消息和一個小標籤TextView的「客戶關閉」如果他們將要點擊該標籤組成的回收視圖「客戶關閉」,他們將不能夠因爲它已關閉發送消息。否則,所有包含「客戶關閉」標籤的回收站視圖的editext設置爲false。

public void bind(final Account account, final FirebaseChat chat) { 
    itemView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Context context = itemView.getContext(); 
      if (context instanceof NavigationActivity) { 
       final Activity activity = (Activity) context; 

       final Intent intent = new Intent(itemView.getContext(), myChat.class); 
       intent.putExtra(ChatActivity.KEY_NEW, false); 
       intent.putExtra(ChatActivity.KEY_ACCOUNT, account); 
       intent.putExtra(ChatActivity.KEY_CHAT, chat); 

       activity.startActivity(intent); 
      } 
     } 
    }); 

    cName.setText(chat.getName()); 
    cName.setTypeface(chat.getReadCount() < chat.getNumMessages() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); 
    cTextDate.setText(getFormattedDate(chat.getLastTime())); 
    cTextMessage.setText(chat.getLastMessage()); 
    cTextMessage.setTypeface(chat.getReadCount() < chat.getNumMessages() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); 



} 




public void bind(final Account account, final FirebaseChatInfo customerInfo) { 
    itemView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Context context = itemView.getContext(); 
      if (context instanceof NavigationActivity) { 
       final Activity activity = (Activity) context; 
       final Intent intent = new Intent(itemView.getContext(), myChat.class); 
       intent.putExtra(ChatActivity.KEY_NEW, false); 
       intent.putExtra(ChatActivity.KEY_ACCOUNT, account); 
       intent.putExtra(ChatActivity.KEY_CHAT_INFO, customerInfo); 

       activity.startActivity(intent); 
      } 
     } 
    }); 




    cName.setText(chatInfo.getName()); 
    cName.setTypeface(chatInfo.isLastVisitorMessaged() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); 
    cTextDate.setText(getFormattedDate(chatInfo.getLastTime())); 
    cTextMessage.setText(chatInfo.getLastMessage()); 
    cMessage.setTypeface(chatInfo.isLastVisitorMessaged() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); 

    this.shoptag = (TextView) itemView.findViewById(R.id.shoptag); 
    shoptag.setText(chatInfo.getShop_id()); 
    this.customerclosed = (TextView) itemView.findViewById(R.id.customer_closed); 
    this.customerclosed.setVisibility(chatInfo.isClosed() ? View.GONE : View.VISIBLE); 
    this.message_editext = (EditText)itemView.findViewById(R.id.message_editext); 

    message_text.setEnabled(!chatInfo.isClosed()); 

    if(customerInfo.isClosed()) 
    { 

     message_text.setEnabled(false); 


    }else { 

     message_textt.setEnabled(true); 
    } 

} 

我得到這個錯誤

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setEnabled(boolean)' on a null object reference 

回答

2

所有你需要做的僅僅是添加其他標籤

if(customerInfo.isClosed){ 
     message_editext.setEnabled(false); 
    } else { 
     message_editext.setEnabled(true); 
    } 
+2

在單行:(!customerInfo.isClosed)message_editext.setEnabled; – AnixPasBesoin

+0

@AnixPasBesoin我也試過了代碼,但沒有任何事情發生,如果我打算把別人是否考慮customerInfo的持有人,不幸的是它不會承認的message_editext, 因爲message_editext犯規屬於customerInfo持有人,即使製作的EditText保護不起作用:\' – kris

+0

@kris請提供您的適配器類的完整代碼。 – Aks4125