I am trying to implement a RecycleView adapter that has CardViews as each row. The card views do not show up even though I have the adapter created and attached to the view of the activity.
I am using the Tabbed activity and trying to modify the OnCreateView() method.
As seen in this picture I get nothing in the recycle view but I am expecting "Test Title" and "Test Message" in a CardView.
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_TAB_NUMBER = "tab_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int tabNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_TAB_NUMBER, tabNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_coversations, container, false);
RecyclerView recycle = (RecyclerView) rootView.findViewById(R.id.recycleView);
List<RecyclerData> list = new ArrayList<>();
RecycleViewAdapter adapter = new RecycleViewAdapter(list);
recycle.setAdapter(adapter);
RecyclerData r = new RecyclerData("Test Title", "Test Message", 0);
list.add(r);
adapter.notifyDataSetChanged();
return rootView;
}
}
You need to set a layout manager to your recycler view. In your case try adding a LinearLayoutManager to your recycler view.
// use a linear layout manager
recycle.setLayoutManager(new LinearLayoutManager(getActivity());
Related
I have a ListView in a Fragment that is populated when the app starts.
I put a ParcelableArrayList in a Bundle in my newInstance method, and I get it back in my OnCreateView after passing the ArrayList in the newInstance method in my Activity (which is the data read from the SQLite database).
This part works, as I display my data in my Fragment correctly.
I implemented a button that removes all data from the table, and I would now like to update my view after I cleaned the table.
The remove all button is handled in my main activity where I call my database handler to empty the table.
What is the best way to do that ? Here are the parts of the code that seem relevant to me :
My Fragment class :
public class MainFragment extends Fragment {
public static final String RECETTES_KEY = "recettes_key";
private List<Recette> mRecettes;
private ListView mListView;
public MainFragment() {
// Required empty public constructor
}
public static MainFragment newInstance(List<Recette> r) {
MainFragment fragment = new MainFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(RECETTES_KEY, (ArrayList<? extends Parcelable>) r);
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRecettes = getArguments().getParcelableArrayList(RECETTES_KEY);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
configureListView();
}
// Configure ListView
private void configureListView(){
this.mListView = getView().findViewById(R.id.activity_main_list_view);
RecetteAdapter adapter = new RecetteAdapter(getContext(), mRecettes);
mListView.setAdapter(adapter);
}
}
Relevant parts from my Main acivity :
This is in my OnCreate method :
mDatabaseHandler = new DatabaseHandler(this);
mRecettes = mDatabaseHandler.readRecettes();
mDatabaseHandler.close();
This is in the method I use to show a fragment :
if (this.mMainFragment == null) this.mMainFragment = MainFragment.newInstance(mRecettes);
this.startTransactionFragment(this.mMainFragment);
Let me know if I should add more of my code, this is my first time posting :)
Lucile
In your R.layout.fragment_main, you can add an id to the root view, say with android:id#+id/fragment_root
And whenever you want to change the fragment view:
In activity:
MainFragment fragment = (MainFragment) getSupportFragmentManager().getFragmentById(R.id.fragment_root);
fragment.updateList(mRecettes);
And then create the new method updateList() in your MainFragment
public void updateList(List<Recette> recettes) {
mRecettes.clear();
mRecettes.addAll(recettes);
configureListView();
}
Also you can tag your fragment when you add it to your transaction instead of using its id, and then use getSupportFragmentManager().getFragmentByTag()
I tried to use arraylist in fragment
my arraylist not work in fragment class.
please help me fix this
before I use this code in main activity, and then I added menu fragment I move this code to this fragment
and then my arraylist get error
this error " 'RelativeLayout(anroid.content.Context)' in 'android.widget.RelativeLayout' cannot be applied to '(com.cupaxxhd.mysurah.HomeFragment)' "
and this " 'MyAdapter(android.content.Context, java.util.Arraylist)' in 'com.cupaxxhd.mysurah.Myadapter' cannot be applied to '(com.cupaxxhd.mysurah.HomeFragment, java.util.Arraylist)'
public class HomeFragment extends Fragment {
RecyclerView mRecyclerView;
MyAdapter myAdapter;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
mRecyclerView = v.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new RelativeLayout(this));
myAdapter = new MyAdapter(this, getMyList());
mRecyclerView.setAdapter(myAdapter);
return v;
}
private ArrayList<Model> getMyList(){
ArrayList<Model> models = new ArrayList<>();
Model m = new Model();
m.setTitle("Al-Lahab");
m.setDescription("Surah Al-Lahab adalah surat ke-111 dalam Al-Qur'an.");
m.setDetails("تَبَّتْ يَدَا أَبِي لَهَبٍ وَتَبّ\n"
);
m.setImg(R.drawable.al_lahab);
models.add(m);
return models;
}
You need to pass Context instead of fragment instance and use LayoutManager instead of RelativeLayout. Like this:
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
new MyAdapter(getActivity(), getMyList());
I've created activity from TabbedActivity template (in Android Studio):
This template uses Fragment and FragmentPagerAdapter to handle tabs.
This is code of two classes, PlaceholderFragment extends Fragment, and SectionsPagerAdapter extends FragmentPagerAdapter:
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);
int scnum = getArguments().getInt(ARG_SECTION_NUMBER);
final ListView listView = (ListView) rootView.findViewById(R.id.products_list);
// here refresh my listView trough internet
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
In onCreateView method I refresh my ListView. This method is called when i switch tabs.
But I need to refresh ListView not when user switch tabs, but:
1. every 10 seconds
2. when user comes back to this activity (onRestart() method is called)
I have no idea how to access my ListView outside onCreateView method.
I've already tried many solutions from the internet to call onCreateView method from onRestart method, and just to access my ListView from onRestart(). None of them worked.
Globally in Activity class I have an instance of SectionsPagerAdapter and ViewPager:
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
They are initalized in onCreate by this way:
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
I think, there are 2 possible solutions, access my Fragment's view or just recall onCreateView.
I've already tried to do:
This one just doesn't work:
#Override
protected void onRestart() {
super.onRestart();
mSectionsPagerAdapter.notifyDataSetChanged();
}
This one crashes app because of NullPointerException:
#Override
protected void onRestart() {
super.onRestart();
Fragment f = mSectionsPagerAdapter.getItem(0); // tab with my list view is first tab. ( getItem(1) also does not work )
View v = f.getView(); // v is null here, however f isn't.
final ListView listView = v.findViewById(R.id.products_list); // v is null, NullPointerException is thrown.
// here refresh my listView trough internet
}
EDIT:
Since I started using Kotlin, answer based on that language will be also accepted. However, I am new to Kotlin, so more detailed answer would be needed then.
So you have a list in a Fragment that is inside a ViewPager. Ther are a few ways to solve your problem, I'd suggest you use the first one, because it is the simplest.
Fragment have lifecycle callbacks that are triggered together with lifecycle callbacks of Activity that contains that fragment. It covers most of the methods, but unfortunately not the onRestart that you are looking for. But that's not a big problem. Actually, you almost never need onRestart because you have onStart method, that method is accessible from both Fragments and Activities. It is called every time activity is restarted + the very first time activity is started. And as we can see it is exactly what you need. So to have your list updated every time, just remove the update code from the onCreate method and put it into onStart method of the Fragment.
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private ListView listView = null;
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);
int scnum = getArguments().getInt(ARG_SECTION_NUMBER);
listView = (ListView) rootView.findViewById(R.id.products_list);
return rootView;
}
#Override
public void onStart() {
// here refresh your listView trough internet using the listView class field
}
}
Another option would be to get a reference to your fragment inside the activity. The solution I usually use is to retain a reference to the fragment inside the adapter. It is similar to the second option in your question. The only problem with your solution is that mSectionsPagerAdapter.getItem(0); always create a new fragment, while you need to get a reference to already existing fragment.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private PlaceholderFragment fragmentZero = null;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
PlaceholderFragment tabFragment = PlaceholderFragment.newInstance(position + 1);
if (position == 0) {
fragmentZero = tabFragment;
}
return tabFragment;
}
#Override
public int getCount() {
return 3;
}
public PlaceholderFragment getFragmentZero() {
return fragmentZero;
}
}
You also need to move your list update logic to a separate method in the PlaceholderFragment:
private ListView listView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);
int scnum = getArguments().getInt(ARG_SECTION_NUMBER);
listView = (ListView) rootView.findViewById(R.id.products_list);
refreshListView();
return rootView;
}
public void refreshListView() {
// here refresh your listView trough internet
}
At this point you can do:
#Override
protected void onRestart() {
super.onRestart();
Fragment listFragment = mSectionsPagerAdapter.getFragmentZero();
listFragment.refreshListView();
}
Whenever you have a fragment inside an activity, in a ViewPager or not, you can get a reference to the fragment using FragmentManager. Check this for details regarding the ViewPager option.
All the code above is rather straightforward and would not require any special Kotlin idioms if you'd prefer to implement it in Kotlin. You can start with converting your classes in the android studio. To do it:
Navigate to your java class
Press Control + Shift + A on Windows or Command + Shift + A on Mac
Search for Convert Java File to Kotlin File
Apply
And you are good to go with the Kotlin code of given classes. The only thing that might be quite different from java is handling ARG_SECTION_NUMBER since Kotlin does not have static fields and use Companion Objects instead.
I have a fragment which doesn't contain anything at the application start, but after the data is loaded it should show the loaded data. I do all my data loading in MainActivity in function onDataLoaded which implements DataLoadedListener. After the data is loaded the function sends the data (ArrayList) as a parameter through the function call (initializeAdapter()). Then, the function initializeAdapter() gets it and initialize custom made adapter (adapter is a global object, initialized in initializeAdapter() and used in onCreateView()).
I guess that I should refresh fragment view after the data is loaded and the adapter is initialized, but I don't know how to do it. I tried in a lot of ways but didn't make it.
onDataLoaded function in MainActivity which calls initializeAdapter()
#Override
public void onDataLoaded(List<Grad> gradovi, List<Ponuda> ponude) {
Spinner spinnerGradovi = (Spinner) findViewById(R.id.gradovi_spinner);
ArrayAdapter<String> adapterGradovi;
List<String> listaGradova = new ArrayList<>();
ArrayList<Ponuda> ponudaArrayList = new ArrayList<Ponuda>();
ponudaLista = ponude;
gradLista = gradovi;
for(Grad grad : gradovi ){
listaGradova.add(grad.getNaziv());
}
for(Ponuda ponuda : ponude){
ponudaArrayList.add(ponuda);
}
adapterGradovi = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listaGradova);
spinnerGradovi.setAdapter(adapterGradovi);
Fragment fragmentGet = svePonudeFragment;
((SvePonudeFragment)fragmentGet).initializeAdapter(ponudaArrayList);
}
and this is a Fragment
public class SvePonudeFragment extends Fragment {
private RecyclerView rv;
RVAdapter adapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
rv.setAdapter(adapter);
return rootView;
}
public void initializeAdapter(List<Ponuda> preuzetePonude){
adapter = new RVAdapter(preuzetePonude);
//this isn't working
/*FragmentManager manager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
ft.detach(this).attach(this).commit();*/
//this is not working, neither
Fragment frg = getFragmentManager().findFragmentByTag("sve_ponude_fragment_tag");
final android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
I am also interested in how to make an empty adapter which loads if data is not loaded yet? And when the data is loaded to fill out another adapter and use it.
Initialize adapter before you set. If you interested to change you approach I can give suggestions: Create a constructor for you fragment which you can pass the ArrayList. OnCreate or OnCreateView you can initialize the adapter and pass the list into. After setting adapter notifyDataChanges and to check if there is data please use some Logs when you pass the data and before you pass the data. Do not directly blame the adapter or fragment control your data. Besides I suggest you to study more using fragments + RecyclerView. I am posting one example that you can look at but the data load in the fragment.
public class CategoryFragment extends Fragment {
//Class Tag
private static final String TAG = CategoryFragment.class.getSimpleName();
//Fragments base context
private Context mContext;
//RecyclerView widget
private RecyclerView mRecyclerView;
//RecyclerView's Layout Manager
private RecyclerView.LayoutManager mLayourManager;
//RecyclerView's Adapter
private CategoryFragmentAdapter mCategoryFragmentAdapter;
//Category Data List
private ArrayList<Category> mCategoryList;
//Interface object
onCategoryItemClick mCallBackCategory;
#Override
public void onAttach(Context context) {
super.onAttach(context);
//Instantiate base context
mContext = context;
//Instantiate interface
mCallBackCategory = (onCategoryItemClick)getActivity();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Instantiate Category List
mCategoryList = new ArrayList<Category>();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//Custom fragment view instantiating
View view_fragment_category = inflater.inflate(R.layout.fragment_items, container, false);
//Instantiating Fragment's recycler view
mRecyclerView = (RecyclerView) view_fragment_category.findViewById(R.id.recycle_categories);
//Instantiate RecycleView's Layout Manager: LinearLayoutManager is one of the default
mLayourManager = new LinearLayoutManager(mContext);
//Setting layout manager for recycler view
mRecyclerView.setLayoutManager(mLayourManager);
//Retrieve data from server, populate the category data list and set the RecyclerView's adapter
getCategories();
//Instantiate the adapter and push the data
mCategoryFragmentAdapter = new CategoryFragmentAdapter(mContext, mCategoryList);
//RecyclerView item click listener
mCategoryFragmentAdapter.SetOnItemClickListener(new CategoryFragmentAdapter.OnItemClickListener() {
#Override
public void onItemClick(View v , int position) {
//Test display
//Toast.makeText(mContext, mCategoryList.get(position).getId(),Toast.LENGTH_LONG).show();
//Push to Activity method with clicked item ID
mCallBackCategory.proceedToProducts(mCategoryList.get(position).getId());
}
});
return view_fragment_category;
}
I am trying to use the method
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new MappingPage();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
I have written a class called MappingPage(), if I try to use the above method, the fragment I create is of type MappingPage because has been extended and hence throw an error as the function returns a fragment (not a MappingPage Object)
EDIT:I am getting an error when I do
Fragment fragment = new MappingPage();
Eclipse tell me that I need to change fragment to type MappingPage, which means i have to change the return type of the function
two Questions
1) How are you supposed to put your custom fragments into this?
2) Why does the dummySectionFrament return a Fragment Object and not a DummySectionFragment object?
thanks in advance
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
here is my class
public class MappingPage extends Fragment
{
private MapView map;
private MapController myMapController;
public static final String ARG_SECTION_NUMBER = "1";
public MappingPage() {
}
public View onCreateView (LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Context context = new ContextThemeWrapper(getActivity(), R.style.fragment_theme);
//LayoutInflater Inflater = inflater.cloneInContext(context);
View view=inflater.inflate(R.layout.fragment_main_dummy, container, false);
return view;
}
#Override
public void onResume() {
map = (MapView)getActivity().findViewById(R.id.openmapview);
map.setBuiltInZoomControls(true);
myMapController = map.getController();
myMapController.setZoom(15);
super.onResume();
}
}
Regarding your first question, this should work. MappingPage is an indirect instance of the Fragment class, and you can therefore return it.
About question two, the method that you call always returns a specific type, like Fragment in the case of getItem(). However, this can be a subclass of that as well, and if you are sure it is, you can safely cast it to that subclass.