I need to constantly update my Spinner after adding data which it shows.
Part of my code:
public class MainActivity extends AppCompatActivity {
ArrayAdapter<String> adapter;
Spinner spinner;
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> categories = databaseHelper.getAllCategories();
spinner = (Spinner) findViewById(R.id.spinner);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
In DatabaseHelper class, I make simple query which selects one column.
public class MainActivity extends AppCompatActivity {
ArrayAdapter<String> adapter;
Spinner spinner;
ArrayList<String> categories;
protected void onCreate(Bundle savedInstanceState) {
categories = databaseHelper.getAllCategories();
spinner = (Spinner) findViewById(R.id.spinner);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
private function updateSpinner(){
categories.clear();
categories.addAll(databaseHelper.getAllCategories());
adapter.notifyDataSetChanged();
}
Related
I am trying to insert an item into an Adapter that multiple Spinners are using. However when I insert into the adapter, I would like the Spinners to retain their original selection based off the object and not the position.
In the case below, the Spinner is originally selecting "four", but when I click the button to insert "three", the spinner is now set to "three" instead of updating to the new position of "four". How can I achieve this?
public class MyActivity extends Activity {
List list;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("four");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, list);
spinner.setAdapter(adapter);
// set selection to "four"
spinner.setSelection(2);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//list.add(2, "three") causes the same problem
adapter.insert("three", 2);
adapter.notifyDataSetChanged();
}
});
}
Should you call spinner.setAdapter(adapter); or a similar method after inserting a new value into the adaptor?
spinner.setSelection(list.indexOf("four"));
This will set the selection to "four" no matter on what position it is.
The app crash because of the following code lines.
I have two string-array in a XML file named as Telephones in a folder named values and two TextView in text.xml.
I can not figure out what's going wrong here.
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1= new ArrayAdapter<String>(this, R.layout.text, number);
listView.setAdapter(arrayAdapter1);
}
}
Your help would be appreciated.
Thank You!
I forgot to add TextView id in MainActivity java but after adding the names_txt won't show up the only one show up is numbers_txt, how fix it please?
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, R.id.names_txt, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, R.layout.text, R.id.numbers_txt, number);
listView.setAdapter(arrayAdapter1);
}
}
I am trying to set and get value to a spinner for item dynamically ?
any ideas ?
I just need help with the Spinner behavior right now, the rest should be quite easy.
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add("value");
spinnerAdapter.notifyDataSetChanged();
spinner.setSelection(0);
String text = spinner.getSelectedItem().toString();
1
2
3
XML file:
<Spinner android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Java file:
public class SpinnerExample extends Activity {
private String[] arraySpinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.arraySpinner = new String[] {
"1", "2", "3", "4", "5"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);
}
// To get value from spenner
spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView parent) {
}
});
}
Try this this will help you.
Spinner mySpinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(myAdapter);
If you want to add the elements dynamically, you can by doing this:
myAdapter.add("newelement");
myAdapter.notifyDataSetChanged();
Spinner spinn = findViewById(R.id.socialmedia_spinner_adsocmeda);
ArrayList<String> fam = new ArrayList<>();
fam.add("INSTAGRAM");
fam.add("FACEBOOK");
fam.add("GOODWALL");
fam.add("TWITTER");
fam.add("YOUTUBE");
fam.add("LINKEDIN");
fam.add("SNAPCHAT");
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (ProfileActivity.this,
android.R.layout.simple_list_item_1, fam.toArray(new String[0]));
spinn.setAdapter(myAdapter);
That's a way we can add data to spinner.
I have code to load spinner with class extends Activity. It is work successfully. This is the code ...
package nielpoenya.blogspot.com;
import....
public class MySQLite extends Activity implements OnItemSelectedListener {
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
loadSpinnerData();
}
/**
* Function to load the spinner data from SQLite database
* */
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected.....
Now i want to try in class with extends Fragment, but it make it stop working..
package com.tugasbesar.medantrain;
import....
public class InfoJadwal extends Fragment implements OnItemSelectedListener {
Spinner spinner;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.info_jadwal, container, false);
spinner = (Spinner) getActivity().findViewById(R.id.spin_tujuan);
spinner.setOnItemSelectedListener(this);
loadSpinnerData();
return rootView;
}
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(this.getActivity());
// Spinner Drop down elements
List<String> lables = db.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
this.getActivity(), android.R.layout.simple_spinner_item,
lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected....
What was lacking with my fragment class ? Thanks
The error is in your onCreateView, you need to replace :
spinner = (Spinner) getActivity().findViewById(R.id.spin_tujuan);
by :
spinner = (Spinner) rootView.findViewById(R.id.spin_tujuan);
because with getActivity().findViewById() it will try to search a view in the contentview of your Activity, not in your Fragment.
Android 2.3.3
I am having a Spinner in my xml file and I wish to set an ArrayList as a source. I am trying to instantiate the ArrayAdapter with the ArrayList, but i don't get the R.simple_spinner_adapter to select.
Here is the code :::
public class UnitConverter extends Activity{
Spinner spnUnit;
ArrayAdapter<String> ad;
ArrayList<String> alAngle = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.unitconverter);
spnUnit = (Spinner) findViewById(R.id.spinnerUnit);
ad = new ArrayAdapter<String>(this, android.R.simple_spinner_adapter, alAngle);
// Error at above line near android.R.simple_spinner_adapter
setDataToArrayLists();
}
The ans to ur question is u are not using adapter properly:-
android.R.simple_spinner_adapter should be android.R.layout.simple_spinner_item
Sample to this can be:-
Spinner spinner = (Spinner) findViewById(R.id.font_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.font_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
check using as above this is sample used in my code...!!!!