Populate spinner with years dynamically in Android? - android

I have been racking my brain trying to get this to work. I want to dynamically enter in years from 1900 to the current year into a spinner. I don't think that this is possible to do using an XML defined array but can I do it with an array adapter? Here is what I have so far:
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1900; i <= thisYear; i++)
{
years.add(i);
}
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, years);
//spinner to enter this list to
spinYear = (Spinner)findViewById(R.id.yearspin);
Here's the XML for the spinner:
<Spinner
android:id="#+id/yearspin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Thanks for any help.

You're very close. Try this:
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1900; i <= thisYear; i++) {
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);
Spinner spinYear = (Spinner)findViewById(R.id.yearspin);
spinYear.setAdapter(adapter);
You just forgot to add
spinYear.setAdapter(adapter);

I hope it will help you.
static final String[] Months = new String[] { "January", "February",
"March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
// Set years
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1900; i <= thisYear; i++) {
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);
Spinner spinYear = (Spinner)findViewById(R.id.spinnerYears);
spinYear.setAdapter(adapter);
// Set months
ArrayAdapter<String> adapterMonths = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Months);
adapterMonths.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinMonths = (Spinner)findViewById(R.id.spinnerMonths);
spinMonths.setAdapter(adapterMonths);
// Set days
ArrayList<String> days = new ArrayList<String>();
for (int i = 1; i <= 31; i++) {
days.add(Integer.toString(i));
}
ArrayAdapter<String> adapterDays = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, days);
Spinner spinDays = (Spinner)findViewById(R.id.spinnerDays);
spinDays.setAdapter(adapterDays);

I suggest the easy one:
ArrayList<String> yearsList = new ArrayList<>();
int current_year = Calendar.getInstance().get(Calendar.YEAR);
int count = 60;
for (int i = 0; i < count; i++) {
yearsList.add(Integer.toString(current_year - i));
}
ArrayAdapter<String> yearsAdapter = new ArrayAdapter<>(getContext(), R.layout.dropdown_item, yearsList);
spinYear.setAdapter(yearsAdapter);
Just change the value of int count with how many years you want to print prior to the current year.

Related

Spinner default no selection when data fetched from json

Hi I asked this question before.But i didn't got a proper solution. I have a spinner which will load data from json.After selecting any item from spinner then it will post zeroth postition value again to another API.Everyting works fine.But my problem is I want defaultly no slection for spinner.I added a string named "no selection" to to zeroth position but it is not working.Please help me to implement this.
My spinner
void getList(){
final Common common = new Common();
int a= 100;
String webService = "API/Employee/GetList";
String postData = "";
String[] dataColumns = {"ID",//0
"Code",//1
"Name" ,//2
};
Runnable postThread = new Runnable() {
#Override
public void run() {
//Spinner
int a= 0;
ArrayList<String> Names = new ArrayList<String>();
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Insert.this, R.layout.item_spinner_black, Names);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
Spinner =(Spinner)findViewById(R.id.spinner);
//I added below code but it not working
// dataAdapter.insert(getString(R.string.selectemp), 0);
Spinner.setAdapter(dataAdapter);
}
My Post Data
postData="{\"Title\":\""+title.getText().toString()
+"\",\"Spinnervalue\":\""+List.get(Spinner.getSelectedItemPosition())[0]}
I think its help you.
ArrayList<String> Names = new ArrayList<String>();
Names.add("no selection");
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
The problem is the initialization of your adapter. So when you add later the data, it will not have pre-selected item.
Edit:
I reordered your current code.
void getList(){
final Common common = new Common();
int a= 100;
String webService = "API/Employee/GetList";
String postData = "";
String[] dataColumns = {"ID",//0
"Code",//1
"Name" ,//2
};
Runnable postThread = new Runnable() {
#Override
public void run() {
//Spinner
int a= 0;
ArrayList<String> Names = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Insert.this, R.layout.item_spinner_black, Names);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
Spinner =(Spinner)findViewById(R.id.spinner);
//I added below code but it not working
// dataAdapter.insert(getString(R.string.selectemp), 0);
Spinner.setAdapter(dataAdapter);
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
}
I added a dummy value.And It worked fine.
ArrayList<String> Names = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Insert.this, R.layout.item_spinner_black, Names);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
Spinner =(Spinner)findViewById(R.id.spinner);
String[] data = new String[3];
data[0]="00000000-0000-0000-0000-000000000000";
data[1]="Select";
data[2]="Name";
List.add(data);
for (int i=0;i<common.dataArrayList.size();i++){
String[] data=new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
List.add(data);
}
for(int i=0;i<List.size();i++){
Names.add(List.get(i)[1]+" - "+List.get(i)[2]);
}
Spinner.setAdapter(dataAdapter);

Populate Spinner with based on value returned from REST service

