2017-07-29 7 views
0

commentClassListenerでonChickRemovedメソッドを呼び出すには、内部クラスメソッドであるので、setOnclickListnerをどこに置いてもエラーが発生しますので、を教えてくださいこのボタンにsetOnclickListnerので、このメソッド/関数は、コメントを削除作業を呼び出すか、行うことができますButton.setOnclickListnerでアダプタの関数を呼び出す

package com.ecbclass.database; 

public class PostDetailActivity extends AppCompatActivity implements View.OnClickListener { 

    private static final String TAG = "PostDetailActivity"; 
    public static final String EXTRA_POST_KEY = "post_key"; 

    private DatabaseReference mPostReference; 
    private DatabaseReference mCommentsReference; 
    private ValueEventListener mPostListener; 
    private String mPostKey; 
    private CommentAdapter mAdapter; 
    private TextView numStarsView; 
    private TextView mAuthorView; 
    private ImageView mAuthorPhotoView; 
    private TextView mTitleView; 
    private TextView mBodyView; 
    private EditText mCommentField; 
    private Button mCommentButton; 
    private RecyclerView mCommentsRecycler; 
    private TextView mStreamView; 
    private TextView mSubjectView; 
    private static ProgressDialog progress; 
    private LinearLayoutManager mCommentManager; 
    private Button commentDelete; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_post_detail); 

     //progress bar for network chek 
     progress = new ProgressDialog(this); 
     progress.setIndeterminate(true); 
     progress.setCancelable(false); 
     progress.setMessage("Server not reachable.Check internet Connection"); 
     progress.setProgressStyle(ProgressDialog.STYLE_SPINNER); 

     // Get post key from intent 
     mPostKey = getIntent().getStringExtra(EXTRA_POST_KEY); 
     if (mPostKey == null) { 
      throw new IllegalArgumentException("Must pass EXTRA_POST_KEY"); 
     } 

     // Initialize Database 
     mPostReference = FirebaseDatabase.getInstance().getReference() 
       .child("posts").child(mPostKey); 
     mCommentsReference = FirebaseDatabase.getInstance().getReference() 
       .child("post-comments").child(mPostKey); 

     // Initialize Views 
     mAuthorView = (TextView) findViewById(R.id.post_author); 
     mAuthorPhotoView = (ImageView) findViewById(R.id.post_author_photo); 
     mTitleView = (TextView) findViewById(R.id.post_title); 

     mBodyView = (TextView) findViewById(R.id.post_body); 
     mBodyView.setScroller(new Scroller(this)); 
     mBodyView.setMaxLines(4); 
     mBodyView.setVerticalScrollBarEnabled(true); 
     mBodyView.setMovementMethod(new ScrollingMovementMethod()); 

     numStarsView = (TextView) findViewById(R.id.post_num_stars_cmnt); 
     mStreamView = (TextView) findViewById(R.id.post_stream); 
     mSubjectView = (TextView) findViewById(R.id.post_subject); 

     mCommentField = (EditText) findViewById(R.id.field_comment_text); 
     mCommentButton = (Button) findViewById(R.id.button_post_comment); 
     mCommentsRecycler = (RecyclerView) findViewById(R.id.recycler_comments); 
     mCommentButton.setOnClickListener(this); 
     mCommentManager = new LinearLayoutManager(this); 
     mCommentManager.setReverseLayout(true); 
     mCommentManager.setStackFromEnd(true); 
     mCommentsRecycler.setLayoutManager(mCommentManager); 

     commentDelete = (Button) findViewById(R.id.comment_delete); 

     isOnlne(); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     isOnlne(); 
     // Add value event listener to the post 
     // [START post_value_event_listener] 
     ValueEventListener postListener = new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       // Get Post object and use the values to update the UI 
       Post post = dataSnapshot.getValue(Post.class); 
       // [START_EXCLUDE] 
       setTitle(post.title); 
       mAuthorView.setText(post.author); 
       mTitleView.setText(post.title); 
       mBodyView.setText(post.body); 
       mStreamView.setText(post.stream); 
       mSubjectView.setText(post.subject); 
       if (post.userImage.equalsIgnoreCase("phone")) { 
        mAuthorPhotoView.setImageResource(R.drawable.blue_call_icon); 

       } else { 
        Picasso.with(PostDetailActivity.this).load(post.userImage) 
          .placeholder(R.drawable.google) 
          .error(R.drawable.ic_action_account_circle_40) 
          .into(mAuthorPhotoView); 
       } 
       // [END_EXCLUDE] 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       // Getting Post failed, log a message 
       Log.w(TAG, "loadPost:onCancelled", databaseError.toException()); 
       // [START_EXCLUDE] 
       Toast.makeText(PostDetailActivity.this, "Failed to load post.", 
         Toast.LENGTH_SHORT).show(); 
       // [END_EXCLUDE] 
      } 
     }; 
     mPostReference.addValueEventListener(postListener); 
     // [END post_value_event_listener] 

     // Keep copy of post listener so we can remove it when app stops 
     mPostListener = postListener; 

     // Listen for comments 
     mAdapter = new CommentAdapter(this, mCommentsReference); 
     mCommentsRecycler.setAdapter(mAdapter); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // Remove post value event listener 
     if (mPostListener != null) { 
      mPostReference.removeEventListener(mPostListener); 
     } 

     // Clean up comments listener 
     mAdapter.cleanupListener(); 
    } 

    @Override 
    public void onClick(View v) { 
     int i = v.getId(); 
     if (i == R.id.button_post_comment) { 
      postComment(); 
     } 
    } 

    public String getUid() { 

     String uid = FirebaseAuth.getInstance().getCurrentUser().getUid(); 
     if (uid == null) { 
      Intent intent = new Intent(getApplicationContext(), Login.class); 
      startActivity(intent); 
     } 
     return uid; 
    } 

    private void postComment() { 
     final String uid = getUid(); 
     final String userImage; 
     FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser(); 
     String str = currentUser.getEmail(); 
     if (str != null && str != "") { 
      userImage = currentUser.getPhotoUrl().toString(); 
     } else { 
      userImage = "phone"; 
     } 
     FirebaseDatabase.getInstance().getReference().child("users").child(uid) 
       .addListenerForSingleValueEvent(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         // Get user information 
         User user = dataSnapshot.getValue(User.class); 
         String authorName = user.username; 

         // Create new comment object 
         String commentText = mCommentField.getText().toString(); 
         if (TextUtils.isEmpty(commentText)) { 
          mCommentField.setError("comment shouldn't empty"); 
          return; 
         } 
         isOnlne(); 
         Comment comment = new Comment(uid, authorName, commentText, userImage); 

         // Push the comment, it will appear in the list 
         mCommentsReference.push().setValue(comment); 
         // Clear the field 
         mCommentField.setText(null); 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 
    } 

    private static class CommentViewHolder extends RecyclerView.ViewHolder { 

     public TextView authorView; 
     public TextView bodyView; 
     public ImageView authorPhotoView; 

     public CommentViewHolder(final View itemView) { 
      super(itemView); 
      authorPhotoView = (ImageView) itemView.findViewById(R.id.comment_photo); 
      authorView = (TextView) itemView.findViewById(R.id.comment_author); 
      bodyView = (TextView) itemView.findViewById(R.id.comment_body); 
     } 
    } 

    private class CommentAdapter extends RecyclerView.Adapter<CommentViewHolder> { 

     private Context mContext; 
     private DatabaseReference mDatabaseReference; 
     private ChildEventListener mChildEventListener; 

     private List<String> mCommentIds = new ArrayList<>(); 
     private List<Comment> mComments = new ArrayList<>(); 


     public CommentAdapter(final Context context, DatabaseReference ref) { 
      mContext = context; 
      mDatabaseReference = ref; 

      // Create child event listener 
      // [START child_event_listener_recycler] 
      ChildEventListener childEventListener = new ChildEventListener() { 
       @Override 
       public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { 
        Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey()); 

        // A new comment has been added, add it to the displayed list 
        Comment comment = dataSnapshot.getValue(Comment.class); 

        // [START_EXCLUDE] 
        // Update RecyclerView 
        mCommentIds.add(dataSnapshot.getKey()); 
        mComments.add(comment); 
        notifyItemInserted(mComments.size() - 1); 
        mCommentManager.scrollToPosition(mComments.size() - 1); 
        // [END_EXCLUDE] 
       } 

       @Override 
       public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) { 
        Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey()); 

        // A comment has changed, use the key to determine if we are displaying this 
        // comment and if so displayed the changed comment. 
        Comment newComment = dataSnapshot.getValue(Comment.class); 
        String commentKey = dataSnapshot.getKey(); 

        // [START_EXCLUDE] 
        int commentIndex = mCommentIds.indexOf(commentKey); 
        if (commentIndex > -1) { 
         // Replace with the new data 
         mComments.set(commentIndex, newComment); 

         // Update the RecyclerView 
         notifyItemChanged(commentIndex); 
         mCommentManager.scrollToPosition(commentIndex); 
        } else { 
         Log.w(TAG, "onChildChanged:unknown_child:" + commentKey); 
        } 
        // [END_EXCLUDE] 
       } 

       ******@Override 
       public void onChildRemoved(DataSnapshot dataSnapshot) { 
        Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey()); 
        // A comment has changed, use the key to determine if we are displaying this 
        // comment and if so remove it. 
        String commentKey = dataSnapshot.getKey(); 

        // [START_EXCLUDE] 
        int commentIndex = mCommentIds.indexOf(commentKey); 
        if (commentIndex > -1) { 
         // Remove data from the list 
         mCommentIds.remove(commentIndex); 
         mComments.remove(commentIndex); 

         // Update the RecyclerView 
         notifyItemRemoved(commentIndex); 
         mCommentManager.scrollToPosition(commentIndex); 
        } else { 
         Log.w(TAG, "onChildRemoved:unknown_child:" + commentKey); 
        } 
        // [END_EXCLUDE] 
       }******* 

       @Override 
       public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) { 
        Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey()); 

        // A comment has changed position, use the key to determine if we are 
        // displaying this comment and if so move it. 
        Comment movedComment = dataSnapshot.getValue(Comment.class); 
        String commentKey = dataSnapshot.getKey(); 

        // ... 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 
        Log.w(TAG, "postComments:onCancelled", databaseError.toException()); 
        Toast.makeText(mContext, "Failed to load comments.", 
          Toast.LENGTH_SHORT).show(); 
       } 
      }; 
      ref.addChildEventListener(childEventListener); 
      // [END child_event_listener_recycler] 

      // Store reference to listener so it can be removed on app stop 
      mChildEventListener = childEventListener; 
     } 

     @Override 
     public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      LayoutInflater inflater = LayoutInflater.from(mContext); 
      View view = inflater.inflate(R.layout.item_comment, parent, false); 
      return new CommentViewHolder(view); 
     } 

     @Override 
     public void onBindViewHolder(CommentViewHolder holder, int position) { 
      Comment comment = mComments.get(position); 
      holder.authorView.setText(comment.author); 
      holder.bodyView.setText(comment.text); 
      if (comment.userImage.equalsIgnoreCase("phone")) { 
       holder.authorPhotoView.setImageResource(R.drawable.blue_call_icon); 

      } else { 
       Picasso.with(mContext).load(comment.userImage) 
         .placeholder(R.drawable.google) 
         .error(R.drawable.ic_action_account_circle_40) 
         .into(holder.authorPhotoView); 
      } 


     } 

     @Override 
     public int getItemCount() { 
      numStarsView.setText(String.valueOf(mComments.size())); 
      return mComments.size(); 
     } 

     public void cleanupListener() { 
      if (mChildEventListener != null) { 
       mDatabaseReference.removeEventListener(mChildEventListener); 
      } 
     } 

    } 

    public void isOnlne() { 
     ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
     if (networkInfo != null && networkInfo.isConnected()) { 
      changeStatus(true); 
     } else { 
      changeStatus(false); 
     } 
    } 

    // Method to change the text status 
    public void changeStatus(boolean isConnected) { 
     // Change status according to boolean value 
     if (isConnected) { 
      progress.dismiss(); 
     } else { 
      progress.show(); 
     } 
    } 

    @Override 
    protected void onPause() { 

     super.onPause(); 
     NetworkCheker.activityPaused();// On Pause notify the Application 
    } 

    @Override 
    protected void onResume() { 

     super.onResume(); 
     NetworkCheker.activityResumed();// On Resume notify the Application 
    } 
} 

答えて

0
commentDelete.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     mAdapter.onChildRemoved(dataSnapshot); 
    } 
}); 
+0

そして**私はそれを置くどこでも私のfile.Becauseに**このコードを配置する必要があり、それは** mAdpter **や** datasnapshotにエラーを与える** –

+0

@UttamMeerwalあなたはonCreateのPostDetailActivityに入れて、関数にdataSnapshotの値を渡す必要があります –

0

まずあなたCommonConstructorのコンストラクタブラケットを閉じます。

commentDelete.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
     mAdapter.onChildRemoved(yourdatasnapshot); 


} 

`

関連する問題