Continue to build the skills needed to complete your final course project. For t
ID: 3668806 • Letter: C
Question
Continue to build the skills needed to complete your final course project. For this assignment, you will be creating a project that allows a user to input pieces of information. In this case, you will begin to create and store your own treasure items to be used for your final Geocaching course project. Write a test application called project06 which asks the user to input 5 pieces of information, and write these to an XML file within the application's /files directory Ensure that when the application restarts, the data from the file can be read and displayed back to the user during onCreate(). This must be done using the android studio development. A single activity is required. Inputs should be: First Name , Last Name, Address, Phone Number, Age.
Explanation / Answer
public class MainActivity extends AppCompatActivity { private EditText firstName; private EditText lastName; private EditText address; private EditText phoneNumber; private EditText age; private Button save; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); firstName = (EditText) findViewById(R.id.first_name); lastName = (EditText) findViewById(R.id.last_name); address = (EditText) findViewById(R.id.address); phoneNumber = (EditText) findViewById(R.id.phone_number); age = (EditText) findViewById(R.id.age); save = (Button) findViewById(R.id.save); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { // create person object from user given data Person person = new Person(); person.setFirstName(firstName.getText().toString()); person.setLastName(lastName.getText().toString()); person.setAddress(address.getText().toString()); person.setPhoneNumber(phoneNumber.getText().toString()); try { person.setAge(Integer.parseInt(age.getText().toString())); } catch (Exception ex) { ex.printStackTrace(); // set default age 18 person.setAge(18); } File filesDirectory = getDir("files", MODE_PRIVATE); File outputFile = new File(filesDirectory, "person_records.xml"); FileOutputStream fos = new FileOutputStream(outputFile); fos.write(new PersonXMLIO().getXMLString(person).getBytes()); fos.close(); Toast.makeText(MainActivity.this, "Save successful!", Toast.LENGTH_LONG).show(); } catch (Exception ex) { ex.printStackTrace(); Toast.makeText(MainActivity.this, "Save failed!", Toast.LENGTH_LONG).show(); } } }); // check if file exists and try to read available record try { File fileDir = getDir("files", MODE_PRIVATE); File inputFile = new File(fileDir, "person_records.xml"); FileInputStream fis = new FileInputStream(inputFile); Person person = new PersonXMLIO().parse(fis); if (person == null) { Toast.makeText(this, "Person data not available at the moment!", Toast.LENGTH_LONG).show(); } else { firstName.setText(person.getFirstName()); lastName.setText(person.getLastName()); address.setText(person.getAddress()); phoneNumber.setText(person.getPhoneNumber()); try { age.setText(Integer.toString(person.getAge())); } catch (Exception ex) { age.setText(""); } } } catch (Exception ex) { ex.printStackTrace(); Toast.makeText(this, "Person data not available at the moment!", Toast.LENGTH_LONG).show(); } } private class Person { private String firstName; private String lastName; private String address; private String phoneNumber; private int age; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } private class PersonXMLIO { public static final String PERSON = "person"; public static final String FIRST_NAME = "firstName"; public static final String LAST_NAME = "lastName"; public static final String ADDRESS = "address"; public static final String PHONE_NUMBER = "phoneNumber"; public static final String AGE = "age"; public String getXMLString(Person person) throws Exception { XmlSerializer xmlSerializer = Xml.newSerializer(); StringWriter writer = new StringWriter(); xmlSerializer.setOutput(writer); // start DOCUMENT xmlSerializer.startDocument("UTF-8", true); // open tag: xmlSerializer.startTag("", PersonXMLIO.PERSON); // add first name tag xmlSerializer.startTag("", PersonXMLIO.FIRST_NAME); xmlSerializer.text(person.getFirstName()); xmlSerializer.endTag("", PersonXMLIO.FIRST_NAME); // add last name tag xmlSerializer.startTag("", PersonXMLIO.LAST_NAME); xmlSerializer.text(person.getLastName()); xmlSerializer.endTag("", PersonXMLIO.LAST_NAME); // add address tag xmlSerializer.startTag("", PersonXMLIO.ADDRESS); xmlSerializer.text(person.getAddress()); xmlSerializer.endTag("", PersonXMLIO.ADDRESS); // add phone number tag xmlSerializer.startTag("", PersonXMLIO.PHONE_NUMBER); xmlSerializer.text(person.getPhoneNumber()); xmlSerializer.endTag("", PersonXMLIO.PHONE_NUMBER); // add first name tag xmlSerializer.startTag("", PersonXMLIO.AGE); xmlSerializer.text(Integer.toString(person.getAge())); xmlSerializer.endTag("", PersonXMLIO.AGE); xmlSerializer.endTag("", PersonXMLIO.PERSON); // end DOCUMENT xmlSerializer.endDocument(); return writer.toString(); } public Person parse(InputStream is) { Person person = null; try { // get a new XmlPullParser object from Factory XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); // set input source parser.setInput(is, null); // get event type int eventType = parser.getEventType(); // process tag while not reaching the end of document while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { // at start of document: START_DOCUMENT case XmlPullParser.START_DOCUMENT: person = new Person(); break; // at start of a tag: START_TAG case XmlPullParser.START_TAG: // get tag name String tagName = parser.getName(); // if , get attribute: 'id' if (tagName.equalsIgnoreCase(PersonXMLIO.FIRST_NAME)) { person.setFirstName(parser.nextText()); } // if else if (tagName.equalsIgnoreCase(PersonXMLIO.LAST_NAME)) { person.setLastName(parser.nextText()); } // if else if (tagName.equalsIgnoreCase(PersonXMLIO.ADDRESS)) { person.setAddress(parser.nextText()); } // if else if (tagName.equalsIgnoreCase(PersonXMLIO.PHONE_NUMBER)) { person.setAddress(parser.nextText()); } // if else if (tagName.equalsIgnoreCase(PersonXMLIO.AGE)) { person.setAge(Integer.parseInt(parser.nextText())); } break; } // jump to next event eventType = parser.next(); } // exception stuffs } catch (XmlPullParserException e) { person = null; } catch (IOException e) { person = null; } // return Study object return person; } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.