spinner won't populate from my array [duplicate] - android

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.

Related

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"/>

Spinner will not populate and won't set arrayadapter

My app redirects from home screen to city screen through the tap of a button. After I go the city screen. I'm trying to populate a spinner(id = countries). But, when I run it eclipse it opens up an empty spinner in my city class.
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;
}

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!!

update spinner from dialogfragment

I have a Spinner in my class TelaEntrada.java loading normally.
public class TelaEntrada extends Fragment {
public Fragment newInstance(Context context) {
TelaEntrada telaEntrada = new TelaEntrada();
return telaEntrada;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.telaentrada, container, false);
DbEntrada banco = new DbEntrada(getActivity().getApplicationContext());
List<Tipo> tipos = banco.todosTipos();
List<String> item = new ArrayList<String>();
for(Tipo tipo : tipos){
item.add(tipo.getDescricao().toString());
}
final Spinner spn_Entrada = (Spinner) root.findViewById(R.id.spnEntradaTipo);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this.getActivity(),
android.R.layout.simple_spinner_dropdown_item, item);
spn_Entrada.setAdapter(adapter2);
I too have one DialogFragment in another class. Code is below.
public class DialogTipo extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View root = (View) inflater.inflate(R.layout.dialog_tipo, container, false);
final EditText txtDialogTipo = (EditText)root.findViewById(R.id.txtDialogTipo);
Button btnDialogTipoAdd = (Button) root.findViewById(R.id.btnDialogTipoAdd);
Button btnDialogTipoCancel = (Button) root.findViewById(R.id.btnDialogTipoCancel);
btnDialogTipoAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DbEntrada banco = new DbEntrada(getActivity().getApplicationContext());
banco.insereTipo(txtDialogTipo.getText().toString());
//I want update my spinner here!
dismiss();
}
});
getDialog().setTitle("Inserir Novo Tipo");
return root;
}
}
I wanted to add a new item, the spinner was directly updated the database to display the new information in the User spinner. Thanks. Sorry for my english!

Setting spinners in fragment

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;
}

Categories

Resources