how to show dynamic listview in fragments? - android

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.

Related

Which one of these is the best way to access android fragment views?

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.

How change a element of fragment.xml from MainActivity.java

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

getting force close when using findviewbyId()

I'm starting to learn android app development and currently practicing with simple apps. I'm writing this app that includes a listview and i want to fill it with fake data but, when i use findViewbyId() in my code the app gives me force close BUT when i set the content view to my fragment instead of activity_main i get no force close, but the app shows me an empty list.
This is my code :
package com.example.karen.please;
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray =
{
"sunny",
"cloudy",
"asteroids",
"rainy",
"sunny"
};
List<String> weekforecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter =
new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekforecast
);
ListView mylistview = (ListView)container.findViewById(R.id.listview_forecast); //screws upp in this line
mylistview.setAdapter(mForecastAdapter);
return inflater.inflate(R.layout.fragment_main, container, true);
}}
Any help would be appreciated.
You're trying to find a view before you inflate your layout... Try inflating at the top of your method, like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_main, container, true);
<all the other init code you have>
return myView;
}
First you need inflate your layout...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
...
ListView mylistview = (ListView)rootView.findViewById(R.id.listview_forecast);
...
Before using findViewById you should Create a View variable, use the inflate.. line, use findViewById on the View variable you created, and then return that.
This is what your code should look like:
package com.example.karen.please;
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Instead of inflate and return instantly, store it in a variable,
// Then return at the end of the method.
View view = inflater.inflate(R.layout.fragment_main, container, true);
[...]
// Here, use findViewById on the View you have just inflated.
ListView mylistview = (ListView) view.findViewById(R.id.listview_forecast); //screws upp in this line
mylistview.setAdapter(mForecastAdapter);
// Return the view you created on the beginning.
return view;
}}
You should start with a proper tutorial not chaotically adding code that you don't understand. Start reading here:
Creating and Using Fragments
After you understood how it goes then you will see that first you need to inflate the layout that contains the stuff you want to find by id and then by using the view(that defines the layout) you can find the desired view(listview, etc).
ListView my listview = (ListView).findViewById(R.id.listview_forcast);
I think this is how it should be, no container in the code

android : Changing font size/style duplicate the linear layout

A fragment is launched which has a list view in it, from an activity.
Now being on this screen switch to setting and change the font size/style the list view duplicates, changing the font size/style again make the list view 3 times and so on..
I am not getting why it is happening??
I checked the onCreateView(....) of fragment if the the view is not null then return the same view.
private view myView;
.
.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(myView!=NULL) return myView;
.
.
myView = inflater.inflate(R.layout.activity_frag, container, false);
.
.
return myView;
}

Android how to recreate fragment view

I have an Android fragment. When I'm creating view I fill fields from layout with values from an model.
and at one point I want to recreate view using the same class model but with other values.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater
.inflate(R.layout.item_view, container, false);
editText = (EditText) v.findViewById(R.id.question_text);
questionText.setText(myModel.getName());
---------------
}
I have a private function which will call at on click on view
private void recreateView(MyModel model){
myModel = model;
//here I want to recall onCreateView or something like that but I don't know if that is ok, or is a more simple way to do that
}

Categories

Resources