RecyclerView doesn't Update until Soft Keyboard is Open/Close - android

I have a Posts Feed (Something like Facebook News Feed) Activity and in it's layout I have an EditText, share Button at the top and a RecyclerView below it. The RecyclerView shows the Posts of all users by getting all the Posts from Server and binding data through Adapter in OnCreate Method.
When a User post something by typing some text in EditText and hit share Button, the data is sent to Server (I am using Retrofit) and upon Successful Response from Server I call the same function which call in OnCreate Method to show all Posts to update the RecyclerView.
The Problem I am facing is that the data is posted to Server, but the Layout only gets updated only when I press the back button to Hide/Close the Keyboard after typing or Show/Open the Keyboard by tapping the EditText.
Following is Some of the Code for better understanding:
Here I Send Request to Server when User Post something:
Call<CreatePost> call = mAPIService.sharePost(apiKey, post_description);
call.enqueue(new Callback<CreatePost>() {
#Override
public void onResponse(#NonNull Call<CreatePost> call, #NonNull Response<CreatePost> response) {
boolean error = response.body().getError();
if (!error) {
displayFeedPosts();
}
}
#Override
public void onFailure(#NonNull Call<CreatePost> call, #NonNull Throwable t) {
Log.d(TAG, "Error: " + t.getMessage());
}
});
Here Is displayFeedPosts Method:
private void displayFeedPosts() {
Call<FeedPosts> call = mAPIService.displayFeedPosts(apiKey);
call.enqueue(new Callback<FeedPosts>() {
#Override
public void onResponse(#NonNull Call<FeedPosts> call, #NonNull Response<FeedPosts> response) {
boolean error = response.body().getError();
if (!error) {
ArrayList<Post> feedPosts = response.body().getPosts();
for (Post post : feedPosts) {
mTripFeedPostUserNames.add(post.getFirstName() + " " + post.getLastName());
mTripFeedPostTime.add(post.getPostDatetime());
mTripFeedPostContent.add(post.getPostDescription());
}
}
}
#Override
public void onFailure(#NonNull Call<FeedPosts> call, #NonNull Throwable t) {
Log.d(TAG, "Error: " + t.getMessage());
}
});
initRecyclerView();
}
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);
RecyclerView recyclerView = findViewById(R.id.trip_feed_recycler_view);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(layoutManager);
mTripFeedPostRecyclerViewAdapter = new TripFeedPostRecyclerViewAdapter(this, mTripFeedPostProfileImages, mTripFeedPostUserNames, mTripFeedPostTime, mTripFeedPostContent, mTripFeedPostImages, mTripFeedPostID);
recyclerView.setAdapter(mTripFeedPostRecyclerViewAdapter);
}
PS: I am new to Android, I apologise if I have done things the wrong way. Your suggestions are Welcomed.
Note: The same Question has been asked regarding ListView here and here but it doesn't solved my problem

I solved the above problem with the help of Arsalan Khan Answer.
Actually the initRecyclerView() was not executing outside the onResponse() method. Including it inside the onResponse() method and using notifyDataSetChanged() instead of initRecyclerView() solved the problem.
The solution to data being populated twice I solved it by modifying the displayFeedPosts() in the following way:
private void updateFeedPosts() {
Call<FeedPosts> call = mAPIService.displayFeedPosts(apiKey);
call.enqueue(new Callback<FeedPosts>() {
#Override
public void onResponse(#NonNull Call<FeedPosts> call, #NonNull Response<FeedPosts> response) {
boolean error = response.body().getError();
if (!error) {
mTripFeedPostProfileImages.clear();
mTripFeedPostUserNames.clear();
mTripFeedPostTime.clear();
mTripFeedPostContent.clear();
mTripFeedPostImages.clear();
mTripFeedPostID.clear();
mTripFeedPostRecyclerViewAdapter.notifyDataSetChanged();
ArrayList<Post> feedPosts = response.body().getPosts();
for (Post post : feedPosts) {
mTripFeedPostProfileImages.add(post.getProfilePicture());
mTripFeedPostUserNames.add(post.getFirstName() + " " + post.getLastName());
mTripFeedPostTime.add(post.getPostDatetime());
mTripFeedPostContent.add(post.getPostDescription());
mTripFeedPostImages.add(post.getPostImagePath());
mTripFeedPostID.add(post.getPostTripfeedId());
}
mTripFeedPostRecyclerViewAdapter.notifyDataSetChanged();
}
}
#Override
public void onFailure(#NonNull Call<FeedPosts> call, #NonNull Throwable t) {
Log.d(TAG, "Error: " + t.getMessage());
}
});
}
I cleared the Adapter Data before populating it again with updated data.
I hope my answer will help someone having the same problem.
PS: I know I am not using my Adapter Model in correct way, I can use only one ArrayList for all the values, I'll modify it later right now I am focusing on the main functions.

