#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_schedule, container, false);
// array adapter + list fragment
arrayAdapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_list_item_1_custom,
schedules.get(getArguments().getInt(ARG_SECTION_NUMBER) - 1)); //
arrayAdapter.setNotifyOnChange(true);
setListAdapter(arrayAdapter);
return rootView;
}
This is the onCreateView of my class that extends ListFragment, I am trying to update the contents of the list from my activity, so far I've tried clearing the arrayAdapter and calling notifyDataSetChanged on it, and it doesn't work. The only way I can get my list to update is if I swipe two tabs and go back again, then it shows all the changed values. Any suggestions?
This is the onCreateView of your fragment. This is fired every time when you create your fragment or swipe between tabs. As I understand, you just add values to the db onClick and when you go and come back your fragment you see the new values.
Please, try adding new values to your adapter on onClick button then try updating your list in the same scope with the NotifyDataChanged mechanisim of the adapter.
If I get wrong idea about whay you are doing pleas share more code.
Related
public class Home extends Fragment {
ArrayList<String> notes = new ArrayList<String>();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ListView listView = (ListView) getActivity().findViewById(R.id.listView);
notes.add("Example note");
ArrayAdapter arrayAdapter =new ArrayAdapter(this, android.R.layout.simple_list_item_1,notes);
listView.setAdapter(arrayAdapter);
return inflater.inflate(R.layout.home,container,false);
}
when i use this it gives me an error, tried using getactivity or getcontext and both crash the app when i do it
where did i go wrong
any suggestions are welcomed and thanks in advance <3
You must get corresponding fragment view by below 2 lines
View myView = inflater.inflate(R.layout.fragment_view, container, false);
ListView lv = myView.findViewById(R.id.listView);
getActivity() may be null while your fragment is in process of preparation.
Just inflate your fragment layout in onCreateView and place ListView logic to onViewCreated() or onActivityCreated
Get listview from fragment by inflate, not from parent activity
ArrayAdapter needs a context as the first parameter, so either use this.getContext() or this.getActivity() instead of passing this, which is a fragment.
Furthermore, you should move the code into onViewCreated method, as the view must be inflated before you access its elements.
And you have to use view.findViewById() instead of getActivity().
For some reason Fragments do not inherit the context from an Activity. Try this:
ListView listView = ((ListView) getActivity()).findViewById(R.id.listView);
Same works for getContext.
Context context = ((ListView) getActivity)).getApplicationContext();
For some reason my recyclerView doesn't attach to the Firebase RecyclerView adapter. I'm implementing the recyclerView inside a fragment that shows in a tabbed activity. my code (relevant parts):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = LayoutInflater.from(container.getContext())
.inflate(R.layout.fragment_fresh, container, false);
// Initialize ProgressBar and RecyclerView.
mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
mSessionRecyclerView = (RecyclerView) rootView.findViewById(R.id.sessionRecyclerView);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setStackFromEnd(true);
mSessionRecyclerView.setLayoutManager(mLinearLayoutManager);
//implement recyclerview
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
mFirebaseAdapter = new FirebaseRecyclerAdapter<Session, SessionViewHolder>(
Session.class,
R.layout.item_session,
SessionViewHolder.class,
//change back to SESSIONS_CHILD
mFirebaseDatabaseReference.child("test")) {
#Override
protected void populateViewHolder(final SessionViewHolder viewHolder,
Session session, int position) {
//implementing populateViewHolder..
}
};
mSessionRecyclerView.setLayoutManager(mLinearLayoutManager);
mSessionRecyclerView.setAdapter(mFirebaseAdapter);
return inflater.inflate(R.layout.fragment_fresh, container, false);
}
}
The XML is just a recyclerView and a progressBar.
Worth mentioning that I've gone through this thread.Tried setting recycler's size to wrap_parent, move setAdapter to onCreate, and pretty much every other answer - to no avail.
EDIT: I got this working when inside an activity, but whenever I try from within a fragments I get this error. Also tried with a regular recyclerView and a custom adapter and the same thing happens.
Does the recyclerView attaches itself to an adapter differently in a fragment?
OK, I finally fixed it. The problem was actually in the tabbed activity - the getItem method inside the PlaceHolder fragment created a new instance of PlaceHolder.
Therefore, my fragments hadn't even been created.
None of the threads on the skipping layout advise checking that, hope this helps.
Although there are many questions about nested fragments but still it got me stumped in this case.I am making an android app which has an Activity having a frame layout.I am loading a fragment(say Fragment B) into the frame layout.Fragment B has a ViewPager that contains two fragments.First fragment of viewpager(say Fragment VP1) has a gridview that loads images from network.Now on griditem image click I want to show the image in full size in a new fragment which has NetowrkImageView. How do i do this? I tried need to call getChildFragmentManager(),but didn't work. If you guys need code I'll show that. Thanks in advance.
onCreateView() of VP1
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_images_and_videos,null);
gridItemList=new ArrayList<>();
gridview= (GridView) view.findViewById(R.id.gridview);
adapter=new ImagesVideosGridviewAdapter(getActivity(),gridItemList);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// GridItemModel gridItem= (GridItemModel) parent.getItemAtPosition(position);
Bundle args=new Bundle();
args.putString("url",gridItemList.get(position).getUrl());
FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container_event_specific,new ShowFullImageFragment()).commit();//frame_container_event_specific is present in the main activity.
}
});
new LoadMedia().execute("");
return view;
}
Logcat error
java.lang.IllegalArgumentException: No view found for id 0x7f0d008a (com.test.rajat.a10times:id/frame_container_event_specific) for fragment ShowFullImageFragment{ccc6a0 #0 id=0x7f0d008a}
Because your VP1 layout doesn't contain R.id.frame_container_event_specific, the fragment manager is unable to find the view. It's in the activity layout, so you better let the fragment manager from the activity to replace the fragment.
But think again about using the fragment approach. I think it's much simpler and easier to make the full image fragment as a standalone Activity. It displays full image anyway. Otherwise, even though you are able to replace the fragment correctly, you have to do a lot to manage the fragment back stack, like replacing the previous fragment when user hits back button.
I am beginner in android.
I was having a requirement similar to what is discussed here:
Separate Back Stack for each tab in Android using Fragments.
Now, I am using that project given here gitHub
What I want is to add listview to a fragment..Further on clicking a particular item in list another fragment is visible.
But I am facing a problem in doing it.
For eg here,I get error in using setListAdapter(adapter) function.
Plz help.
public class AppTabAFirstFragment extends BaseFragment {
private Button mGotoButton;
//private String[] characters= {"shs","sds","sdss","sdsd"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_tab_a_first_screen, container, false);
mGotoButton = (Button) view.findViewById(R.id.id_next_tab_a_button);
mGotoButton.setOnClickListener(listener);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),R.layout.app_tab_d_first_screen, characters);
// setListAdapter(adapter);
return view;
}
In this way,it doesn't allow us to use setListAdapter() function reason being I can't extend ListActivity. I am all confused.
Kindly help.
I haven't checked your links, but as I can see from the little code you posted I think this might be the problem:
You need to lookup your listview from the xml file (if you have defined it there) and set the adapter to that object. For instance:
ListView listView = (ListView) view.findViewById(R.id.my_list_view);
listView.setAdapter(adapter);
When a layout , which is defined for a Fragment and used inside setContentView() method and it has no relation with Activity .
then why we need Activity Reference to access it .
so I want to create a ListView , inside a Fragment . than i have to create it inside Activity layout or inside Fragment layout .
Hey Neil , i have a question because every time I have made a mistake because , i am performing this
TextView txt = ( TextView ) getActivity().findViewById(R.id.txt);
txt.setText("nayak nahi , khalnayak hun mein");
inside oncreateView() so it is creating problem but when ever i am implementing this inside
onActivityCreated() , it is working , so can you please tell me Why this is happening .
if you want to find the view by getActivity(), you should do that after onCreateView is called, because till onCreateView is called the view of fragment is null. So you can do:
#Override
public void onResume() {
super.onResume();
TextView txt = ( TextView ) getActivity().findViewById(R.id.txt);
txt.setText("nayak nahi , khalnayak hun mein");
}
You can define a ListView in a layout for your Fragment. Try something like this in your fragment to define a alternative layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
_baseView = inflater.inflate(R.layout.yourlayout, null); //where base view is your upper most parent in your layout
return _baseView;
}
Then in your onActivityCreated you can use _baseView.findViewById('yourListView') to set your ListView. I don't know if this is best practice but it works perfectly for me.
In Fragment you should use its View but getActivity:
public View onCreateView(…){
View rootView = inflater.inflate(R.layout.listview_xml_file, container, false);
ListView ListNewsBelarus = (ListView) rootView.findViewById(R.id.listViewId);
return rootView;
}