I have a REST method that returns result as "Res:"2". Now i need to populate a spinner with values 1,2. If the REST method returns the result as "Res:"3", the spinner values must be 1,2,3.
This is the code that i have implemented. But the spinner shows only 2 for the
Integer[] items = new Integer[]{Integer.valueOf(user2.getString("Res"))};
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);
Note: user2 is a JSONObject
try this
List<Integer> numbers = new ArrayList<>();
int item = Integer.valueOf(user2.getString("Res"));
for(int i=1; i<item+1; i++){
numbers.add(i)
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this,
android.R.layout.simple_spinner_item, numbers);
cboFloorLevel.setAdapter(adapter);
Try this code
int end_value = Integer.valueOf(user2.getString("Res"));
Integer[] items = new Integer[]{end_value};
for(int i=0;i<end_value;i++){
items[i] = i+1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);
I understand from the question that if the json response is
{"Res:"3"} you need to populate the spinner with 1,2,3.
int item = Integer.valueOf(user2.getString("Res"));
ArrayList <Integer>items = new ArrayList<>();
for(int j=1; j < item+1 ; j++){
items.add(j);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this,
android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);
Found my solution:
You need to import Arrays from java8:
import java.util.Arrays;
and then you can convert string to int, create array and fill that array with lambda passed to Arrays.setAll():
int maxValue = Integer.parseInt(user2.getString("Res"));
int[] items = new int[maxValue];
Arrays.setAll(array, i -> i + 1);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, items);
cboFloorLevel.setAdapter(adapter);

Extract substring from a string for spinner entry

I need to extract part of the string and display it in the spinner
I need that when the spinner display data example
For array entries like the following
"Equipo-001"
"Equipo-002"
Should show only:
"001"
"002"
Here's my code
private void rellenarSpinnerConFoliosDeMaquinasDelPunto(List<String> folios) {
maquinas = dbOn.getMaquinasDePunto(idPunto);
for (int i = 0; i < maquinas.size(); i++) {
foliosDeMaquinas.add(maquinas.get(i).getcFolioMaquina());
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, folios);
adaptadorFoliosMaquina.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_folioMaquina.setAdapter(adaptadorFoliosMaquina);
spn_folioMaquina.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
ArrayList<String> numberFolios = new ArrayList<>();
for(int j =0; j < folios.size(); j++){
numberFolios.add(folios.get(j).substring(8, 10));
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, numberFolios);
Check link for better reference on how to use substring.
You can use SPLIT function
ArrayList<String> data = new ArrayList();
foreach(String get:folios){
data.add(folios.split("-")[1]);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
If your result will be dynamic(The string length would vary in future) the below solution may work,
private void rellenarSpinnerConFoliosDeMaquinasDelPunto(List<String> folios) {
try{
maquinas = dbOn.getMaquinasDePunto(idPunto);
for (int i = 0; i < maquinas.size(); i++) {
foliosDeMaquinas.add(maquinas.get(i).getcFolioMaquina().toString().split("-")[1]);
}
} catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
adaptadorFoliosMaquina = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, folios);
adaptadorFoliosMaquina.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_folioMaquina.setAdapter(adaptadorFoliosMaquina);
spn_folioMaquina.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

Android Age Spinner Item

Is it possible to build a for loop that goes to 100 to populate an Android spinner? As opposed to manually adding to the ArrayList.
Something like the below:
List<Integer> age = new ArrayList<>();
for (int i = 1; i < 101; i++) {
age.add(i);
}
It's perfectly fine to programmatically populate your ArrayList; you just need to use an ArrayAdapter like this:
List age = new ArrayList<Integer>();
for (int i = 1; i <= 100; i++) {
age.add(Integer.toString(i));
}
ArrayAdapter<Integer> spinnerArrayAdapter = new ArrayAdapter<Integer>(
this, android.R.layout.simple_spinner_item, age);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter(spinnerArrayAdapter);
Hope this helps!
yes of coures you can use...
Like this --
List age = new ArrayList<>();
for (int i = 1; i < 101; i++) {
age.add(i);
}]
now you have to use Array\adapter-
ArrayAdapter<Integer> sa= new ArrayAdapter<Integer>(
this, android.R.layout.simple_spinner_item, age);
sa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item );
Spinner s= (Spinner)findViewById(R.id.spinner);
s.setAdapter(sa);

Dynamically view and hide Spinners in an Activity

