I am trying to show values in spinner from Arrayadapter in one of my fragments in the onCreateView in my public final class Manual extends Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vista =inflater.inflate(R.layout.fragment_pag1,container,false);
fragment pag1
calcular= (Button)vista.findViewById(R.id.button);
etd=(EditText)vista.findViewById(R.id.editText);
resultadocp=(TextView)vista.findViewById(R.id.textView3);
lista = (Spinner)vista.findViewById(R.id.spinner);
String []opciones={"one","two","three","four","five"};
ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, opciones);
lista.setAdapter(adapter); return vista; }
i don´t understand why when runs aplication, this spinner shows empty
Declare The Array Like this:
private static final String[] opciones={"one","two","three","four","five"};
The Spinner Code Here Should be like:-
Spinner spinner=(Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
opciones);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Related
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.
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 tries to set up a listView in a fragment but my app crashes,tried to search on the internet but couldn't find an answer.
Java Source code:
public static class FragmentFind extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String [] cinemas={
"Cinema 1","Cinema 2","Cinema 3"
};
ArrayAdapter<String> adapter=new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,cinemas);
ListView list=(ListView) getActivity().findViewById(R.id.cinema_list);
list.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_find,container,false);
}
}
Android XML layout for the fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cinema_list"
android:layout_alignParentTop="true"/>
</RelativeLayout>
Thanks.
Because your ListView is Null. As your Listview is part of Fragment layout you have to call findViewById() on view inflated by that fragment layout file.
So try like,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find,container,false);
String [] cinemas={"Cinema 1","Cinema 2","Cinema 3"};
ArrayAdapter<String> adapter=new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,cinemas);
ListView list=(ListView) view.findViewById(R.id.cinema_list);
list.setAdapter(adapter);
return view;
}
I believe the error is probably a null exception. The main differences that I saw from my fragment from yours.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.whatever, container, false);
View testview= (View) v.findViewById(R.id.id);
return v;
}
I suggest you to look at the Difference between onCreateView() and onViewCreated() in Fragment.Even tough you can do do the same thing in onCreateView() method, it is best to do the setting of the Adapter in the onViewCreated() method
This is where the issue is,
ListView list=(ListView) getActivity().findViewById(R.id.cinema_list);
Here is how I would do it.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_find,container,false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
String [] cinemas={
"Cinema 1","Cinema 2","Cinema 3"
};
ArrayAdapter<String> adapter=new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,cinemas);
ListView list=(ListView) view.findViewById(R.id.cinema_list);
list.setAdapter(adapter);
}
Your activity does not have the ListView R.id.cinema_list when the app tries to search for it by: getActivity().findViewById(R.id.cinema_list);
So you should better search the ListView within inflated view by:
View view = inflater.inflate(R.layout.fragment_find,container,false);
ListView myList = (ListView)view.findViewById(R.id.cinema_list);
And then set the adapter and return the view .
You are lucky that your app crashed. It is also possible that getActivity().findViewById(R.id.cinema_list) could find a ListView if your activity layout has a ListView with R.id.cinema_list. I am telling this because it is obvious that you will have only one ListView with such specific id, but sometimes people use common ids such as listView1. So you better be careful.
Replace this:
ListView list=(ListView) getActivity().findViewById(R.id.cinema_list);
with this:
ListView list = (ListView) view.findViewById(R.id.cinema_list);
I want to make spinner inside my Google Map class so i can change the view of googlemaps like HYBRID, SATELLITE so on.. I make a this spinner on fragment inside of fragment.
when i declare arrayaddapter it keep show "Unfortunately, --- has stopped"
public class GMaps extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.popup_map, null, false);
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sview.setAdapter(dataAdapter);
// ..
I think it is because the parameter context = this.getActivity()... I have try this.getView(), getActivity(), v.getContext, nothing works.
First of all you should change this
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
to
sview = (Spinner)v.findViewById(R.id.spinner1);
it's becoz your Spinner view belong to you inflated layout view.
then try to set adapter like:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sview.setAdapter(dataAdapter);
//your spinner is in popup_map.xml so you need to do
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
instead of getActivity() you need to use View v
sview = (Spinner)v.findViewById(R.id.spinner1);
It's my understanding that a ListFragment is just a Fragment that automatically has a ListView in it's layout. I have an array of Strings in my XML string resource file, and I've got a class that extends ListFragment called UrlListFragment.
Here's the array:
<string-array name="list_array">
<item>http://www.microsoft.com</item>
<item>http://www.xbox.com</item>
<item>http://www.windowsphone.com</item>
</string-array>
And here's my extened ListFragment:
public class UrlListFragment extends ListFragment {
private static String[] urlList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
Resources res = getActivity().getResources();
urlList = res.getStringArray(R.array.list_array);
view = super.onCreateView(inflater, container, savedInstanceState);
return view;
}
}
How can I add the array of strings to the ListFragment so that every entry in the array is an entry in the list?
A list fragment has a setAdapter() method.
You can create a simple adapter with:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, urlList);
And then, you just have to do:
setListAdapter(adapter);