recyclerview notifyDataSetChanged() is not working - java

I am trying to update myadapter using notifyDataSetchanged() in updateList().But not working for recyclerview. I can able to see list size in updateList() and its displaying actual result but only reclerview is not updating.This class was designed in MVVM Design Pattern.
public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.TasksAdapterViewHolder> {
private static final String TAG = TasksAdapter.class.getSimpleName();
static ArrayList<ItemModel> list;
static Context mContext;
private Fragment fragment;
private int row_index = -1;
private SessionManager session;
// SelectColorVM colorVM;
public TasksAdapter(Fragment fragment, Context context, ArrayList<ItemModel> itemList) {
this.mContext = context;
this.list = itemList;
this.fragment = fragment;
session = new SessionManager(fragment.getContext());
}
public void updateList(ArrayList<ItemModel> itemList){
list.clear();
this.list = itemList;
notifyDataSetChanged();
}
#Override
public TasksAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TasksItemBinding binding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.tasks_item,
parent, false);
return new TasksAdapter.TasksAdapterViewHolder(binding);
}
#Override
public void onBindViewHolder(TasksAdapterViewHolder holder, final int position) {
holder.binding.setViewModel(list.get(position));
if(position == 0 && list.get(position).btnText.get().toString().length() > 0)
holder.binding.btnUpdate.setVisibility(View.VISIBLE);
else
holder.binding.btnUpdate.setVisibility(View.INVISIBLE);
holder.binding.layoutItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// row_index = position;
// notifyDataSetChanged();
Fragment fragment = new DetailFragment();
FragmentManager manager = ((FragmentActivity) mContext).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
Bundle bundle = new Bundle();
ItemModel item = list.get(position);
bundle.putSerializable("item", item);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.id_frame, fragment, "details");
fragmentTransaction.addToBackStack("details");
fragmentTransaction.commit();
}
});
holder.binding.btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
HashMap<String, String> user = session.getUserDetails();
if(list.get(position).btnText.get().equals("Task Start")){
LandingEngine.updateStatusAndSetStartTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
} else if(list.get(position).btnText.get().equals("Task Complete")){
LandingEngine.updateStatusAndSetCompleteTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
}
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public static class TasksAdapterViewHolder extends RecyclerView.ViewHolder {
TasksItemBinding binding;
public TasksAdapterViewHolder(TasksItemBinding rowbinding) {
super(rowbinding.getRoot());
this.binding = rowbinding;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
}
I am calling adapter from my fragment like below
if(list.size() > 0) {
if(adapter == null) {
view.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(fragment.getContext());
view.setLayoutManager(mLayoutManager);
adapter = new TasksAdapter(fragment, fragment.getContext(), list);
view.setAdapter(adapter);
view.addItemDecoration(new DividerItemDecoration(fragment.getContext()));
} else {
adapter.updateList(list);
// view.getAdapter().notifyDataSetChanged();
// view.invalidate();
}
}
Please check this and let me know where i am doing wrong here.Thanks in advance..

Finally its working by calling this method from fragment class once getting API response from the volley library.
I have written following method in ViewModel class.
public void updateList(ArrayList<Class> list){
if(adapter != null) {
adapter.updateList(list);
}
}
And calling this method from fragment class like following.
ArrayList<StatisticsModel> list = (ArrayList) data;
viewModel.updateList(list);
So in the adapter class update method is following.
public void updateList(ArrayList<StatisticsModel> itemList){
list.clear();
// this.list = itemList;
// list.addAll(itemList);
// notifyItemInserted(list.size());
// notifyDataSetChanged();
this.list.addAll(itemList);
// notifyItemInserted(list.size());
notifyDataSetChanged();
}

Don't
public void updateList(ArrayList<ItemModel> itemList)
{
list.clear();
this.list = itemList;
notifyDataSetChanged();
}
Whenever calling this method list.clear(); clear all value.
The clear() method is used to remove all of the elements from a list.
DO
public void updateList(ArrayList<ItemModel> itemList)
{
this.list.addAll(itemList);
notifyDataSetChanged();
}

I have faced the same problem, you are doing mistake in this, change list into itemList:
#Override
public int getItemCount() {
return itemList.size();
}

Try this one
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.adapter.notifyDataSetChanged();
}
});

