I got a problem that I can't share a RxBleClient between MainActivity and Fragments.
I pass a instance of RxBleClient to MyViewModel, and while trying to receive from ViewModel in Fragment it is just a Null. Fragment is a part of ViewPager2. Here is how I did this:
View Model code:
public class MyViewModel extends ViewModel {
private MutableLiveData<RxBleClient> rxBleClient = new MutableLiveData<RxBleClient>();
public MyViewModel() {
}
public void setRxBleClient(RxBleClient item) {
rxBleClient.setValue(item);
}
public LiveData<RxBleClient> getRxBleClient() {
return rxBleClient;
}
}
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyViewModel model = new ViewModelProvider(this).get(MyViewModel.class);
// Turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
int REQUEST_ENABLE_BT = 1;
this.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
// pass instance of RxBleClient to ViewModel
rxBleClient = RxBleClient.create(getApplicationContext());
model.setRxBleClient(rxBleClient);
// bind
ButterKnife.bind(this);
// rest of code
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_connection_list, container, false);
scanResultList = new ArrayList<ScanResult>(0);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyConnectionRecyclerViewAdapter(scanResultList));
}
// get View Model
myViewModel = new ViewModelProvider(getActivity()).get(MyViewModel.class);
myViewModel.getRxBleClient().observe(getViewLifecycleOwner(), client -> {
this.rxBleClient = client; // HERE IS NULL
});
return view;
}
Where did I make mistake?
First edit:
This is my only one Activity, yet. This Activity is hosting ViewPager2:
// FRAGMENT ADAPTER
fragmentStateAdapter = new ViewPager2FragmentStateAdapter(getSupportFragmentManager(), getLifecycle());
viewPager2 = findViewById(R.id.pager);
viewPager2.setAdapter(fragmentStateAdapter);
// TOP BAR
final String tabTitles[] = {"Debugging", "Bluetooth", "Video"};
final #DrawableRes int tabDrawable[] = {R.drawable.ic_bug_report_24px, R.drawable.ic_bluetooth_24px, R.drawable.ic_videocam_24px};
tabLayout = findViewById(R.id.tab_layout);
new TabLayoutMediator(tabLayout, viewPager2,
(tab, position) -> tab.setText(tabTitles[position]).setIcon(tabDrawable[position])
).attach();
Related
I have app that recive input from activity and needs to display it in recyclerview that imlement in tabs.
After the button pressed in the activity the values doesn't show in the target tab that I need (the 2nd one) and it shows there only after I scroll the last tab (4t) and return to the 2nd one.
Here is the code :
tab2
public class Tab2Fragment extends Fragment {
private ArrayList<exampleItem> mExampleList = new ArrayList<>();
RecyclerView mRecyclerView;
private exampleAdapter mAdapter = new exampleAdapter(mExampleList);
private RecyclerView.LayoutManager mLayoutManager;
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_two, container, false);
mRecyclerView = v.findViewById(R.id.recyclerview2);
buildRecyclerView(v);
loadData();
return v;
}
public void removeItem(int position) {
mExampleList.remove(position);
//mExampleList.clear();
//mRecyclerView.removeViewAt(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
}
public void changeItem(int position, String text) {
mExampleList.get(position).changeText1(text);
mAdapter.notifyItemChanged(position);
}
private void loadData() {
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences12", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list12", null);
Type type = new TypeToken<ArrayList<exampleItem>>() {
}.getType();
mExampleList = gson.fromJson(json, type);
if (mExampleList == null) {
mExampleList = new ArrayList<>();
}
}
private void buildRecyclerView(View view) {
mRecyclerView = view.findViewById(R.id.recyclerview2);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mAdapter = new exampleAdapter(mExampleList);
//mRecyclerView.swapAdapter(mAdapter,false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new exampleAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
changeItem(position, "Clicked");
}
#Override
public void onDeleteClickListener(int position) {
removeItem(position);
}
});
}
}
mainActivty
public class MainActivity extends AppCompatActivity {
private TabAdapter adapter;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
adapter = new TabAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "main");
adapter.addFragment(new Tab2Fragment(), "daily");
adapter.addFragment(new Tab3Fragment(), "weekly");
adapter.addFragment(new Tab4Fragment(), "monthly");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}
You use notifyDataSetChanged() in changeItem and removeitem and buildRecyclerView functions.buildRecyclerView function is called before loadData and changeItem is called in onItemClick that I don't know is the same as the button you mention that you pressed. but in loadData function, you don't call notifyDataSetChanged() or other notify functions. probably this is the cause of your problem. please check that after every change in data list of recyclerview, notifyDataSetChanged() or other notify functions must is called
i am stuck at nested fragments , some one please guide me a little bit. i am in learning phase...
actually i am using a bottomNavigationView and popluate fragments and when selecting an item, one fragment has a view pager and tab layout in it, and loading list data from fire base. the problem is that when a fragment with viewpager in it selected, data fetched successfuly but not populated in list view, but when i swite view pages the listview is populated . any one please help me for this problem or guide me where to put network calls in fragment or something else solution of it.
First Fragment which have Viewpager in .
public class explore extends Fragment {
public static explore Instance = null;
private TabLayout tabLayout;
private ViewPager viewPager;
private Activity activity;
private Context context;
private PagerAdapter pagerAdapter;
public static explore getInstance() {
if (Instance == null) {
Instance = new explore();
}
return Instance;
}
public explore() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
activity = (Activity) getActivity();
context = getActivity();
return inflater.inflate(R.layout.fragment_explore, container, false);
}
#Override
public void onViewCreated(#NonNull View v, #Nullable Bundle savedInstanceState) {
tabLayout = v.findViewById(R.id.tabs_Explore);
viewPager = v.findViewById(R.id.viewpager_Explore);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
List<Category> categories = new FirebaseHelper(getActivity()).getCategoriesList();
pagerAdapter = new PagerAdapter(getChildFragmentManager(), categories);
viewPager.setAdapter(pagerAdapter);
super.onActivityCreated(savedInstanceState);
}
}
pager's first fragment means(ChildFragment)
public class categories extends Fragment {
private static categories Obj = null;
private RecyclerView listView;
private List<Category> categoryList;
private CategoryAdapter categoryAdapter;
public categories() {
categoryList = new ArrayList<>();
}
public static categories getInstance() {
if (Obj == null) {
Obj = new categories();
}
return Obj;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_blank, container, false);
listView = v.findViewById(R.id.mRecycler);
listView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
listView.setHasFixedSize(true);
SnapHelper helper = new LinearSnapHelper();
helper.attachToRecyclerView(listView);
categoryAdapter = new CategoryAdapter(getActivity(), categoryList);
listView.setAdapter(categoryAdapter);
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (bundle != null) {
categoryList = bundle.getParcelableArrayList("data");
categoryAdapter.notifyDataSetChanged();
}
super.onActivityCreated(savedInstanceState);
}
}
I am using a main activity to show different fragments. I need to refresh one of fragment according to broadcast result. What I mention about is below.
My fragment class:
public class ConservationFragment extends Fragment
{
FragmentType fragmentType;
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
public ConservationFragment()
{
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public void onResume()
{
super.onResume();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_conservation, container, false);
try
{
this.fragmentType = (FragmentType) getArguments().getSerializable("fragmenttype");
ArrayList<Message> messageRecords = getMessagesFromDB(fragmentType);
recyclerView = (RecyclerView) rootView.findViewById(R.id.conservationsRecyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
mAdapter = new ConversationAdapter(messageRecords, getActivity());
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
return rootView;
} catch (Exception e)
{
Log.d("RUNTIME ERROR", "Please check ConversationFragment's onCreateView method.");
}
return inflater.inflate(R.layout.fragment_conservation, container, false);
}
public ArrayList<Message> getMessagesFromDB(FragmentType fragmentType)
{
..
}
}
How I initiate fragments in main activity ;
public class MainActivity extends AppCompatActivity
{
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
ConservationFragment personalFragment;
ConservationFragment blockedFragment;
SmsReceiver receiver;
ViewPagerAdapter adapter;
String[] tabTitle = {"PERSONAL", "BLOCKED"}; //
int[] unreadCount = {1, 2};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new SmsReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, intentFilter);
}
#Override
protected void onResume()
{
super.onResume();
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
try
{
setupTabIcons();
} catch (Exception e)
{
e.printStackTrace();
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
#Override
public void onPageSelected(int position)
{
viewPager.setCurrentItem(position, false);
}
#Override
public void onPageScrollStateChanged(int state)
{
}
});
}
private void setupViewPager(ViewPager viewPager)
{
Bundle personalCriterial= new Bundle();
personalCriterial.putSerializable("fragmenttype",FragmentType.PERSONAL);
Bundle blockedCriterial= new Bundle();
blockedCriterial.putSerializable("fragmenttype",FragmentType.BLOCKED);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
personalFragment = new ConservationFragment();
personalFragment.setArguments(personalCriterial);
blockedFragment = new ConservationFragment();
blockedFragment.setArguments(blockedCriterial);
adapter.addFragment(personalFragment, tabTitle[0]);
adapter.addFragment(blockedFragment, tabTitle[1]);
viewPager.setAdapter(adapter);
}
private void setupTabIcons()
{
for (int i = 0; i < tabTitle.length; i++)
{
tabLayout.getTabAt(i).setCustomView(prepareTabView(i));
}
}
private View prepareTabView(int pos)
{
View view = getLayoutInflater().inflate(R.layout.custom_tab, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
TextView tv_count = (TextView) view.findViewById(R.id.tv_count);
tv_title.setText(tabTitle[pos]);
return view;
}
public void receiveMessage(Intent intent)
{
//here I want to refresh one of my fragments. such as personalFragment..
}
}
Critical point is here; tab view should not go first tab.(Updatement should be in same fragment)
I am aware of code is so long but I have to show how I initiate objects to lead solutions. Thanks.
If you don't need to update the whole fragment, but only pass some data and refresh textviews or lists, you can always define public methods in fragment and call it from activity.
Example method in fragment:
public void updateWithData(String exampleString) {
myTextView.setText(exampleString);
}
Example usage in activity:
public void receiveMessage(Intent intent) {
myFragment.updateWithDate("Example string message");
}
This way, you don't need to recreate the whole fragment and you can update only relevant UI elements instead. However, keep in mind that it is up to you to make sure that fragment is created and shown when you will call this update method, otherwise you will get undefined behavior.
PS: It might be a good idea to create something like Updatable interface and implement it in your fragment, so you can reuse this update logic somewhere else in your application. But it's not required and for small scope, public methods should be sufficient enough.
I'm writing an Android application with ViewPager. My ViewPager contains two Fragments. In each fragment are located two receclerViews to show different LiveData lists of items from Database (I'm using Room)
The code for BaseFragment
public class BaseFragment extends Fragment {
private RecyclerView recyclerView;
private NewsAdapter adapter;
public ViewModel mViewModel;
public BaseFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(ViewModel.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_feeds, container, false);
recyclerView = mView.findViewById(R.id.feeds_recycler_view);
return mView;
}
#Override
public void onActivityCreated(#Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setUpRecyclerView();
mViewModel.getAllNewsEntities()
.observe(this, allNewsEntities -> {
NewsEntityUtilCallback productDiffUtilCallback =
new NewsEntityUtilCallback(adapter.getNewsItems(), allNewsEntities);
DiffUtil.DiffResult newsEntitiesDiffResult = DiffUtil.calculateDiff(productDiffUtilCallback);
adapter.setNewsItems(allNewsEntities);
newsEntitiesDiffResult.dispatchUpdatesTo(adapter);
});
}
protected void setUpRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
ItemClickListener onClickListener = (v, position) -> {
String url = adapter.getNewsItems().get(position).getLink();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (browserIntent.resolveActivity(getContext().getPackageManager()) != null) {
startActivity(browserIntent);
}
};
adapter = new NewsAdapter(getContext(), onClickListener);
recyclerView.setAdapter(adapter);
}
}
Look closely at this code section:
mViewModel.getAllNewsEntities()
.observe(this, allNewsEntities -> {
NewsEntityUtilCallback productDiffUtilCallback =
new NewsEntityUtilCallback(adapter.getNewsItems(), allNewsEntities);
DiffUtil.DiffResult newsEntitiesDiffResult = DiffUtil.calculateDiff(productDiffUtilCallback);
adapter.setNewsItems(allNewsEntities);
newsEntitiesDiffResult.dispatchUpdatesTo(adapter);
});
}
Here I'm going to observe different LiveData queries in each Fragment(in the second Fragment it's mViewModel.getAllBookmarkedNewsEntities()).The other things my code will be equals (the same lifecycles methods, the same RecyclerView). So could give me advice about the best possible design principle to refactor my code. I don't want simply to copy my code in another Fragment class just because of one line
Just extends the BaseFragment and override onActivityCreated method. Something like this:
public class ReuseBaseFragment extends BaseFragment {
#Override
public void onActivityCreated(#Nullable final Bundle savedInstanceState) {
//Your new code here
}
}
Only the first fragment is populated with the image, the second fragment is blank. I am using the same adapter for both fragments. I am not sure where is the problem.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mSectionsPagerAdapter.addFragment(PopularMoviesFragment.newInstance(SORT_POPULAR), "MOST POPULAR");
mSectionsPagerAdapter.addFragment(RatingMoviesFragment.newInstance(SORT_RATING), "TOP RATING");
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
public void switchToDetail(Intent intent){
startActivity(intent);
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
static class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
First fragment
public static PopularMoviesFragment newInstance(String sort) {
PopularMoviesFragment fragment = new PopularMoviesFragment();
Bundle args = new Bundle();
args.putString("sort", sort);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sortOrder = getArguments().getString("sort");
mContext = getActivity();
if (savedInstanceState != null) {
mPopularList = savedInstanceState.getParcelableArrayList(POPULAR_LIST);
for (Movie m : mPopularList) {
Log.d(LOG_TAG, "onCreate: " + m.getTitle());
}
} else {
FetchPopularMovies pm = new FetchPopularMovies();
pm.execute(SORT_POPULAR);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View pView = inflater.inflate(R.layout.fragment_popular_movies, container, false);
mAdapter = new MoviePosterAdapter(mContext, mPopularList);
mProgressBar = (ProgressBar) pView.findViewById(R.id.movies_progress_bar);
RecyclerView mRecyclerView = (RecyclerView) pView.findViewById(R.id.my_recycler_view);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return pView;
}
Second fragment
public static RatingMoviesFragment newInstance(String sort) {
RatingMoviesFragment fragment = new RatingMoviesFragment();
Bundle args = new Bundle();
args.putString("sort", sort);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sortOrder = getArguments().getString("sort");
mContext = getActivity();
if (savedInstanceState != null) {
Log.d(LOG_TAG, "Has saved instance");
mRatingList = savedInstanceState.getParcelableArrayList(RATING_LIST);
} else {
FetchRatingMovies rm = new FetchRatingMovies();
rm.execute(SORT_RATING);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_rating_movies, container, false);
ratingAdapter = new MoviePosterAdapter(mContext, mRatingList);
mProgressBar = (ProgressBar) view.findViewById(R.id.movies_progress_bar);
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(ratingAdapter);
return view;
}
MoviePosterAdapter
public class MoviePosterAdapter extends RecyclerView.Adapter<MoviePosterAdapter.ViewHolder> {
private final String LOG_TAG = MoviePosterAdapter.class.getSimpleName();
final Context mContext;
private ArrayList<Movie> mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
//public final View mView;
public final ImageView mImageView;
public ViewHolder(View view) {
super(view);
mImageView = (ImageView) view.findViewById(R.id.imageView);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MoviePosterAdapter(Context context, ArrayList<Movie> myDataset) {
this.mContext = context;
mDataset = myDataset;
}
public void setMoviesData(ArrayList<Movie> moviesData){
mDataset = moviesData;
}
// Create new views (invoked by the layout manager)
#Override
public MoviePosterAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(mContext).inflate(R.layout.movie_poster, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Movie mMovie = mDataset.get(position);
//Creating URL for image
final String POSTER_BASE_URL = "http://image.tmdb.org/t/p/";
final String SIZE_PATH = "w185";
String IMG_PATH = mMovie.getImg();
//Loading image using Glide
Glide.with(holder.mImageView.getContext())
.load(POSTER_BASE_URL + SIZE_PATH + IMG_PATH)
.placeholder(R.color.grid_placeholder_bg)
.into(holder.mImageView);
holder.mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(mContext, DetailActivity.class);
i.putExtra("id", mMovie.getId());
i.putExtra("title", mMovie.getTitle());
if (mContext instanceof MainActivity) {
MainActivity main= (MainActivity) mContext;
main.switchToDetail(i);
}
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.size();
}
}
Please help me!
EDIT: Both the arrays are not empty. Here is the issue I think, only one fragment gets populated, maybe because there is some problem with the adapter(not sure). If I try to stop first fragment to get populated(commenting out the notifyDatasetChanged()), then the second fragment works fine.
The image wasn't populating in the second fragment because it was an issue with Glide library. Switched to Picasso. Works fine.
I checked your code and i found that everything is fine. I tried with dummy data so i am damn sure that the mistake is in your data or adapter where you are setting data. Just try once with dummy data so you will be more clear or if still you are getting blank screen then let me know.
Generally blank screen comes because of there is no data. so please check once..