I want to generate Spinners dynamically. Maximum numbers of Spinners I want to generate is 7. But I want to generate Spinners according to the data sets I'm getting from json response.
For the moment I have below shown code, which is used to identify how many spinners to generate. According to the below code MealDealCatCode contains L1, L2. So I want to get the value as 2 and display only 2 Spinners out of 7 (Hide remaining 5 Spinners).
How is possible to achieve my task. Any help will be appreciated.
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> MainMenuDescription1 = new ArrayList<String>();
List<String> MainMenuDescription2 = new ArrayList<String>();
List<String> MainMenuDescription3 = new ArrayList<String>();
List<String> MainMenuDescription4 = new ArrayList<String>();
List<String> MainMenuDescription5 = new ArrayList<String>();
List<String> MainMenuDescription6 = new ArrayList<String>();
List<String> MainMenuDescription7 = new ArrayList<String>();
List<String> MealDealCatCode = new ArrayList<String>();
int uniqueItemIdCount = 0;
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
JSONArray subMenuArray = object
.getJSONArray("MealDealItemEntity");
for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray.getJSONObject(j);
Log.i("MealDealCatCode ",
subMenuObject.getString("MealDealCatCode"));
MealDealCatCode.add(subMenuObject
.getString("MealDealCatCode"));
MealDealCatCode = Utils
.removeDuplicatesFromList(MealDealCatCode);
uniqueItemIdCount = MealDealCatCode.size();
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L1")) {
Log.i("MainMenuDescription",
subMenuObject.getString("MainMenuDescription"));
MainMenuDescription1.add(subMenuObject
.getString("MainMenuDescription"));
}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L2")) {
MainMenuDescription2.add(subMenuObject
.getString("MainMenuDescription"));
}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L3")) {
MainMenuDescription3.add(subMenuObject
.getString("MainMenuDescription"));
}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L4")) {
MainMenuDescription4.add(subMenuObject
.getString("MainMenuDescription"));
}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L5")) {
MainMenuDescription5.add(subMenuObject
.getString("MainMenuDescription"));
}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L6")) {
MainMenuDescription6.add(subMenuObject
.getString("MainMenuDescription"));
}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L7")) {
MainMenuDescription7.add(subMenuObject
.getString("MainMenuDescription"));
}
}
item1SP = (Spinner) findViewById(R.id.sp_item1);
item2SP = (Spinner) findViewById(R.id.sp_item2);
item3SP = (Spinner) findViewById(R.id.sp_item3);
item4SP = (Spinner) findViewById(R.id.sp_item4);
item5SP = (Spinner) findViewById(R.id.sp_item5);
item6SP = (Spinner) findViewById(R.id.sp_item6);
item7SP = (Spinner) findViewById(R.id.sp_item7);
item1SP.setVisibility(View.GONE);
item2SP.setVisibility(View.GONE);
item3SP.setVisibility(View.GONE);
item4SP.setVisibility(View.GONE);
item5SP.setVisibility(View.GONE);
item6SP.setVisibility(View.GONE);
item7SP.setVisibility(View.GONE);
item1SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription1 = Utils
.removeDuplicatesFromList(MainMenuDescription1);
ArrayAdapter<String> dataAdapterOne = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription1);
dataAdapterOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item1SP.setAdapter(dataAdapterOne);
item1SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterOne,
R.layout.contact_spinner_row_nothing_selected, this));
item2SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription2 = Utils
.removeDuplicatesFromList(MainMenuDescription2);
ArrayAdapter<String> dataAdapterTwo = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription2);
dataAdapterTwo
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item2SP.setAdapter(dataAdapterTwo);
item2SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterTwo,
R.layout.contact_spinner_row_nothing_selected, this));
item3SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription3 = Utils
.removeDuplicatesFromList(MainMenuDescription3);
ArrayAdapter<String> dataAdapterThree = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription3);
dataAdapterThree
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item3SP.setAdapter(dataAdapterThree);
item3SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterThree,
R.layout.contact_spinner_row_nothing_selected, this));
item4SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription4 = Utils
.removeDuplicatesFromList(MainMenuDescription4);
ArrayAdapter<String> dataAdapterFour = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription4);
dataAdapterFour
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item4SP.setAdapter(dataAdapterFour);
item4SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterFour,
R.layout.contact_spinner_row_nothing_selected, this));
item5SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription5 = Utils
.removeDuplicatesFromList(MainMenuDescription5);
ArrayAdapter<String> dataAdapterFive = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription5);
dataAdapterFive
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item5SP.setAdapter(dataAdapterFive);
item5SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterFive,
R.layout.contact_spinner_row_nothing_selected, this));
item6SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription6 = Utils
.removeDuplicatesFromList(MainMenuDescription6);
ArrayAdapter<String> dataAdapterSix = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription6);
dataAdapterSix
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item6SP.setAdapter(dataAdapterSix);
item6SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterSix,
R.layout.contact_spinner_row_nothing_selected, this));
item7SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription7 = Utils
.removeDuplicatesFromList(MainMenuDescription7);
ArrayAdapter<String> dataAdapterSeven = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription7);
dataAdapterSeven
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item7SP.setAdapter(dataAdapterSeven);
item7SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterSeven,
R.layout.contact_spinner_row_nothing_selected, this));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I was able to achieve what I want by following answer,
for (int a = 0; a < uniqueItemIdCount; a++) {
if (uniqueItemIdCount == 1) {
item1SP.setVisibility(View.VISIBLE);
}
if (uniqueItemIdCount == 2) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
}
if (uniqueItemIdCount == 3) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
}
if (uniqueItemIdCount == 4) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
}
if (uniqueItemIdCount == 5) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
item5SP.setVisibility(View.VISIBLE);
}
if (uniqueItemIdCount == 6) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
item5SP.setVisibility(View.VISIBLE);
item6SP.setVisibility(View.VISIBLE);
}
if (uniqueItemIdCount == 7) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
item5SP.setVisibility(View.VISIBLE);
item6SP.setVisibility(View.VISIBLE);
item7SP.setVisibility(View.VISIBLE);
}
}

Categories

Resources