Try to add notifyDataSetChanged() in your Fragment class like...
adapter.notifyDataSetChanged();

try this remove list.clear(); from your updateList method
public void updateList(ArrayList<ItemModel> itemList){
this.list.addAll(itemList);
notifyItemInserted(list.size());
notifyDataSetChanged();
}
notifyItemInserted(int position)
Notify any registered observers that the item reflected at position has been newly inserted.
also try this
list.addAll(your new list);
if (adapter == null) {
view.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(fragment.getContext());
view.setLayoutManager(mLayoutManager);
adapter = new TasksAdapter(fragment, fragment.getContext(), list);
view.setAdapter(adapter);
view.addItemDecoration(new DividerItemDecoration(fragment.getContext()));
} else {
view.notifyItemInserted(list.size());
view.notifyDataSetChanged();
}

have a same issue. In my case there was error message in the logcat RecyclerView: No layout manager attached; skipping layout
which i have fixed with following code
LinearLayoutManager llm = new LinearLayoutManager(this.getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
then the notifyDataSetChanged start work

I don't know why sometimes we are not creating two instances of the adapter and also we are on the main thread adapter.notifyDataSetChanged() is not working In this case just call the notifyDataSetChanged() method from the Adapter class.
adapter.setNewValue(newlist)
and on AdapterClass
fun setNewValue(newlist:ArrayList<Any>) {
list = newlist
notifyDataSetChanged()
}

Related

How can filter recyclerview in selected viewpager fragment on search?

I have 3 tab fragments in viewpager with tabs. All tabs have recyclerview with ListAdapter showing customers list. Each tab represents 1) all customers 2) New ordered customers 3) Paid customers. I want to set search filter only on tab selected fragment. If the Searchview can place on activity which is common for all fragments, will be better in my case. I tried several ways but was failed. My search was resulting always only in last tab (Paid). Please can any one help me with full example of code ?
And my MainActivity
private void initializeTab(){
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager = (MyViewPager) findViewById(R.id.view_pager);
viewPager.setOffscreenPageLimit(5);
viewPager.setAdapter(mSectionsPagerAdapter);
tabLayout = (SmartTabLayout) findViewById(R.id.tabs);
tabLayout.setViewPager(viewPager);
viewPager.setPagingEnabled(true);
}
private class SectionsPagerAdapter extends FragmentStatePagerAdapter {
protected int currentPosition = -1;
protected Fragment currentFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#Override
public Fragment getItem(int position) {
return ViewPagerFragment.newInstance(getPageTitle(position)+"");
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "All";
case 1:
return "New";
case 2:
return "Paid";
}
return null;
}
#Override
public void setPrimaryItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
super.setPrimaryItem(container, position, object);
this.currentPosition = position;
if (object instanceof Fragment){
this.currentFragment = (Fragment)object;
}
}
public Fragment getCurrentFragment() {
return currentFragment;
}
public void setCurrentFragment(Fragment currentFragment) {
this.currentFragment = currentFragment;
}
}
and my Fragment
public class ViewPagerFragment extends AbstractFragment {
............
public static ViewPagerFragment newInstance(String title) {
ViewPagerFragment fragment = new ViewPagerFragment();
Bundle args = new Bundle();
args.putString("TITLE",title);
fragment.setArguments(args);
return fragment;
}
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeToRefresh);
recycler = (RecyclerView) view.findViewById(R.id.recycler);
adapter = new CustomerAdapter(getContext());
recycler.setLayoutManager(new LinearLayoutManager(getContext()));
recycler.setHasFixedSize(true);
recycler.addItemDecoration(new ItemDecoration(1,false, dpToPx(0),true,2));
recycler.setItemAnimator(new DefaultItemAnimator());
recycler.setAdapter(adapter);
changeFragment(swipeLayout);
}
public void changeFragment(final SwipeRefreshLayout swipeLayout){
final String tab = getArguments().getString("TITLE");
loadDatas(allRecords,tab);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeLayout.setRefreshing(true);
utils.getDetails(getContext());
utils.setOnDataLoaded(new MyUtils.DataLoadingListener() {
#Override
public void onDataLoaded(ArrayList<Customers> list) {
loadDatas(list, tab);
swipeLayout.setRefreshing(false);
}
});
}
});
// Scheme colors for animation
swipeLayout.setColorSchemeColors(
getResources().getColor(android.R.color.holo_blue_bright),
getResources().getColor(android.R.color.holo_green_light),
getResources().getColor(android.R.color.holo_orange_light),
getResources().getColor(android.R.color.holo_red_light)
);
}
void loadDatas(List<Customers> list, String tab){
if (list != null && list.size() > 0) {
List<Customers> tabList = new ArrayList<>();
if (tab.equals("All")) {
tabList = list;
}else {
for (Customers item : list) {
if (item.getStatus().equals(tab)) {
tabList.add(item);
}else {
Log.d(TAG, "loadDatas: "+item.getStatus());
}
}
}
adapter.submitList(tabList);
}
}
}
the Searchview can place on activity which is common for all fragments, will be better in my case.
This is right.
I tried several ways but was failed. My search was resulting always only in last tab (Paid).
So, I'm assuming that the issue now isn't in the filtering itself, but that it always returns the results of the last tab; and therefore you probably didn't provide the filtering code for such a reason.
Now we've 3 tabs in the ViewPager, each represents a fragment; So we need to get the current selected fragment of the ViewPager in order to allow the activity send it the searched text.
To get the current fragment from ViewPager (Your case):
ViewPagerFragment currentFragment = (ViewPagerFragment) mViewPagerAdapter.instantiateItem(viewPager, viewPager.getCurrentItem());
To get the current fragment from ViewPager2:
The ViewPager has getCurrentItem() which returns the current page number
So, we need to link the each page fragment to the page number.
But we can get a ViewPager fragment by its id (item id), so the first step is to have page Ids that equals to the corresponding position, to do so override getItemId in the ViewPager adapter.
#Override
public long getItemId(int position) {
return position;
}
Then in the activity filtering part assuming you are searching for a String that is in a var named seachString:
int pageId = viewpager.getCurrentItem();
Fragment currentFragment = (ViewPagerFragment) getChildFragmentManager()
.findFragmentByTag("f" + pageId);
Then after getting the current fragment, create some method in it that accepts a String parameter for the searched value:
So, in ViewPagerFragment, create:
public void searchFor(String search) {
// filter the results of the RecyclerView
}
And call that method on the current fragment of the ViewPager in the activity:
currentFragment.searchFor(seachString);
About the recyclerview filter data:
could make your class CustomerAdapter implements Filterable
like :
class Adapter extends RecyclerView.Adapter implements Filterable {
......
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
return ...;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
}
};
}
}
could also refer this:https://github.com/xaverkapeller/Searchable-RecyclerView-Demo
About how to do this in selected viewpager fragment , you just need to invoke the search method of the current fragment.
At last I succeeded, I think it is not very professional but it is working fine. To help some one suffering I am sharing that here.
Firstly I added tabScrollingListener to get the selected fragment in MainActivity
public class MainActivity extends AppCompatActivity{
..........
private ViewPagerFragment selectedFragment;
private void initializeTab(){
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager = (MyViewPager) findViewById(R.id.view_pager);
viewPager.setOffscreenPageLimit(5);
viewPager.setAdapter(mSectionsPagerAdapter);
tabLayout = (SmartTabLayout) findViewById(R.id.tabs);
tabLayout.setViewPager(viewPager);
viewPager.setPagingEnabled(true);
viewPager.setCurrentItem(0);
tabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
selectedFragment = (ViewPagerFragment) mSectionsPagerAdapter.getCurrentFragment();
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
Then I transferred my filter function to Fragment like mentioned in Zain's answer. As I am using ListAdapter for my recycler it's easy to submit my filter list to adapter. I loaded my filter list on loadData function.
public class ViewPagerFragment extends AbstractFragment {
............
private List<Customers> filterList;
public static ViewPagerFragment newInstance(String title) {
ViewPagerFragment fragment = new ViewPagerFragment();
Bundle args = new Bundle();
args.putString("TITLE",title);
fragment.setArguments(args);
return fragment;
}
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeToRefresh);
recycler = (RecyclerView) view.findViewById(R.id.recycler);
adapter = new CustomerAdapter(getContext());
recycler.setLayoutManager(new LinearLayoutManager(getContext()));
recycler.setHasFixedSize(true);
recycler.addItemDecoration(new ItemDecoration(1,false, dpToPx(0),true,2));
recycler.setItemAnimator(new DefaultItemAnimator());
recycler.setAdapter(adapter);
changeFragment(swipeLayout);
}
public void changeFragment(final SwipeRefreshLayout swipeLayout){
final String tab = getArguments().getString("TITLE");
loadDatas(allRecords,tab);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeLayout.setRefreshing(true);
utils.getDetails(getContext());
utils.setOnDataLoaded(new MyUtils.DataLoadingListener() {
#Override
public void onDataLoaded(ArrayList<Customers> list) {
loadDatas(list, tab);
swipeLayout.setRefreshing(false);
}
});
}
});
// Scheme colors for animation
swipeLayout.setColorSchemeColors(
getResources().getColor(android.R.color.holo_blue_bright),
getResources().getColor(android.R.color.holo_green_light),
getResources().getColor(android.R.color.holo_orange_light),
getResources().getColor(android.R.color.holo_red_light)
);
}
void loadDatas(List<Customers> list, String tab){
if (list != null && list.size() > 0) {
List<Customers> tabList = new ArrayList<>();
if (tab.equals("All")) {
tabList = list;
}else {
for (Customers item : list) {
if (item.getStatus().equals(tab)) {
tabList.add(item);
}else {
Log.d(TAG, "loadDatas: "+item.getStatus());
}
}
}
filterList = tabList; // this fills filter list
adapter.submitList(tabList);
}
}
}
public void filter(String text, String filter) {
ArrayList<Customers> filteredlist = new ArrayList<>();
// running a for loop to compare elements.
if (filterList != null && filterList.size() > 0) {
if (text.length() > 0) {
for (Customers item : filterList) {
// here filter options gone
}
}else {
filteredlist.addAll(filterList);
}
}
adapter.submitList(filteredlist);
}
Yea, Now I got currentSelectedFragment with filter function in MainActivity
In searchListener I can call it like selectedFragment.filter(query).
searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
selectedFragment.filter(newText); // search filter here
return true;
}
});
Follow the below step to find the clicked tab,
First add tab select listener
tabLayout.addOnTabSelectedListener(object:TabLayout.OnTabSelectedListener{
override fun onTabSelected(tab: TabLayout.Tab?) {
selectedFragment= fragmentViewPagerAdapter!!.fragments[tab!!.position]
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
// enter code here
}
override fun onTabReselected(tab: TabLayout.Tab?) {
// enter code here
}
})
and then setupWithViewPager
tabLayout.setupWithViewPager(viewPager)

