I have abit of a problem. I have a main class (MyActivity) in which the user select a location from a spinner. When the user selected an item this should trigger a Async method which then returns the data in form of a measurement(which includes temp, winddirection and more).
Ive got the spinner and the Async task to start as they should. My problem is, how should i get the measurment back so i can read it in the MyActivity and place the data in som TextViews. A thing i tried is tho use the method setTextView from the Async task (onPostExecute) but then i got a Null exception syaing that the TextView isnt set up yet.
Code for MyActivity :
public class MyActivity extends Activity {
//Activity which basicly is the GUI. The user can click the menu button to configure widgets from here.
//TODO: Daniel add some fancy wind graphs
TextView txt_spotName;
TextView txt_spotTemp;
TextView txt_spotWind;
Measurement measurement2;
public Boolean b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt_spotName = (TextView) findViewById(R.id.txt_spotName);
txt_spotTemp = (TextView) findViewById(R.id.txt_spotTemp);
txt_spotWind = (TextView) findViewById(R.id.txt_spotWindDir);
final Spinner spotChooser = (Spinner) findViewById(R.id.spin_spotvelger);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spots, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spotChooser.setAdapter(adapter);
spotChooser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
//User selected a value in the spinner
//String s = String.valueOf(pos);
String spotName = (String) spotChooser.getItemAtPosition(pos);
Spots spots = new Spots();
spotName = spots.getSpotIdFromName(spotName);
//spotName now equals the ID instead of the Name
//Call the updateNow function with the ID and a debug parameter.
VindsidenUpdateService vindsidenUpdateService = new VindsidenUpdateService();
vindsidenUpdateService.updateNow(spotName, "user");
//Here the code shoud somehow wait untill "measurement2" is set by the Async task.
//My biggest issue that i dont know how to do this code right.
try {
txt_spotName.setText(measurement2.getStationID());
} catch (Exception e) {
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public void setTextView (String s ){
//This should in theory work but i cant get it to setText.
//The error i get is basicly that "txt_spotname" = null
txt_spotName.setText(s);
}
//OnOptionsItemSelected is called when user clicks the menu/overflow button
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
//Switch case to check which menu item user clicked
switch (menuItem.getItemId()) {
case R.id.settings:
//Define settings page to be com.vindsiden.windwidget/.Settings
String SettingsPage = "com.vindsiden.windwidget/.Settings";
try {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(ComponentName.unflattenFromString(SettingsPage));
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
} catch (ActivityNotFoundException e)
{
//This will be called when activity is not found (Check manifest)
Log.d("", e.getStackTrace().toString());
}
break;
case R.id.About:
About.Show(this);
//TODO : Add featurerequest/send email to developer
//http://stackoverflow.com/questions/2197741/how-to-send-email-from-my-android-application
}
return true;
}
}
Code for Async Class :
public class VindsidenUpdateService {
private static final String NEXT_SCHEDULE_URI_POSTFIX = "/next_schedule";
private static final String WIDGET_URI_PREFIX = "/widget_id/";
List<Measurement> measurements = null;
public void updateNow(String stationID, String from) {
//upDateNow is just used to call the GetData method
//stationID = "1";
try {
if (stationID.equals("")) {
//Actually used for debugging
stationID = "1";
}
//Start the GetData
String[] input = {String.valueOf(stationID), from};
new GetData().execute(input);
} catch (Exception e) {
Log.d("Vindsiden2" + " UpdateNow", "Failed");
}
}
class GetData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... data) {
String s = data.toString();
Log.d("Vindsiden2 + Convert array to string", s);
try {
String urlString = WindWidgetConfig.getVindsidenUrlPrefix() + data[0].toString()
+ WindWidgetConfig.getVindsidenUrlPostfix();
Log.d("Vindsiden2", urlString);
measurements = (new VindsidenWebXmlReader()).loadXmlFromNetwork(urlString);
Log.d("Vindsiden2", measurements.toString());
//Filldata here
} catch (IOException e) {
Log.d("Vindsiden2", "An IO exception occured. Stack follows: ");
Log.d("Vindsiden2", e.getStackTrace().toString());
// xmlRetrievalSuccessful = false;
// not certain how robust throwing a runtime exception is, might break stuff with recurrence etc!
// throw new RuntimeException(getResources().getString(R.string.connection_error));
} catch (XmlPullParserException e) {
Log.d("Vindsiden", "An XmlPullParserException occured. Stack follows: ");
Log.d("Vindsiden", e.getStackTrace().toString());
//xmlRetrievalSuccessful = false;
// throw new RuntimeException(getResources().getString(R.string.xml_error));
}
Log.d("Vindsiden ", "Got data, now find measurment");
String Return = "";
int array = data.length;
if (array == 1) {
//stop
Return = "";
} else if (array == 2) {
Return = data[1].toString();
}
return Return;
}
protected void onPostExecute(String result) {
// fillData(measurements);
MyActivity myActivity = new MyActivity();
myActivity.measurement2 = measurements.get(0);
//ive also tried to create a setTextView method in MyActivity but this dosent seem to work either.
//Commect
}
}
}
The full code is avalible at : https://github.com/danielgolan/Vindsiden/tree/Daniel2