should call initRecyclerView here:
private void displayFeedPosts() {
Call<FeedPosts> call = mAPIService.displayFeedPosts(apiKey);
call.enqueue(new Callback<FeedPosts>() {
#Override
public void onResponse(#NonNull Call<FeedPosts> call, #NonNull Response<FeedPosts> response) {
boolean error = response.body().getError();
if (!error) {
ArrayList<Post> feedPosts = response.body().getPosts();
for (Post post : feedPosts) {
mTripFeedPostUserNames.add(post.getFirstName() + " " + post.getLastName());
mTripFeedPostTime.add(post.getPostDatetime());
mTripFeedPostContent.add(post.getPostDescription());
}
initRecyclerView();
}
}
#Override
public void onFailure(#NonNull Call<FeedPosts> call, #NonNull Throwable t) {
Log.d(TAG, "Error: " + t.getMessage());
}
});
}
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);
RecyclerView recyclerView = findViewById(R.id.trip_feed_recycler_view);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(layoutManager);
mTripFeedPostRecyclerViewAdapter = new TripFeedPostRecyclerViewAdapter(this, mTripFeedPostProfileImages, mTripFeedPostUserNames, mTripFeedPostTime, mTripFeedPostContent, mTripFeedPostImages, mTripFeedPostID);
recyclerView.setAdapter(mTripFeedPostRecyclerViewAdapter);
mTripFeedPostRecyclerViewAdapter.notifyDataSetChanged();
}

Related

android- Send comments to server at same time and fetch them to RecyclerView