Custom Card View Adapter : notify data set changed not working

Im setting up a custom card-view slider adapter and on startup the card view loads up fine but after items is added/replaced in the array list and the notify adapter method is called, no changes occur to the content.
I have already tried adding the items directly from the adapter via a public method and the item gets added however, it is always placed at the last position.
This is my adapter :
static class UploadImageAdapter extends CardSliderAdapter<ImageObject>
{
private Context context;
private ArrayList<ImageObject> arrayList;
private OnUploadButtonClick onUploadButtonClick;
public interface OnUploadButtonClick{
void upload(int position);
}
UploadImageAdapter(Context context, ArrayList<ImageObject> items) {
super(items);
this.context = context;
this.arrayList = items;
}
static class ViewHolder{
ImageView image;
ImageButton addImage;
Button removeImage;
View layout;
}
#Override
public void bindView(int position, #NotNull View itemContentView, ImageObject imageObject) {
final ViewHolder holder = new ViewHolder();
holder.image = itemContentView.findViewById(R.id.card_image);
holder.addImage = itemContentView.findViewById(R.id.upload_image_btn);
holder.removeImage = itemContentView.findViewById(R.id.card_remove_btn);
holder.layout = itemContentView.findViewById(R.id.card_image_layout);
if (imageObject.hasImage) {
Glide.with(context)
.load(Uri.parse(imageObject.getImage_path()))
.placeholder(R.color.offsetWhiteBackground)
.centerCrop()
.transition(withCrossFade())
.into(holder.image);
holder.addImage.setVisibility(View.GONE);
holder.layout.setVisibility(View.VISIBLE);
holder.removeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
else {
holder.layout.setVisibility(View.GONE);
holder.addImage.setVisibility(View.VISIBLE);
holder.addImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onUploadButtonClick.upload(position);
}
});
}
}
#Override
public ImageObject getItem(int position) {
return arrayList.get(position);
}
void setOnUploadButtonClick(OnUploadButtonClick onUploadButtonClick){
this.onUploadButtonClick = onUploadButtonClick;
}
#Override
public int getItemContentLayout(int position) {
//TODO return the item layout of every position
return R.layout.upload_image_card;
}
}
And below is where i instantiate :
private void setCardSlider() {
upload = new ArrayList<>();
for(int i = 0; i < 3; i++){
upload.add(new ImageObject(i,false,""));
}
adapter = new UploadImageAdapter(getContext(), upload);
CardSliderViewPager cardSliderViewPager = rootView.findViewById(R.id.image_upload_slider);
cardSliderViewPager.setAdapter(adapter);
uploadDialog = new UploadDialog(getActivity(), getContext());
adapter.setOnUploadButtonClick((int position) -> {
uploadDialog.callUploadDialog();
mPosition = position;
});
}
public void uploadImage(){
uploadDialog.callUploadDialog();
}
public void setImage(Uri uri){
upload.get(mPosition).setHasImage(true);
upload.get(mPosition).setImage_path(uri.toString());
adapter.notifyDataSetChanged();
}
I would suggest using notifyItemChange instead. There you use a position as a parameter and it doesn't need to redraw everything.

