I cannot use my arraylist in class fragment - java

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());

Related

setAdapter(T) cannot be applied to (android.widget.ListAdapter) when using fragment

I am getting an error at autoCompleteTextView it says setAdapter(T) cannot be applied to (android.widget.ListAdapter)
public class HomeFragmnet extends Fragment {
TextInputLayout textInputLayout;
AutoCompleteTextView autoCompleteTextView;
ListView listView;
ListAdapter listAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstancesState){
View v =inflater.inflate(R.layout.fragment_home, null);
textInputLayout = (TextInputLayout) v.findViewById(R.id.menu_dropdown);
autoCompleteTextView = (AutoCompleteTextView) v.findViewById(R.id.drop_items);
String [] items={"Pune", "Mumbai", "Nashik"};
listAdapter = new ArrayAdapter<String>(getActivity() , R.layout.items_list, items );
autoCompleteTextView.setAdapter( listAdapter );
return v;
}
}
According to the docs of AutocompleteTextView its setAdapter method takes a <T extends ListAdapter & Filterable>, meaning the adapter must both extend ListAdapter and implement Filterable.
ListAdapter itself does not implement Filterable, but some subclasses such as ArrayAdapter do.

why setVisibility in fragment doesn't work?

the ProgressBar doesn't disappear in the fragment also items aren't loaded in the RecyclerView, When the code was in the body of onCreate of the main activity ,it was working
I am making fragment that contains ProgressBar and RecyclerView with retrieving data from firebase database , I added setVisibility at the end of onDataChange method so that after getting all the data and store it in the array list , the ProgressBar disappear
RecyclerView offersRecycler;
ArrayList<offer> offers;
OffersAdapter offersAdapter;
View fragment_layout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false);
// Offers RecyclerView
offersRecycler = fragment_layout.findViewById(R.id.offersRecycler);
offers = new ArrayList();
offers_init();
offersAdapter = new OffersAdapter(offers,getContext());
offersRecycler.setAdapter(offersAdapter);
offersRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_offers, container, false);
}
private void offers_init() {
DatabaseReference db_offers = db_ref().child("offers");
db_offers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
ProgressBar loadingOffers = fragment_layout.findViewById(R.id.loadingOffers);
loadingOffers.setProgress(10);
for (DataSnapshot offer_item : dataSnapshot.getChildren()) {
String id = offer_item.getKey();
String title = offer_item.child("title").getValue().toString();
String rating = offer_item.child("rating").getValue().toString();
String orders_number = offer_item.child("orders_number").getValue().toString();
offer offer_object = new offer(id,title, rating, orders_number);
offers.add(offer_object);
offersAdapter.notifyDataSetChanged();
}
Toast.makeText(getContext(),"title",Toast.LENGTH_LONG).show();
loadingOffers.setVisibility(View.GONE);
}catch (Exception ex){
Toast.makeText(getContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
}
I expect the ProgressBar to hide and the array loaded into the RecyclerView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false);
// Offers RecyclerView
offersRecycler = fragment_layout.findViewById(R.id.offersRecycler);
offers = new ArrayList();
offers_init();
offersAdapter = new OffersAdapter(offers,getContext());
offersRecycler.setAdapter(offersAdapter);
offersRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_offers, container, false);
}
^ This is your code snippet. What you are doing is - inflating a layout fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false) and using fragment_layout to get the instance of RecyclerView. This is fine.
But at the end of the method, you are inflating a new layout and returning that. So the Android framework uses that View instead of fragment_layout to set the Fragment view. All the views in fragment_layout won't be shown since it's never added to the fragment and hence you don't see any changes in the RecyclerView.
At the end of the onCreateView method, you should just return fragment_layout.
in your CreateView, change
return inflater.inflate(R.layout.fragment_offers, container, false);
by
return fragment_layout;
You call the layout, init recyclerview, but recall the "blank" layout
Also, avoid underscore for method or variable name, it's not the java convention

RecycleView adapter not adding to the view

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());

Refreshing fragment in Android

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;
}

Android Viewpager inside of Viewpager not showing layout

I have been tasked with creating a very unorthodox layout that can only be accomplished with a viewpager inside the second pane of another viewpager, thankfully the outer viewpager needs to be locked so the task was a little more realistic, however the unexpected issue arose where after the inner viewpager was loaded onCreateView is never called, I can see that it is sliding side to side so the inner viewpager is setup but it will not load any inner view for some odd reason, any help will go a long way thanks
Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_fragment);
pagePosition = 0;
setupViews();
}
private void setupViews() {
//setup images to be changed later
ivCategory = (ImageView) findViewById(R.id.ivCatergory);
ivHome = (ImageView) findViewById(R.id.ivHome);
ivProfile = (ImageView) findViewById(R.id.ivProfile);
//setup the viewpager
pager = (CustomViewPager) findViewById(R.id.viewpager);
//add fragments to the fragment array
pageList.add(NavigationFragment.newInstance(0, getApplicationContext(), this));
pageList.add(NavigationFragment.newInstance(1, getApplicationContext(), this));
pageList.add(NavigationFragment.newInstance(0, getApplicationContext(), this));
// initialize the page adapter
pageAdapter = new FragmentHomeAdapter(getSupportFragmentManager(), pageList);
//set the page adapter to the viewpager
pager.setAdapter(pageAdapter);
//set homepage to be first
pager.setCurrentItem(1);
//disable swiping to mimic iphone behaviour
pager.setPagingEnabled(false);
}
viewpager
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int pageType = getArguments().getInt(PAGE_TYPE);
View v;
if (pageType == 1) {//all inner fragments will happen here
v = inflater.inflate(R.layout.fragment_container, container, false);
//setup the inner viewpager
pager = (CustomViewPager) v.findViewById(R.id.viewpager);
pageList.add(HomeFragment.newInstance(0, context, getActivity()));
pageList.add(HomeFragment.newInstance(1, context, getActivity()));
// initialize the page adapter
pageAdapter = new FragmentHomeAdapter(getActivity().getSupportFragmentManager(), pageList);
//set the page adapter to the viewpager
pager.setAdapter(pageAdapter);
pager.setPagingEnabled(true);
}
return v;
}
inner viewpager
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v;
v = inflater.inflate(R.layout.fragment_list_feed, container, false);
lv = (ListView) v.findViewById(R.id.lvFeed);
listAdapter = new ListHomeAdapter(getArguments().getInt(PAGE_TYPE), feedListItems, getActivity());
//add header
header = (ViewGroup) inflater.inflate(R.layout.list_item_header, lv, false);
lv.addHeaderView(header, null, false);
lv.setAdapter(listAdapter);
if (getArguments().getInt(PAGE_TYPE) == 0) {
new getFeedListItems().execute();
} else {
new getFeedListItems().execute();
}
Toast.makeText(context, "This never shows",
Toast.LENGTH_SHORT).show();
return v;
}
When showing Fragments inside another Fragment, use getChildFragmentManager() instead of getSupportFragmentManager()
Initialize your pageAdapter this way:
pageAdapter = new FragmentHomeAdapter(getChildFragmentManager(), pageList);

Categories