0

I am displaying comments on a post using a recycler adapter. The code is set to scroll the recycler view to the bottom when the edit text is clicked, and when a new comment is posted by the current user.

If the keyboard is shown and the recycler view still does not touches the keyboard (there is like 2 to 4 comments displayed), the app crashes when the comment is posted. If there are many items (enough to go under the keyboard), the recyclerview scrolls and nothing crashes.

This is my code:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // some code
            scrollToBottom = true; // this is initially false
            loadComments();

        }
    });

    private void loadComments () {

        Query query = firebaseFirestore.collection...;

        FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
                .setLifecycleOwner(this)
                .setQuery(query, Model.class)
                .build();

        adapter = new Adapter(options, this);

        recyclerview.setHasFixedSize(true);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(adapter);

        if (scrollToBottom) {
            scrollToBottom = false;
            scrollToTheBottom();
        }

    }

    private void scrollToTheBottom() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerview.smoothScrollToPosition(adapter.getItemCount());
            }
        }, 600);

    }

Adapter code:

public class Adapter extends FirestoreRecyclerAdapter<Model, Adapter.ViewHolder> {

Context context;
TimeAgo timeAgo;

public Adapter(@NonNull FirestoreRecyclerOptions<Model> options, Context context) {
    super(options);
    this.context = context;
}

@Override
protected void onBindViewHolder(@NonNull final ViewHolder holder, int position, @NonNull Model model) {

    final String userID = model.getUser_id();
    String image = model.getImage();
    String username = model.getUsername();
    String comment = model.getComment();
    Timestamp commentTimeAgo = model.getTimestamp();

    String timestampString = String.valueOf(commentTimeAgo);
    String[] noOpeningParentheses = timestampString.split("\\(");
    String[] noClosingParentheses = noOpeningParentheses[1].split("\\)");
    String[] noCommaAndSpace = noClosingParentheses[0].split(", ");
    String[] secondsFromTimestamp = noCommaAndSpace[0].split("seconds=");
    String[] nanosecondsFromTimestamp = noCommaAndSpace[1].split("nanoseconds=");
    long millis = TimeUnit.SECONDS.toMillis(Long.parseLong(secondsFromTimestamp[1])) + TimeUnit.NANOSECONDS.toMillis(Long.parseLong(nanosecondsFromTimestamp[1]));

    // Applying
    if (image.equals("default")) {
        holder.userImage.setImageResource(R.mipmap.no_image);
    } else {
        Glide.with(context).load(image).into(holder.userImage);
    }
    holder.userUsername.setText(username);
    holder.comment.setText(String.valueOf(comment));
    holder.commentTimeAgo.setText(timeAgo.getTimeAgo(context, millis));

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
    context = parent.getContext();
    timeAgo = new TimeAgo();
    return new ViewHolder(view);

}

public class ViewHolder extends RecyclerView.ViewHolder {

    CircleImageView userImage;
    TextView userUsername, comment, commentTimeAgo;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        userImage = itemView.findViewById(R.id.userimage);
        userUsername = itemView.findViewById(R.id.userUsername);
        comment = itemView.findViewById(R.id.comment);
        commentTimeAgo = itemView.findViewById(R.id.timeago);

    }

}

}

When viewing the logcat, I get an error on the 3rd line:

String timestampString = String.valueOf(timestamp);
String[] noOpeningParentheses = timestampString.split("\\(");
String[] noClosingParentheses = noOpeningParentheses[1].split("\\)"); // error here
String[] noCommaAndSpace = noClosingParentheses[0].split(", ");
String[] secondsFromTimestamp = noCommaAndSpace[0].split("seconds=");
String[] nanosecondsFromTimestamp = noCommaAndSpace[1].split("nanoseconds=");
long millis = TimeUnit.SECONDS.toMillis(Long.parseLong(secondsFromTimestamp[1])) + TimeUnit.NANOSECONDS.toMillis(Long.parseLong(nanosecondsFromTimestamp[1]));

This is a code that I wrote for converting a firebase firestore timestamp field to milliseconds.

What I'm getting in logcat is something like java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

I do not know how to solve this. Any help please?

grhtbgvfdc
  • 43
  • 6
  • please post your adapter – tendai May 03 '20 at 00:34
  • @tendai done. you can check it – grhtbgvfdc May 03 '20 at 00:39
  • Are you sure array timestampString.split("\\(") has 2 values? but since its only crashing after you scroll through a lot of items it might be something to do with smoothScroll – tendai May 03 '20 at 00:57
  • @tendai it crashes when there is a couple of items, not if there is a lot of items. – grhtbgvfdc May 03 '20 at 08:54
  • @tendai I tried printing each of the lines of the code for converting the timestamp. When posting a comment the app crashes and I get a message null when printing it. If the items are behind the edittext, I can see the recyclerview scrolling clearly and no crashes are made. But if the items are like 2 or 3 and are still not behind the edittext, this is where the app crashes. – grhtbgvfdc May 03 '20 at 09:25

1 Answers1

1

Replace:

recyclerview.smoothScrollToPosition(adapter.getItemCount());

with:

int lastIndex = adapter.getItemCount()-1;
if(lastIndex!=-1)
{recyclerview.smoothScrollToPosition(lastIndex);}
Tayyab Mazhar
  • 1,560
  • 10
  • 26