Last active
October 7, 2017 07:42
-
-
Save Butch78/b5cc1b1d608b291254f523e29b5d0aa0 to your computer and use it in GitHub Desktop.
ListView Mobile App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swindroid.suntime.calc; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
/** | |
* Created by Matthew on 28/09/2017. | |
*/ | |
public class CSVFile { | |
private InputStream inputStream; | |
public CSVFile(InputStream inputStream) | |
{ | |
this.inputStream = inputStream; | |
} | |
public ArrayList<String[]> read() | |
{ | |
ArrayList<String []> resultList = new ArrayList<String[]>(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | |
try | |
{ | |
String csvLine; | |
while ((csvLine = reader.readLine()) != null) { | |
String[] row = csvLine.split(","); | |
resultList.add(row); | |
} | |
} | |
catch(IOException ex) | |
{ | |
throw new RuntimeException("Error in CSV file" +ex); | |
} | |
finally { | |
try{ | |
inputStream.close(); | |
} | |
catch (IOException e) | |
{ | |
throw new RuntimeException("Error while closing stream:"+e); | |
} | |
} | |
return resultList; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swindroid.suntime.ui; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import org.w3c.dom.Text; | |
import java.util.ArrayList; | |
import java.util.List; | |
import swindroid.suntime.R; | |
import swindroid.suntime.calc.GeoLocation; | |
import static swindroid.suntime.R.layout.main; | |
/** | |
* Created by Matthew on 5/10/2017. | |
*/ | |
public class GeoAdapter extends ArrayAdapter<GeoLocation> { | |
public GeoAdapter(Context context, ArrayList<GeoLocation> geoLocations){ | |
super(context, 0, geoLocations); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) | |
{ | |
GeoLocation geoLocation = getItem(position); | |
if(convertView == null) | |
{ | |
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_geo, parent, false); | |
} | |
TextView textViewGeoName = (TextView)convertView.findViewById(R.id.TextViewGeoName); | |
TextView textViewGeoLocation = (TextView)convertView.findViewById(R.id.TextViewGeoTimeZone); | |
textViewGeoName.setText("Name: " + geoLocation.getLocationName()); | |
textViewGeoLocation.setText("Lat/Long: " + Double.toString(geoLocation.getLatitude()) + " " + Double.toString(geoLocation.getLongitude())); | |
return convertView; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swindroid.suntime.ui; | |
import java.io.InputStream; | |
import java.lang.reflect.Array; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.TimeZone; | |
import swindroid.suntime.R; | |
import swindroid.suntime.calc.AstronomicalCalendar; | |
import swindroid.suntime.calc.CSVFile; | |
import swindroid.suntime.calc.GeoLocation; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.Button; | |
import android.widget.DatePicker; | |
import android.widget.DatePicker.OnDateChangedListener; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class Main extends Activity | |
{ | |
private GeoLocation currentGeoLocation; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
initializeUI(); | |
} | |
public GeoLocation getCurrentGeoLocation() | |
{ | |
return this.currentGeoLocation; | |
} | |
public void setCurrentGeoLocation(GeoLocation currentGeoLocation) | |
{ | |
this.currentGeoLocation = currentGeoLocation; | |
} | |
private void initializeUI() | |
{ | |
ArrayList<String []> tempArrayList; | |
tempArrayList = readCSVFile(); | |
String[] tempStringArray; | |
final ArrayList<GeoLocation> geoArray = new ArrayList<GeoLocation>(); | |
for(int i = 0; i < tempArrayList.size(); i++) | |
{ | |
tempStringArray = tempArrayList.get(i); | |
String timeZone = tempStringArray[3]; | |
TimeZone tz = TimeZone.getTimeZone(timeZone); | |
GeoLocation geoLocation = new GeoLocation(tempStringArray[0], Double.parseDouble(tempStringArray[1]), Double.parseDouble(tempStringArray[2]), tz); | |
geoArray.add(geoLocation); | |
} | |
GeoAdapter adapter = new GeoAdapter(getApplicationContext(), geoArray); | |
ListView geoListView = (ListView) findViewById(R.id.GeoListView); | |
geoListView.setAdapter(adapter); | |
geoListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view,int position, long id) { | |
GeoLocation geoLocation = geoArray.get(position); | |
setCurrentGeoLocation(geoLocation); | |
} | |
}); | |
Button buttonUpdateLocation = (Button) findViewById(R.id.buttonUpdate); | |
buttonUpdateLocation.setOnClickListener(new View.OnClickListener(){ | |
public void onClick(View v) | |
{ | |
selectGeoLocation(); | |
} | |
}); | |
} | |
public void selectGeoLocation() | |
{ | |
DatePicker dp = (DatePicker) findViewById(R.id.datePicker); | |
Calendar cal = Calendar.getInstance(); | |
int year = cal.get(Calendar.YEAR); | |
int month = cal.get(Calendar.MONTH); | |
int day = cal.get(Calendar.DAY_OF_MONTH); | |
dp.init(year,month,day,dateChangeHandler); // setup initial values and reg. handler | |
updateTime(year, month, day); | |
} | |
private ArrayList<String[]> readCSVFile() | |
{ | |
InputStream inputStream = getResources().openRawResource(R.raw.au_locations); | |
CSVFile csvFile = new CSVFile(inputStream); | |
return csvFile.read(); | |
} | |
private void updateTime(int year, int monthOfYear, int dayOfMonth) | |
{ | |
TextView textViewLocation = (TextView) findViewById(R.id.locationTV); | |
GeoLocation geoLocationCurrent = getCurrentGeoLocation(); | |
textViewLocation.setText(geoLocationCurrent.getName()); | |
AstronomicalCalendar ac = new AstronomicalCalendar(geoLocationCurrent); | |
ac.getCalendar().set(year, monthOfYear, dayOfMonth); | |
Date srise = ac.getSunrise(); | |
Date sset = ac.getSunset(); | |
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); | |
TextView sunriseTV = (TextView) findViewById(R.id.sunriseTimeTV); | |
TextView sunsetTV = (TextView) findViewById(R.id.sunsetTimeTV); | |
Log.d("SUNRISE Unformatted", srise+""); | |
sunriseTV.setText(sdf.format(srise)); | |
sunsetTV.setText(sdf.format(sset)); | |
} | |
OnDateChangedListener dateChangeHandler = new OnDateChangedListener() | |
{ | |
public void onDateChanged(DatePicker dp, int year, int monthOfYear, int dayOfMonth) | |
{ | |
updateTime(year, monthOfYear, dayOfMonth); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment