Setting spinners in fragment - android

I am trying to set the values for my spinner from a string Array in one of my fragments in the onCreateView in my public final class Manual extends Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.manual, container, false);
String [] values =
{"Time at Residence","Under 6 months","6-12 months","1-2 years","2-4 years","4-8 years","8-15 years","Over 15 years",};
Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(LTRadapter);
return inflater.inflate(R.layout.manual, container, false);
}
I get no errors with my code, however it doesn't set the spinner. The spinner remains blank with no values. Any ideas on why my code isn't setting the spinner?

The problem was I was returning a new view, not the one I set. I had to return v; and it worked fine.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.manual, container, false);
String [] values =
{"Time at Residence","Under 6 months","6-12 months","1-2 years","2-4 years","4-8 years","8-15 years","Over 15 years",};
Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
return v;
}

Related

Spinner not filled with ArrayAdapter

So my spinner isn't being filled programatically:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first_boot__language_selection, container, false);
setupSpinner(view, R.id.firstLanguageSpinner, R.array.languagesSpinner);
setupSpinner(view, R.id.secondLanguageSpinner, R.array.languagesSpinner);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_boot__language_selection, container, false);
}
private void setupSpinner(View view, int spinnerResourceId, int arrayResourceId) {
Spinner spinner = view.findViewById(spinnerResourceId);
spinner.setSelection(1);
//Adapts strings into CharSequence.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this.getContext(), arrayResourceId, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
However I cannot for the life of me figure out why not? I would fill it via XML, however this code doesn't include other functions that I had added. For instance, in setupSpinner, I had added the onClickListener event, which wasn't working.
In onCreateView you should be using return view;, not return inflater.inflate... What you are doing is inflating a second copy of the fragment, which is not initialized with spinner data.

Autocomplete Text is not working in android fragment

I want to set autocomplete text in my application. The suggestion values will fetch from the database. But for a trial I made a string array for the suggestion values. It works fine when I implement it on an activity but not working in fragment.
In place of getActivity function I tried:
1) getContext()
2) this.getActivity()
3) (Search_Bus)getActivity()
but none work...
it's giving me error:
Attempt to invoke virtual method on a null object reference
Here's my code...
public class Bus_Schedule_tab3 extends Fragment {
AutoCompleteTextView autoCompleteTextView;
ArrayList<String> array;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bus_schedule_tab3, container, false);
stop_names = new String[] {"usa","china","russia","bangladesh"};
autoCompleteTextView = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,stop_names);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Its working for me...You missed to set threshold
AutoCompleteTextView autoCompleteTextView;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);
stop_names = new String[]{"usa", "china", "russia", "bangladesh"};
autoCompleteTextView = rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<>(getActivity(), R.layout.support_simple_spinner_dropdown_item, stop_names);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Set in your xml file android:completionThreshold.
<AutoCompleteTextView
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_weight="1"/>

Updating fragment view after setting listAdapter

I am converting an existing activity (SherlockActivity) into fragment (SherlockListFragment) in order to use tabs.
The fragment returns a onCreateView when created and is using the following code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
That works fine but as I was doing in my activity I want to update the list view with my own content after I finish fetching it from the server.
ListView listContent = (ListView) getView().findViewById(R.id.contentlist);
listContent.setAdapter(myAdapter);
Whatever I have researched so far, I am fetching the listview through activity and setting my adapter to it but that is having no effect on fragment view and it remains the same.
Question is: How can I update listView from outside onCreateView?
First, make your listview a global var. Then set its value on the onCreateView function:
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_placelist, container, false);
list = rootView.findViewById(/*your id*/)
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
//change your listview content here when refresh or something else
public void updateListView(){
List l = /*fetch new data from server*/
listAdapter.addAll(l);
listAdapter.notifyDataSetChanged();
}
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_placelist, container, false);
list = rootView.findViewById(/*your id*/)
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return rootView;
}
from here android versions must be a arraylist or string array or hashmap

spinner won't populate from my array [duplicate]

This question already exists:
Spinner will not populate and won't set arrayadapter
Closed 8 years ago.
My app redirects me to a different screen from my home with the tap of a button. Once, i reach my city screen my spinner is visible. But it doesn't contain any elements from my array. Nothing force closes or anything, but it doesn't work.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_city, container, false);
String[] test = {"test1", "test2"};
Spinner spin = (Spinner) v.findViewById(R.id.countries);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, test);
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapt);
return v;
}
You need to have the below.
public static PlaceHolderFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_city, container, false);
String[] test = {"test1", "test2"};
Spinner spin = (Spinner) v.findViewById(R.id.countries);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, test);
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapt);
return v;
}
}
Also use getActivity() to get the context of hosting activity.

Adapter in Spinner, Android

How do i set the adapter in Spinner for the following bunch of code? I should set the adapter under
//set adapter for spinner here
public class ListViewFragment extends Fragment implements OnItemSelectedListener{
public ListViewFragment(){}
Spinner mspinner; //declare spinner globally
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
mspinner = (Spinner) view.findViewById(R.id.spinner1);
//set adapter for spinner here
return rootView;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//do something when particular item is selected from spinner
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Try this , Hope it works
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
mspinner = (Spinner) view.findViewById(R.id.spinner1);
//set adapter for spinner here
// you need to create a array which one is added to spinner
String [] name = {"A","B","C"};
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
// you can use getActivity() or this
mspinner.setAdapter(adapter);
return rootView;
}
Thank you
//here are the contents its the same as you do in a listview it can be a ArrayList or a array
String[] content={"A","B","C"};
adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,content);
mspinner.setAdapter(adapter);
try this hope this will work for you!!

Categories

Resources