How to load data into recyclerView

In my application i want get some data from server and show into recyclerView. For application architecture i used MVP
I wrote below codes, but after loaded data from server, not show any data into recyclerView!
I used debug mode and show me data in this break point
public void add(List<DataItem> list) {
list.addAll(list);
notifyDataSetChanged();
}
but not show me data into recyclerView!
My Activity codes :
public class ListFragment extends Fragment implements ActiveTestsContract.View {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tester_active_tests, container, false);
//Initialize
init();
//User token log
if (!App.isEmptyString(App.getPrefs("JWT")) || !App.getPrefs("JWT").equals(ConstKeys.EMPTY)) {
userToken = App.getPrefs("JWT");
}
//Load data
getData();
return view;
}
#Override
public void updateTestsList(Data data, int page) {
testerDashboard_emptyLay.setVisibility(View.GONE);
activeTests_pullToLoader.setVisibility(View.VISIBLE);
//Get lasted page
if (page == data.getLastPage()) {
isHasLoadedAll = true;
activeTests_pullToLoader.setComplete();
}
adapter.add(data.getData());
//Complete items
isLoading = false;
nextPage = page + 1;
activeTests_pullToLoader.setComplete();
}
#Override
public void init() {
context = getActivity();
handler = new Handler(Looper.getMainLooper());
testsPresenter = new ActiveTestsPresenter(this, 3);
testerDashboard_loader = view.findViewById(R.id.testerDashboard_loader);
activeTests_pullToLoader = view.findViewById(R.id.activeTests_pullToLoader);
testerDashboard_emptyLay = view.findViewById(R.id.testerDashboard_emptyLay);
emptyLayout_editProfileBtn = view.findViewById(R.id.emptyLayout_editProfileBtn);
layoutManager = new LinearLayoutManager(context);
recyclerView = activeTests_pullToLoader.getRecyclerView();
//Adapter
adapter = new TesterActiveRecyclerAdapter(activeModel, context);
//Init recycler and adapter
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
activeTests_pullToLoader.setColorSchemeResources(R.color.colorPrimary);
}
private void getData() {
activeTests_pullToLoader.isLoadMoreEnabled(true);
activeTests_pullToLoader.setPullCallback(new PullCallback() {
#Override
public void onLoadMore() {
isLoading = true;
//Call api
testsPresenter.testsListResponse(App.recipesApi, userToken, nextPage);
}
#Override
public void onRefresh() {
adapter.clear();
isHasLoadedAll = false;
isLoading = true;
//Call api
testsPresenter.testsListResponse(App.recipesApi, userToken, 1);
}
#Override
public boolean isLoading() {
return isLoading;
}
#Override
public boolean hasLoadedAllItems() {
return isHasLoadedAll;
}
});
activeTests_pullToLoader.initLoad();
}
Adapter codes:
public class TesterActiveRecyclerAdapter extends RecyclerView.Adapter<TesterActiveRecyclerAdapter.ViewHolder> {
private List<DataItem> list;
private Context context;
public TesterActiveRecyclerAdapter(List<DataItem> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_tester_test_list, parent, false);
return new ViewHolder(view);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
DataItem item = list.get(position);
//Name
holder.txt_name.setText(item.getTitle());
//Conditions
if (!App.isEmptyString(item.getOs()) && !App.isEmptyString(item.getType()) && !App.isEmptyString(item.getBrowser())) {
holder.txt_condition.setText(item.getType() + " | " + item.getOs() + " | " + item.getBrowser());
}
//Button actions
holder.setState(item.getState(), position);
//Price
holder.rowTests_priceTxt.setText(item.getPrice() + " Dollar");
//Animate items
Animation animation = AnimationUtils.loadAnimation(App.context,
(position > list.size() - 1) ? R.anim.down_from_top : R.anim.up_from_bottom);
holder.itemView.startAnimation(animation);
Toast.makeText(context, ""+list.get(0).getId(), Toast.LENGTH_SHORT).show();
}
#Override
public int getItemCount() {
return list.size();
}
public void add(List<DataItem> list) {
list.addAll(list);
notifyDataSetChanged();
}
public void clear() {
list.clear();
notifyDataSetChanged();
}
class ViewHolder extends RecyclerView.ViewHolder {
private ViewGroup root;
private TextView txt_name, txt_value, txt_condition, rowTests_priceTxt;
private RoundTextView rowTests_button;
ViewHolder(View view) {
super(view);
root = (ViewGroup) view;
txt_name = view.findViewById(R.id.txtTestListTitle);
txt_value = view.findViewById(R.id.txtTestListSublist);
txt_condition = view.findViewById(R.id.txtTestListSublist2);
rowTests_priceTxt = view.findViewById(R.id.rowTests_priceTxt);
rowTests_button = view.findViewById(R.id.rowTests_button);
}
}
How can i fix it?
You are adding data again in the same list passed in the parameter. Try to replace the below code
public void add(List<DataItem> list) {
list.addAll(list);
notifyDataSetChanged();
}
with
public void add(List<DataItem> list) {
this.list.addAll(list);
notifyDataSetChanged();
}
Clear the current list
Add the data to the adapter's list.
public void add(List<DataItem> list) {
clear();
this.list.addAll(list);
notifyDataSetChanged();
}
Your approach didn't work because you were updating the very list that you were passing in the function and not the adapter's list.
First add all data into list & than notify your adapter , after this set your adapter to recyclerview.
list.addAll(data)
adapter.notifyDataSetChanged()
val layoutManager = LinearLayoutManager(activity)
recyclerview.isNestedScrollingEnabled = false
recyclerview.layoutManager = layoutManager
recyclerview.itemAnimator = DefaultItemAnimator()
recyclerview.adapter = mAdapter

