0
In my original version I populated my spinner from an array, placed in the strings.xml
And to know which selection the user made used the following code
code 1
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
// get selected option
String[] names = getResources().getStringArray(R.array.confirmation_memo);
selectedName = names[position];
}
But now I take the array of a URL and, for that, I had to implement the Asynctask method to get the data Online from the emrpesa website.
with the following code:
code 2
public class JSONOAsyncTask extends AsyncTask<String, Void, Boolean> implements AdapterView.OnItemSelectedListener{
private Spinner spinner;
private ArrayAdapter<String> dataAdapter;
@Override
protected void onPreExecute() {
super.onPreExecute();
spinner = (Spinner) findViewById( R.id.memo_confirmation_spinner );
spinner.setOnItemSelectedListener(PostConfirmationActivity.this);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
Log.e("****** MESSAGE ******", " Json Object = " + JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList"));
List < String > categories = new ArrayList < String > ();
JSONArray array = (JSONArray) JSONParser.getJSONFromUrl(URL).get("ReportDetailTextList");
for (int i = 0; i < array.length(); i++) {
categories.add(array.get(i).toString());
// puting the first option from the URL, to be the "default memo field".
if(i == 0) {
selectedName = array.get(i).toString();
}
}
dataAdapter = new ArrayAdapter < String > (getBaseContext(), android.R.layout.simple_spinner_dropdown_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
// putting adapter in to data
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
And everything works fine, unless I can’t get the "code 1", pass to use the URL array (which is created inside Asynctask)... and so make the choice in the spinner,
That is to say:
within Asynctask, create the ARRAY on the line:
JSONArray array = (JSONArray) SONParser.getJSONFromUrl(URL).get("ReportDetailTextList");
and would like to use this OFF array from Asynctask, in the code
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
// get selected option
String[] names = getResources().getStringArray(R.array.confirmation_memo);
selectedName = names[position];
}
Or is there a better way if you take the choice of the spinner? following the idea of using Asynctask?
@Camilayamamoto From what I understand, you will call your webservice, pick up values and want to put them in a Spinner, correct? If so, you need to interpret your Ws response, add in an array of strings and add "manually".
– Carlos Bridi
@Camilayamamoto I made an adjustment in the answer and I finally added an example of how to do what I mentioned above, if you have any questions, call me. Do not be discouraged!
– Carlos Bridi
For me, Java had its nod when everyone could see in practice the multiplatform, etc...when the new languages came, cross-Platform, everyone began to wonder about it...I think Java has its benefits, just like all other languages also have... Now everyone is passionate about Ionic, angular, and so it goes, every cycle, a new language of the moment..
– Carlos Bridi
Let’s go continue this discussion in chat.
– Carlos Bridi
Thank you Carlos! you helped me to study more! Thank you so much for the good will! We, the newbies, need more people like you... It’s hard to learn! and you guys are not charging us even more.... Useful otherwise, help and give more touches!!! That’s how we learn and have more respect for you! Are you listening to @Ack Lay ?? happy here!
– Camila Yamamoto
Hello Camila, sorry, yesterday the internet operator did maintenance on the network and I could not answer you in chat. If you ever go up this project in Git, I can help you re-create and separate the concepts from the better classes... Thank you and keep moving forward ;)
– Carlos Bridi
cool! thanks!
– Camila Yamamoto