Spinner will not populate and won't set arrayadapter - android

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

Related

How to scroll 3 listviews in different fragment at the same time?

I have 3 ListViews in one layout but in 3 different fragments. I want, when i scroll down, to first load the first listview and the other two listviews to scroll background at same the time when 1 change fragment same position
display at 2nd and 3rd fragment i loaded listview from database.
Pic of 1st fragment
//first fragment code
public class Quran extends Fragment {
ListView quranlst;
Parcelable state;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_quran, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
quranlst = (ListView) getActivity().findViewById(R.id.quranlist);
DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());
Intent it = getActivity().getIntent();
int p = it.getIntExtra("paya", 0);
int i = it.getIntExtra("saya", 0);
if (i != 0) {
List<String> lstsa;
db.open();
lstsa = db.getsaya(i);
db.close();
MyCustomAdapter adapter = new MyCustomAdapter(lstsa, Quran.this.getActivity());
quranlst.setAdapter(adapter);
} else if (p != 0) {
List<String> lstpa;
db.open();
lstpa = db.getpaya(p);
db.close();
MyCustomAdapter adapter = new MyCustomAdapter(lstpa, Quran.this.getActivity());
//handle listview and assign adapter
quranlst.setAdapter(adapter);
}
}
}
pic of Second fragment
that is my 2nd fragment code
public class englisht extends Fragment {
ListView el;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_englisht, container, false);}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
el = (ListView) view.findViewById(R.id.englishlst);
DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());
Intent it = getActivity().getIntent();
int i = it.getIntExtra("saya", 0);
int p = it.getIntExtra("paya", 0);
if (i != 0) {
List<String> lstenglishsurah;
db.open();
lstenglishsurah = db.geteaya(i);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lstenglishsurah);
el.setAdapter(adapter);
} else if (p != 0) {
List<String> lstenglishparah;
db.open();
lstenglishparah = db.paraheng(p);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lstenglishparah);
el.setAdapter(adapter);
}
}
}
**that is my third fragment code**
public class urdut extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
return inflater.inflate(R.layout.activity_urdut, container, false);}
ListView urdulst;
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
urdulst = (ListView) view.findViewById(R.id.urdulst);
DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());
Intent it = getActivity().getIntent();
//SurahAyat
int i = it.getIntExtra("saya", 0);
int p = it.getIntExtra("paya", 0);
if (i!=0) {
List<String> lsturdusurah;
db.open();
lsturdusurah = db.getuaya(i);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lsturdusurah);
urdulst.setAdapter(adapter);
}
else if (p!=0) {
List<String> lsturduparah;
db.open();
lsturduparah = db.parahurdu(p);
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lsturduparah);
urdulst.setAdapter(adapter);
}
}
}
First get scroll position of the list view using this code
View c = listview.getChildAt(0);
int no= -c.getTop() + listview.getFirstVisiblePosition() * c.getHeight();
and then set listview scroll position to other fragment
getListView().setSelection(no);
no is : like 1,2,3....
You can use the methods smoothScrollToPosition and smoothScrollToPositionFromTop to scroll list(other lists) with respect to first list to which you are scrolling

Adding Items to ListView from Edittext

I have an Actiity where I have a simple Edittext and a button as shwown:
No I have a MainActivity which contains many fragments as shown:
This is my Code For passing edittext text to the main activity:
addlist = (ImageButton) findViewById(R.id.btnaddlist);
findViewById(R.id.btnaddlist).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText edit = (EditText) findViewById(R.id.txtlist);
Intent i = new Intent(CreateList.this,
MainActivity.class);
Bundle bundle = new Bundle();
String ItemName = edit.getText().toString();
// Add your data from getFactualResults method to bundle
bundle.putString("ListItemName", ItemName);
// Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
}
});
}
This is my Home fragment:
ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lv = (ListView) getView().findViewById(R.id.itemslistView);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Bundle itemintent = getActivity().getIntent().getExtras();
String ItemName = itemintent.getString("ListItemName");
if (itemintent.getString("ListItemName").equals("ItemName")) {
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
list.add(ItemName);
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> Listadapter;
Listadapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, list);
Listadapter.notifyDataSetChanged();
}
return rootView;
}
I'm not getting any error in logcat..But its not able to add any text to listView on MainActivity.
You made the adapter but didn't call ListView#setAdapter()
Change your Home fragment like that:
ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lv = (ListView) getView().findViewById(R.id.itemslistView);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Bundle itemintent = getActivity().getIntent().getExtras();
String ItemName = itemintent.getString("ListItemName");
if (ItemName.equals("ItemName")) {
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
list.add(ItemName);
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> Listadapter;
Listadapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, list);
lv.setAdapter(Listadapter);
Listadapter.notifyDataSetChanged();// i don't think this line is necessary
}
return rootView;
}
The better way to do this is as below:
Start main activity
give a button to add item
on click of this button, call the activity(use startActivityForResult ) having edittext and a button
and on this button click, setResult with intent extras and finish this activity.
it will to MainActivity. Here write your onActivityResult method and handle it based on the result code. get your data from intent here.
Hope this answer helps you.

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.

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