Dynamically view and hide Spinners in an Activity - android

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

Related

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() {

Issue with android spinners - Loading distinct values in spinners

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.

Multiple Spinners making various JSON calls

The aim of the application is to have spinners left (home team) and on the right (Away team). I can successfully populate the home team spinners but when i try to populate the away team spinners i get the error:
06-25 18:02:36.052: W/System.err(17916): org.json.JSONException: Value at 0 is null.
06-25 18:02:36.057: W/System.err(17916): at org.json.JSONArray.get(JSONArray.java:259)
06-25 18:02:36.057: W/System.err(17916): at org.json.JSONArray.getJSONObject(JSONArray.java:480)
06-25 18:02:36.057: W/System.err(17916): at app.league.npd.CaptainsInfo.useaJson(CaptainsInfo.java:309)
I can see that the error is coming from the "ateam" array, but i dont know what the problem is. I am new to android programming so any help would be appreciated.
Here is the code:
public class CaptainsInfo extends Activity {
JSONArray jsonArray = null;
JSONArray str_login = null;
public String items[];
public String aitems[];
private Spinner
spinner1, spinner2, spinner3, spinner4,
spinner5, spinner6, spinner7, spinner8,
spinner9, spinner10, spinner11, spinner12,
spinner13, spinner14;
private Button btnSubmit;
public String kode;
public String Home_team;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
addListenerOnSpinner2ItemSelection();
}
public void addItemsOnSpinner(String items[]) {
spinner3 = (Spinner) findViewById(R.id.player11);
spinner5 = (Spinner) findViewById(R.id.player12);
spinner7 = (Spinner) findViewById(R.id.player21);
spinner9 = (Spinner) findViewById(R.id.player22);
spinner11 = (Spinner) findViewById(R.id.player31);
spinner13 = (Spinner) findViewById(R.id.player32);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner5.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner7.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner9.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner11.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner13.setAdapter(adapter);
}
public void addItemsOnSpinner2(String aitems[]) {
spinner4 = (Spinner) findViewById(R.id.aplayer11);
spinner6 = (Spinner) findViewById(R.id.aplayer12);
spinner8 = (Spinner) findViewById(R.id.aplayer21);
spinner10 = (Spinner) findViewById(R.id.aplayer22);
spinner12 = (Spinner) findViewById(R.id.aplayer31);
spinner14 = (Spinner) findViewById(R.id.aplayer32);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner6.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner8.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner10.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner12.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner14.setAdapter(adapter);
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.h_type);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//String test1 = parent.getItemAtPosition(position).toString();
String test = (String) spinner1.getSelectedItem();
//String test2 = (String) spinner2.getSelectedItem();
items=useJson(test);
//aitems=useaJson(test2);
//addItemsOnSpinner2(aitems);
addItemsOnSpinner(items);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
} });}
public void addListenerOnSpinner2ItemSelection(){
spinner2 = (Spinner) findViewById(R.id.a_type);
spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//String test2 = parent.getItemAtPosition(position).toString();
//String test = (String) spinner1.getSelectedItem();
String test2 = (String) spinner2.getSelectedItem();
// items=useJson(test);
aitems=useaJson(test2);
addItemsOnSpinner2(aitems);
//addItemsOnSpinner(items);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
} });
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.h_type);
spinner2 = (Spinner) findViewById(R.id.a_type);
spinner3 = (Spinner) findViewById(R.id.player11);
spinner4 = (Spinner) findViewById(R.id.aplayer11);
spinner5 = (Spinner) findViewById(R.id.player12);
spinner6 = (Spinner) findViewById(R.id.aplayer12);
spinner7 = (Spinner) findViewById(R.id.player21);
spinner8 = (Spinner) findViewById(R.id.aplayer21);
spinner9 = (Spinner) findViewById(R.id.player22);
spinner10 = (Spinner) findViewById(R.id.aplayer22);
spinner11 = (Spinner) findViewById(R.id.player31);
spinner12 = (Spinner) findViewById(R.id.aplayer31);
spinner13 = (Spinner) findViewById(R.id.player32);
spinner14 = (Spinner) findViewById(R.id.aplayer32);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(CaptainsInfo.this,
"OnClickListener : " +
"\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
String links_url = "http://192.168.9.59/NPD/detail-info.php?Match_id="+kode;
// Call the FunctionParser to parse the information being returned
// From the FHL databaseList<NameValuePair> params = new ArrayList<NameValuePair>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.FunctionParser(links_url);
// The array is called and the information is returned from
// the database corresponding to each string below
try {
str_login = json.getJSONArray("temp");
String jdwl = "";
TextView isi = (TextView) findViewById(R.id.fixdet);
for(int i = 0; i < str_login.length(); i++){
JSONObject ar = str_login.getJSONObject(i);
Home_team = ar.getString("Home_team");
jdwl += "" + ar.getString("Home_team")+"\n vs \n"+
"" + ar.getString("Away_team")+"\n"+
"" + ar.getString("Fixture_date")+"\n";
}
isi.setText(jdwl);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String[] useJson(String test) {
JSONParser jParser = new JSONParser();
String link_url = "http://10.2.0.0/NPD/P_name.php?"+test;
JSONObject json = jParser.FunctionParser(link_url);
try {
jsonArray = json.getJSONArray("team");
items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i]=jsonObject.getString("name");
}
return(items);}
catch (JSONException e) {
e.printStackTrace();
}
return items;
}
public String[] useaJson(String test2) {
JSONParser jjParser = new JSONParser();
String links_url = "http://10.2.0.0/NPD/P_name1.php?"+test2;
JSONObject json = jjParser.FunctionParser(links_url);
try {
jsonArray = json.getJSONArray("ateam");
aitems = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
aitems[i]=jsonObject.getString("name");
}
return(aitems);}
catch (JSONException e) {
e.printStackTrace();
}
return aitems;
}
}
According to json.org (http://www.json.org/javadoc/org/json/JSONArray.html#getJSONObject(int)):
getJSONObject throws JSONException if there is no value for the index
or if the value is not a JSONObject.
How your ateam array is being populated? Seems that you are adding null values inside it. Make sure that your ateam array is populated only with initialized JSONObject, maybe the JSONObject is null when you add it to ateam array so you must check if is null before adding it to ateam.

Populate spinner with years dynamically in 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.

How to add spinner inside a listview

i'd like each for row in listview has a spinner. But can not. please help me.
//Setting player
adapter = new SimpleAdapter(this, list, R.layout.custom_listitem_edit,
new String[] { "cat", "duration", "order", "id_pos"}, new int[] {
R.id.spn_edibt_selectcat,
/*R.id.spn_editbt_selectpos,*/
R.id.txt_editbt_dur,
R.id.txt_editbt_order,
R.id.txt_edit_idpos
});
populateList();
setListAdapter(adapter);
static final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
/* Set thong tin cho listview */
private void populateList() {
cursor_position_mycurrent = dbHelper_position_mycurrent.fetchAllPositionMyCurrentById(id_bt);
startManagingCursor(cursor_position_mycurrent);
Log.e("SO dong lay duoc la", String.valueOf(cursor_position_mycurrent.getCount()));
if (cursor_position_mycurrent != null) {
cursor_position_mycurrent.moveToFirst();
while (cursor_position_mycurrent.isAfterLast() == false) {
HashMap<String, String> tmp = new HashMap<String, String>();
Log.e("DUR", cursor_position_mycurrent.getString(5));
Log.e("DUR", cursor_position_mycurrent.getString(6));
tmp.put("duration", cursor_position_mycurrent.getString(5));
tmp.put("order", cursor_position_mycurrent.getString(6));
tmp.put("id_pos", cursor_position_mycurrent.getString(0));
//Setting spinner category
cursor_position_allposition =dbHelper_position_allposition.fetchAllPositions();
array_spinner_category = new String[cursor_position_allposition.getCount()];
if (cursor_position_allposition != null) {
int i = 0;
cursor_position_allposition.moveToFirst();
while (cursor_position_allposition.isAfterLast() == false) {
array_spinner_category[i] = cursor_position_allposition.getString(0);
cursor_position_allposition.moveToNext();
i++;
}
cursor_position_allposition.close();
}
Log.e("ALL POS", String.valueOf(cursor_position_allposition.getCount()));
Spinner spinner = (Spinner) findViewById(R.id.spn_edibt_selectcat);
int m = 3;//Integer.parseInt(camping.rulesList.getMaxPers().get(camping.tipSel));
String[] array_spinner=new String[m];
for (int indice = 0; indice < m; indice++)
{
if (indice == 0) array_spinner[indice] = String.format("%d persona", indice+1);
else array_spinner[indice] = String.format("%d persone", indice+1);
}
final ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, array_spinner);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aa);
list.add(tmp);
cursor_position_mycurrent.moveToNext();
}
}
}

Categories

Resources