I'm using two Spinners to show the items I'm getting from the json response. I have 2 problems right now. When u check my logcat u can see there are items repeating (Right side list, u can see so many pan). I want to have 1 item only once in my Spinner. I want to use something similar to distinct we use in sql databases.
My second problem is,
Select pan in the 1 spinner then 2nd spinner should contain items related to pan. (select pan in 1st spinner and 2nd should display only Pan large, pan medium and personal pan)
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
List<String> description = new ArrayList<String>();
List<String> extraDescription = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")
&& (object.getString("SubCategoryID")).equals("1")) {
JSONArray subMenuArray = object
.getJSONArray("SubMenuEntity");
for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray
.getJSONObject(j);
Log.i("Crust", subMenuObject.getString("Crust"));
crust.add(subMenuObject.getString("Crust"));
Log.i("Description",
subMenuObject.getString("Description"));
description.add(subMenuObject.getString("Description"));
JSONArray extraItemEntityArray = subMenuObject
.getJSONArray("ExtraItemEntity");
}
}
crustSP = (Spinner) findViewById(R.id.sp_crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, description);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Output of this
Call this method to get distinct descriptions and then set the adapter using the return value of this function...
public static ArrayList<String> removeDuplicatesFromList(ArrayList<String> descriptions)
{
ArrayList<String> tempList = new ArrayList<String>();
for(String desc : descriptions)
{
if(!tempList.contains(desc))
{
tempList.add(desc);
}
}
descriptions = tempList;
tempList = null;
return descriptions;
}
For instance
description = Utils.removeDuplicatesFromList(description);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, description);
NOTE:
I would suggest you make a new class call it Utils.java and place the above method inside it and then call it i have mentioned above.
Like this...
import java.util.ArrayList;
public class Utils
{
private Utils()
{
//Its constructor should not exist.Hence this.
}
public static ArrayList<String> removeDuplicatesFromList(ArrayList<String> descriptions)
{
ArrayList<String> tempList = new ArrayList<String>();
for(String desc : descriptions)
{
if(!tempList.contains(desc))
{
tempList.add(desc);
}
}
descriptions = tempList;
tempList = null;
return descriptions;
}
}
I hope it helps.
Related
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);
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);
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() {
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);
In Android I am fetching json data from web.I the list is like {Name,dial,code}
I have this
countryinfo = new ArrayList<CountryInfo>();
Countrylist = new ArrayList<String>();
try {
for (String line : result) {
jsonarray= new JSONArray(line);
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
CountryInfo conpop = new CountryInfo();
conpop.setName(jsonobject.optString("Name"));
conpop.setIso(jsonobject.optString("dial"));
conpop.setItu(jsonobject.optString("code"));
countryinfo.add(conpop);
Countrylist.add(jsonobject.optString("Name"));
}
}
} catch (Exception e) {
//Log.e("Error", e.getMessage());
e.printStackTrace();
}
I use
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MobnoAct.this, android.R.layout.simple_spinner_item, Countrylist);
mySpinner.setAdapter(adapter);
mySpinner.setSelection(0);
But in the spinner Its showing a default country name..But I want The country name will be as per locale..
like :--
Locale defaultLocale = getResources().getConfiguration().locale;
String si=defaultLocale.getCountry();
How I can do that???
Try this after you set the adapter and retrieve the default local:
for(String countryName : countryList)
for(CountryInfo country : countryinfo)
if(country.getName().equals(countryName) && country.getCode().toLowerCase().equals(si.toLowerCase()))
mySpinner.setSelection(adapter.getPosition(countryName));
Hope this helps