I have room database for managing chat database for app. In database i have a column which has value 0 and 1
0 for Image waiting for the download 1 for Image downloaded into device
whenever user receive image from other users app runs Asynctask to download an image then enter updated value into room database with value 1 (for data successfully downloaded into device). So when download complete Asynctask calls interface in Pagedlist adapter.
In that interface i check whether that image is downloaded or not by Entity_TableColums.isDownload which always return 0 even in database value got updated and after onChaged called.
My question is how to update Entity value in Pagedlistadapter?
Here is code
public class ChatScreen extends Fragment{
private ChatAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.chat_screen_main_fragment, container, false);
adapter = new ChatAdapter(getActivity(),getLifecycle());
Chat_ViewModel viewModel = new ViewModelProvider(this).get(Chat_ViewModel.class);
viewModel.chatList.observe(getViewLifecycleOwner(), new Observer<PagedList<ChatEntity_TableColums>>() {
@Override
public void onChanged(PagedList<ChatEntity_TableColums> chatEntity_tableColums) {
Log.d(TAG, "onChanged: Chat Entity = "+chatEntity_tableColums.size());
adapter.submitList(chatEntity_tableColums);
}
});
return v;
}
PagedListAdapter
public class ChatAdapter extends PagedListAdapter <ChatEntity_TableColums,RecyclerView.ViewHolder> {
public ChatAdapter(Context context, Lifecycle lifecycle) {
super(diffCallaback);
this.context = context;
this.lifecycle = lifecycle;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_chat_image,parent,false);
return new ViewHolderImage(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ChatEntity_TableColums wrapper = getItem(position);
ViewHolderImage imageHolder = (ViewHolderImage) holder;
chatDownloadPath downloadPath = new chatDownloadPath() {
@Override
public void downloadFilePath(ArrayList<String> path) {
imageHolder.progressBarMainImage.setVisibility(View.GONE);
Log.d(TAG, "downloadFilePath: Is Download = "+wrapper.getIS_DOWNLOADED());// DATA NOT GETTING UPDATE WHEN ON CLICK ALWAYS 0
}
};
if (wrapper.getIS_DOWNLOADED() == 0) {
Log.d(TAG, "Start Downloading File " +wrapper.getUSER_MESSAGE() + "Check Boolean " + wrapper.getIS_DOWNLOADED());
new downloadChatAttachment(context, wrapper.getUSER_MESSAGE(), nameFile + ".jpg", downloadPath, "Media/Elaxer Images", wrapper.getId(), wrapper.getOTHER_USER_ID()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else {
Log.d(TAG, "onBindViewHolder: Image check = "+wrapper.getUSER_MESSAGE());
Glide.with(context).asBitmap()
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.DATA).load(wrapper.getUSER_MESSAGE())
.apply( new RequestOptions().transform(new CenterCrop(), new RoundedCorners(roundedCorner)))
.into(imageHolder.thumbnail);
}
imageHolder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "downloadFilePath: Database ID = "+wrapper.id+" Is Download = "+wrapper.getIS_DOWNLOADED());
if (wrapper.getIS_DOWNLOADED() == 0){
Toast.makeText(context, "Please wait... Image Downloading", Toast.LENGTH_SHORT).show(); // DATA NOT GETTING UPDATE WHEN ON CLICK
}else {
Log.d(TAG,"On Clicked "+wrapper.getIS_DOWNLOADED());
}
});
}
@Override
public int getItemViewType(int position) {
ChatEntity_TableColums wrapper = getItem(position);
return super.getItemViewType(position);
}
public class ViewHolderImage extends RecyclerView.ViewHolder {
public ViewHolderImage(@NonNull View itemView) {
super(itemView);
thumbnail = itemView.findViewById(R.id.chatThumbnail);
}
}
private static DiffUtil.ItemCallback<ChatEntity_TableColums> diffCallaback = new DiffUtil.ItemCallback<ChatEntity_TableColums>() {
@Override
public boolean areItemsTheSame(@NonNull ChatEntity_TableColums oldItem, @NonNull ChatEntity_TableColums newItem) {
return oldItem.getId() == newItem.getId();
}
@Override
public boolean areContentsTheSame(@NonNull ChatEntity_TableColums oldItem, @NonNull ChatEntity_TableColums newItem) {
return oldItem.equals(newItem);
}
};
public interface chatDownloadPath {
void downloadFilePath(ArrayList<String> path);
}
}
ViewModel
public class Chat_ViewModel extends ViewModel {
private static final String TAG = "Chat View Model";
public final LiveData<PagedList<ChatEntity_TableColums>> chatList;
public Chat_ViewModel() {
chatList = new LivePagedListBuilder<>(
ChatRoomDatabase.getInstance(MyApplication.getmContext(),1).chatDAO().getPaginationChat(String.valueOf(Session.getOtherUserID())),20).build();
}
}
DAO
@Query("SELECT * FROM databaseName WHERE orderID = :ID ORDER BY ID DESC")
DataSource.Factory<Integer, ChatEntity_TableColums> getChat(String UserID);
I double check in database whether value is getting update or not and download value is always get update in database before reaching to interface.
If any need of more details please let me know. Help me out to get Entity refresh after update in room database
from Room database Entity not returning updated value in Pagedlistadapter
No comments:
Post a Comment