I send comments to server with two devices at same time. Later I add them to RecyclerView list. But sometimes one of the comments isn't shown. Code:
private void makeComment(String comment) {
Call<Model> call = restApiClass.commentPost(user_id, post_id, comment, date);
call.enqueue(new Callback<Model>() {
#Override
public void onResponse(Call<Model> call, Response<Model> response) {
if (response.body().isSuccess()) {
//send a comment and later refresh the RecylerView to get new added comment.
getComments();
}
}
private void getComments() {
Call<List<CommentsModel>> call = restApiClass.getCommentsPost(post_id);
call.enqueue(new Callback<List<CommentsModel>>() {
#Override
public void onResponse(Call<List<CommentsModel>> call, Response<List<CommentsModel>> response) {
if (response.isSuccessful()) {
list = response.body();
if (commentsAdapter == null) {
commentsAdapter = new CommentsAdapter(list, getContext());
commentsRecyclerView.setAdapter(commentsAdapter );
}
//if there are already comments in Adapter don't create again
else {
commentsAdapter.addComment(list.get(list.size() - 1));
//go to last index of RecyclerView:
((LinearLayoutManager) commentsPostRecyclerView.getLayoutManager()).
scrollToPositionWithOffset(list.size() - 1, 0);
}
}
addComment() in CommentsAdapter:
public void addComment(CommentsModel comment) {
this.list.add(comment);
notifyItemInserted(this.list.size()-1); //item is added to last position.
}

Getting exception while using REST API

I got an error of Expected Begin_ARRAY. but was BEGIN_OBJECTS at line 1 column
I am trying to get data from URL into recyclerview
api = RetrofitClient.getApiClient().create(Api.class);
Call<List<Courses>> call = api.getallcourse();
call.enqueue(new Callback<List<Courses>>()
{
#Override
public void onResponse(Call<List<Courses>> call, Response<List<Courses>> response) {
Toast.makeText(Dashboard.this, ""+response.toString(), Toast.LENGTH_SHORT).show();
courses = response.body();
recyclerAdapter = new RecyclerAdapter(courses);
recyclerView.setAdapter(recyclerAdapter);
}
#Override
public void onFailure(Call<List<Courses>> call, Throwable t) {
Toast.makeText(Dashboard.this, ""+t.toString(), Toast.LENGTH_SHORT).show();
}
});

Am I using LiveData properly in this code? (works)

I'm new to Android development, and am trying to use a good pattern for accessing my data from a web service using ViewModel, Retrofit, LiveData. Looking for feedback on this pattern.
I went through a few iterations and online guides, and came up with this
From the Activity:
LiveData<TicklerResponse> ticklerLiveData =
ticklerViewModel.search(ticklerSearchObject);
ticklerLiveData.observe(this, new Observer<TicklerResponse>() {
#Override
public void onChanged(#Nullable TicklerResponse ticklerResponse) {
Log.d(TAG, "onChanged: ticklerResponse size = " + ticklerResponse.getContent().size());
}
});
The ViewModel
public LiveData<TicklerResponse> search(TicklerSearchObject ticklerSearchObject) {
TicklerRepository ticklerRepository = TicklerRepository.getInstance();
String header = OscarOAuthContainer.getInstance()
.generateAuthorizationHeader(Verb.POST,"/tickler/search?startIndex=0&limit=1000");
MutableLiveData<TicklerResponse> liveData = new MutableLiveData<TicklerResponse>();
ticklerRepository.search(header,liveData, ticklerSearchObject);
return liveData;
}
The Repository
public void search(final String authHeader, final MutableLiveData<TicklerResponse> ticklerLiveData, TicklerSearchObject ticklerSearchObj) {
Log.d(TAG, "search: called");
Call<TicklerResponse> call = api.search(authHeader,0,1000,ticklerSearchObj);
call.enqueue(new Callback<TicklerResponse>() {
#Override
public void onResponse(Call<TicklerResponse> call, Response<TicklerResponse> response) {
if(response.isSuccessful()){
Log.d(TAG, "onResponse: successful response for getProvider");
TicklerResponse result = response.body();
ticklerLiveData.setValue(result);
} else {
Log.d(TAG, "onResponse: NOT SUCCESSFUL");
}
}
#Override
public void onFailure(Call<TicklerResponse> call, Throwable t) {
t.printStackTrace();
}
});
}
It seems to work, but I'm concerned if I'm using the LiveData structure correctly in how it's being instantiated/passed around.

Using retrofit to call two APIs in same class

I am having problem while calling two different APIs. There is no problem while calling first API using retrofit but when it reaches second retrofit call, there occurs error. Following is my code.
//for first retrofit call:
apiInterface = ApiClient.getApiClient().create(LekhaDrillInterface.class);
Call<LekhaDrillModel> call = apiInterface.getData(id, memberId);
call.enqueue(new Callback<LekhaDrillModel>() {
#Override
public void onResponse(Call<LekhaDrillModel> call, Response<LekhaDrillModel> response) {
posts = Collections.singletonList(response.body());
adapter = new LekhaBolpatraDrillAdapter(posts, getApplicationContext());
rvLekhaDrill.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
}
#Override
public void onFailure(Call<LekhaDrillModel> call, Throwable t) {
Log.e("error", t.getMessage());
}
});
here is no error and data is set in adapter
//2nd retrofit call
private void callRetrofitListData(){
pariyojanaInterfaces = ApiClient.getApiClient().create(PariyojanaInterface.class);
Log.e("memberIDIDID", String.valueOf(memberId));
Call <Data> call1 = pariyojanaInterfaces.getData(memberId);
call1.enqueue(new Callback<Data>() {
#Override
public void onResponse(Call<Data> call, Response<Data> response) {
datas = (List<Data>) response.body();
if (response.body()!=null){
Log.d("jchhec","chekc");
}
itemListAdapter = new ItemListAdapter(getApplicationContext(), datas);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
rv.setAdapter(itemListAdapter);
}
#Override
public void onFailure(Call<Data> call1, Throwable t) {
Log.e("error", t.getMessage());
}
});
}
the error is in the line
Call <Data> call1 = pariyojanaInterfaces.getData(memberId);
and the error is
Unable to create call adapter for interface retrofit2.Call for method PariyojanaInterface.getData
PariyojanaInterface.java
public interface PariyojanaInterface { #POST("projectBudget/pariyojanaListForMobile") Call getData(#Query("memberId") int memberId);
Can anybody please help me fix this problem?
First you must change PariyojanaInterface.java to
public interface PariyojanaInterface {
#POST("projectBudget/pariyojanaListForMobile") Call<List<Data>> getData(#Query("memberId") int memberId);
}
I Dont khow what your Data is, so you must replace List<Data> with List<yourData>
And you must change new Callback<Data> To new Callback<List<Data>

Save asynchronous retrofit response

I am using retrofit:2.1.0 and I am trying to save the response returned to my own POJO (UserProfile) but I can't seem to access the POJO, I assign the response to, outside of the callbacks.
So, in this call below, I want to have access to UserProfile outside of this call.
//adding `UserProfile userProfile;` outside of Call didn't help either
call.enqueue(new Callback<UserProfile>() {
#Override
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
if (response.isSuccessful()) {
UserProfile userProfile = response.body();
}
}
#Override
public void onFailure(Call<UserProfile> call, Throwable t) {
//do something
}
});
//here userProfile is null and hence can't get status
Log.d(TAG, "Status outside of Call is: " + userProfile.getStatus());
New approach, same result
private List<UserProfile> userProfileList = new ArrayList<>();
call.enqueue(new Callback<UserProfile>() {
#Override
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
if (response.isSuccessful()) {
UserProfile userProfile = response.body();
userProfileList.add(userProfile);
}
}
#Override
public void onFailure(Call<UserProfile> call, Throwable t) {
//do something
}
});
And this one gets fired even before callback and hence is null
if(userProfileList.size() > 0) {
for(UserProfile userProfile : userProfileList) {
Log.d(TAG, "Status is: " + userProfile.getStatus());
}
} else {
Log.d(TAG, "YakkerProfileList is NULL");
}
I added a setter in onResponse() which allowed me to use the response object elsewhere in the code like so:
#Override
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
if (response.isSuccessful()) {
UserProfile userProfile = response.body();
//elsewhere in the code, I can read the value from this method
setUserProfile(userProfile.getStatus());
}
}

Categories

Resources