SetText get toString
The differences between the Chapter 8 example and this one mostly lie within . We get an for the XML file (), then use the built‑in XML parsing logic to parse the file into a DOM Document, pick out the word elements, then pour the value attributes into an for use by the .
The resulting activity looks the same as before (Figure 18‑1), since the list of words is the same, just relocated.
Figure 18‑1. The StaticFileDemo sample application
Of course, there are even easier ways to have XML files available to you as pre‑packaged files, such as by using an XML resource. That is covered in the next chapter. However, while this example uses XML, the file could just as easily have been a simple one‑word‑per‑line list, or in some other format not handled natively by the Android resource system.
Readin’ ’n’ Writin’
Reading and writing your own, application‑specific data files is nearly identical to what you might do in a desktop Java application. The key is to use and on your or other to get an and , respectively. From that point forward, the process is not much different from using regular Java I/O logic:
• Wrap those streams as needed, such as using an or for text‑based I/O.
• Read or write the data.
• Use to release the stream when done.
If two applications both try reading a file via , they will each access their own edition of the file. If you need to have one file accessible from many places, you probably want to create a content provider, as will be described in Chapter 28.
Note that and do not accept file paths (e.g., ), just simple filenames.
The following code shows the layout for the world’s most trivial text editor, pulled from the sample application available on the Apress Web site:
All we have here is a large text‑editing widget with a Close button above it. The Java is only slightly more complicated:
Дата добавления: 2015-05-16; просмотров: 760;