I want change an element of a fragment from MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);// => this is the error
...
list.setOnItemClickListener(new Adapter.OnItemClickListener() {//this can't work because list isn't in layout=>activity_main
....
I need manipulate 'lis't from MainActivity, but i can't, because the element "list" is in the fragment container.xml and isn't in activity_main.xml .
I can't move 'list' in activity_main.xml, because i need load into container.xml.
How can i call from MainActivity in the right way?
Why you need to perform action on fragments view in activity?
You can do all this inside your fragment.
Add that fragment to activity.
In fragments onCreateView method get the view and do whatever you want.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflatedView = inflater.inflate(R.layout.your_layout, container, false);
ListView list = (ListView) inflatedView.findViewById(R.id.list);// => this is the error
list.setOnItemClickListener(new Adapter.OnItemClickListener() {});
return inflatedView;
}
Although it is a bad idea and it is better that you find a better solution for your requirement, the answer is that you can make your ListView inside fragment public static, and then perform your action on it like:
YourFragment.list.setOnItemClickListener(new Adapter.OnItemClickListener()...
Related
How are these methods different from each other when trying to get the view?
First:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
listView = view.findViewById(R.id.listview);
return view;
}
Second:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = getActivity().findViewById(R.id.listview); }
* some say this is used to get activity views but i used it for getting fragment views(which didn't existed in the activity) and it worked fine.
Third:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = getView().findViewById(R.id.listview);
}
Three methods are good. In the onCreateView you create the view (!), it's the really first time you can use what you inflated. Then onViewCreated is called with the view you returned in the onCreateView, You can directly use the view given as parameter, it is the same the getView() provides. My advice is to initialise your UI variables here
For onActivityCreated, it is the best place to modify your UI elements. It is called when fragment creation is complete and when fragment is re-attached. There you can use the variables you initialised before, without having to get the activity just for that purpose.
I have created a ListView to list all the activities but when I wanna call the method, I can't find getListView
My activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListActivity"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
and in my MainActivity.java seems like there's an error with
getListView
method, I've no idea why
You can use getListView() when working with ListActivity not the usual Activity. In your code you got it wrong and made the ListView with id "a special identifier to the view" ListActivity, so to access it in your code you simply
ListView lv = (ListView) findViewById(R.id.ListActivity);
Your activity must inherit from a ListActivity. Your class should start with
public class MyListAdapter extends ListActivity {
...
}
Then you can use the method
getListView()
This is the official documentation
Second solution
If you use a fragment, then you can access your ListView by using
ListView lv = (ListView) rootView.findViewById(R.id.id_of_your_listView);
The rootView is the view you get in your onCreateView Method of the Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.example_fragment, container, false);
return rootView;
}
Try using (ListView)findViewById(R.id.ListActivity) instead of getListView()
If you have the ListView in the fragment you can use:
ListView list = (ListView)findViewById(android.R.id.list);
But if you want to use getListView directly you must call the ListView's id as
android:id="#android:id/list"
Hope this help.
also note that you dont pass your ListView to the adapter but you pass the adapter to the ListView
ListView list = (ListView)findViewById(R.id.ListActivity);
list.setAdapter(adapter);
You can't extend ListActivity because 1) You are wanting to use Fragments and 2) there isn't a method setSupportActionBar for the ListActivity class...
Anyways... in your question
My activity_fragment.xml
<!-- XML code -->
You can't use findViewById to get that ListView within the Activity class because your ListView isn't within activity_main.xml.
You have to do it from within the Fragment.
public class YourFragment extends Fragment {
private ListView yourListView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_fragment, container, false);
this.yourListView = (ListView) v.findViewById(R.id.listView);
// adapter stuff...
return v;
}
}
i got a problem here. As the title said, i want to update a listview in a fragment adding a value from an edittext of another fragment. My main activity is a navigation drawer. My fragment with the listview is the Fragment_hotels and the other fragment witch contains the edittext is the FragmentAddHotel. I want to add in the listview (with the hotels) a new hotel.
Can someone explain how to do that?
Code given below:
Fragment_hotels:
public class Fragment_hotels extends Fragment {
public static boolean flag=false;
public void Add(){
Target.add("Hotel");
Target.add("Hotel1");
Target.add("Hotel2");
}
static ArrayList<String> Target = new ArrayList<String>();
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_hotels, container,false);
ListView listView = (ListView) view.findViewById(R.id.list);
if (flag==false){
this.Add();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),R.layout.mytextview,R.id.tv, Target);
adapter.setNotifyOnChange(true);
// Assign adapter to ListView
listView.setAdapter(adapter);
return view;
}
}
And FragmentAddHotel:
public class FragmentAddHotel extends Fragment {
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_add_hotel, container, false);
return rootView;
}
}
I got an edittext and a button on my FragmentAddHotel layout.
How can i get the value from edittext and add it on the listview?
Sorry if a similar question has already been answered but i am junior programmer.
Tnx in advance :)
To update view in class from another one you can use EventBus library here is the link
Just register you first class by adding:
EventBus.getDefault().register(this); //in your listener class that contains the listview at onCreate method.
Also implement your receiver method:
public void onEventMainThread(String hotelName) {
// Add the hotelName in your Target array list and then notify your adapter by adapter.notifyDataSetChanged();
}
To send data from your fragment just use :
EventBus.getDefault().post("edittextvalue");
Also don't forget to call :
EventBus.getDefault().unregister(this); // In your listener class onDestroy() method.
In YourActivity:
Fragment_hotels frag_hotels;
void addItem(String item){
frag_hotels.addItemToList(item);
}
Fragment_hotels:
List<String> hotelList;
ArrayAdapter<String> adapter;
void addItemToList(String item){
hotelList.add(item);
adapter.notifyDataSetChanged();
}
In FragmentAddHotel:
// you should call whenever adding is need in fragment
((YourActivity)getActivity()).addItem("Your Item");
Through YourActivity, you can communicate each fragment.
Here's my code in the main activity don'tn know why error in findViewById occurs
.. this is just my first time in JSON paarsing .
public class HomeFragment extends Fragment{
public HomeFragment(){}
View rootView;
ListView list;
ActorsAdapter adapt;
ArrayList<Actors> actorslist;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
list = (ListView)findViewById(R.id.listFeeds);
actorslist = new ArrayList<Actors>();
return rootView;
new ActorsAsynTask().execute("http://jcasim.5gbfree.com/Project/login.php");
}
listFeeds It's a component I'm pretty sure is a component that lives in your fragment. Since you haven't return the root view yet, the findViewById is not going to find it. you have to wait until it's on screen to ask for it using findViewById (in onStart() method) or ask it form the root view before you return it.
it would be list = (ListView)rootView.findViewById(R.id.listFeeds);
The View of the Fragment is not set before the onCreateView() returns. Therefore the findViewById() tries to find the specified R.id.listFeedsfrom a null reference.
Try this instead list = (ListView)rootView.findViewById(R.id.listFeeds);
I have slider menu on which I use fragments.I want to show list view (json data on list view) on fragment.That's my code
public class FragmentList extends Fragment {
ListView sweepstakes_list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_home, null);
//here I use my code
return view;
}
}
how do I show dynamic listview in fragments?
Use ASYNC task.
call async task whenever you want to update your list view.