Viewmodel shows empty recyclerview when activity rotated

I am pretty new to the Android architecture components and have been trying out room for data storage from my server. Problem is no data is being shown on the recycler view IMMEDIATELY. There's a searchview(no logic implemented just there) right above my recyclerview and when I click searchview for input, the recyclerview shows all the data which was supposed to be shown earlier.
RestaurantsAdapter:
public class RestaurantsAdapter extends RecyclerView.Adapter<RestaurantsAdapter.MyViewHolder> {
private List<Restaurant> data;
private Context context;
private LayoutInflater layoutInflater;
private final Random r = new Random();
public RestaurantsAdapter(Context context) {
this.data = new ArrayList<>();
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public RestaurantsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_restaurant, parent, false);
return new RestaurantsAdapter.MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RestaurantsAdapter.MyViewHolder holder, int position) {
holder.rName.setText(data.get(position).getName());
}
public void setData(List<Restaurant> newData) {
if (data != null) {
RestaurantDiffCallback restaurantDiffCallback = new RestaurantDiffCallback(data, newData);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(restaurantDiffCallback);
data.clear();
data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
} else {
// first initialization
data = newData;
}
}
#Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView rName;
public MyViewHolder(View itemView) {
super(itemView);
rName = (TextView) itemView.findViewById(R.id.restaurant_name);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
}
}
class RestaurantDiffCallback extends DiffUtil.Callback {
private final List<Restaurant> oldRestaurants, newRestaurants;
public RestaurantDiffCallback(List<Restaurant> oldPosts, List<Restaurant> newPosts) {
this.oldRestaurants = oldPosts;
this.newRestaurants = newPosts;
}
#Override
public int getOldListSize() {
return oldRestaurants.size();
}
#Override
public int getNewListSize() {
return newRestaurants.size();
}
#Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldRestaurants.get(oldItemPosition).getIdentifier().equals(newRestaurants.get(newItemPosition).getIdentifier());
}
#Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldRestaurants.get(oldItemPosition).equals(newRestaurants.get(newItemPosition));
}
}}
MainActivity:
public class MainActivity extends AppCompatActivity {
private RestaurantsAdapter restaurantsAdapter;
private RestaurantViewModel restaurantViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
restaurantsAdapter = new RestaurantsAdapter(this);
restaurantViewModel = ViewModelProviders.of(this).get(RestaurantViewModel.class);
restaurantViewModel.getAllRestaurants().observe(this, restaurants -> restaurantsAdapter.setData(restaurants));
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(restaurantsAdapter);
}}
ViewModel:
public class RestaurantViewModel extends AndroidViewModel {
private RestaurantDao restaurantDao;
private ExecutorService executorService;
private ApiInterface webService;
public RestaurantViewModel(#NonNull Application application) {
super(application);
restaurantDao = RestaurantsDatabase.getInstance(application).restaurantDao();
executorService = Executors.newSingleThreadExecutor();
webService = ApiClient.getApiClient().create(ApiInterface.class);
}
LiveData<List<Restaurant>> getAllRestaurants() {
refreshUser();
return restaurantDao.findAll();
}
private void refreshUser() {
executorService.execute(() -> {
int numOfRestaurants = restaurantDao.totalRestaurants();
if (numOfRestaurants < 30) {
Call<RestaurantsModel> call = webService.getRestaurants();
call.enqueue(new Callback<RestaurantsModel>() {
#Override
public void onResponse(#NonNull Call<RestaurantsModel> call, #NonNull Response<RestaurantsModel> response) {
restaurantDao.saveAll(response.body().getData().getData());
}
#Override
public void onFailure(#NonNull Call<RestaurantsModel> call, #NonNull Throwable t) {
}
});
}
});
}}
If you don't use the DiffUtil with its diffResult.dispatchUpdatesTo(this); you should do notifyDataSetChanged(). In your case, in RestaurantsAdapter.setData add one line:
// first initialization
data = newData;
notifyDataSetChanged();
You have an issue in your setData method:
public void setData(List<Restaurant> newData) {
if (data != null) {
RestaurantDiffCallback restaurantDiffCallback = new RestaurantDiffCallback(data, newData);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(restaurantDiffCallback);
data.clear();
data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
} else {
// first initialization
data = newData;
}
}
When your newData is null your change the data source of your adapter, but you don't call notifyDataSetChanged.
This way the data that you are seeing on the screen will not be updated.
So in order to fix it:
public void setData(List<Restaurant> newData) {
if (data != null) {
RestaurantDiffCallback restaurantDiffCallback = new RestaurantDiffCallback(data, newData);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(restaurantDiffCallback);
data.clear();
data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
} else {
// first initialization
data = newData;
notifyDataSetChanged();
}
}
Another thing, if not a very good practice setting your adapter dataset has null. So my suggestion is to set your data as an empty list instead of null:
data = new ArrayList<>();
The issue: you're using ViewModels incorrectly. You are only returning data from the ViewModel, but never saving data in the ViewModel
Start by reading how ViewModels work here: https://developer.android.com/topic/libraries/architecture/viewmodel

return Changed arrayList from adapter android

have gridview for which set adapter called ImageAdapter with parameter of arraylist. Inside the adapter have onclicklistener during which from the arraylist one item is removed and then when i use this line ImageAdapter.notifyDataSetChanged in the gridview item is removed. Now i need the changed arrayList in my activity so how can i get it.
Here's my code:
public class ImageAdapter extends BaseAdapter {
Context context;
ArrayList<String> listCheck = new ArrayList<String>();
ImageAdapter adapter = this;
public ImageAdapter(Context context, ArrayList<String> list) {
this.context = context;
listCheck = list;
}
#Override
public int getCount() {
return listCheck.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder121 holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_gridmain, null);
holder = new ViewHolder121();
holder.imageView = (ImageView) convertView
.findViewById(R.id.img_selected_image);
holder.close = (ImageButton) convertView
.findViewById(R.id.img_btn_cancel);
convertView.setTag(holder);
}
else {
holder = (ViewHolder121) convertView.getTag();
}
holder.close.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg1) {
listCheck.remove(position);
adapter.notifyDataSetChanged();
}
}); Bitmap bm = decodeSampledBitmapFromUri(listCheck.get(position), 220,
220);
holder.imageView.setImageBitmap(bm);
return convertView;
} }
Fragment code:
ImageAdapter mainActivityAdapter = new ImageAdapter(getActivity(), ar1);
gridview_withimage.setAdapter(mainActivityAdapter);
question: How to get changed arraylist from ImageAdapter to called Fragement
How to get changed arraylist from ImageAdapter to called Fragement
Create a method in ImageAdapter which will return ArrayList used as data-source in Adapter:
public ArrayList<String> getModifyList() {
return listCheck;
}
Call getModifyList method in Fragment for getting ArrayList using Adapter object:
gridview_withimage.setAdapter(mainActivityAdapter);
gridview_withimage.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
ArrayList<String> arrNewList=mainActivityAdapter.getModifyList();
}
}, 100);
You can pass Fragment instance to Adapter.
private Fragment fragment;
public ImageAdapter(Context context, ArrayList<String> list,Fragment mfragment) {
this.context = context;
listCheck = list;
this.fragment=mfragment;
}
and Make one public method in your fragment and call using this instance of Fragment fragment;
You can create an interface like this
public interface ImageAdapterListener{
void onListChange(List<String> list);
}
And declare a member in ImageAdapter
private ImageAdapterListener listener;
public setListener(ImageAdapterListener listener){
this.listener = listener}
override the notifyDataSetChanged
#Override
void notifyDataSetChanged{
super.notifyDataSetChanged();
if(listener!= null) listener.onListChange(listCheck);
}
and make your fragment implement that interface.
class Myfragment extends Fragment implements ImageAdapterListener {
ImageAdapter mainActivityAdapter = new ImageAdapter(getActivity(), ar1);
gridview_withimage.setAdapter(mainActivityAdapter);
mainActivityAdapter.setListener(this);
#Override
void onListChange(List<String> list){
//do your stuff
}
}

Categories