Android ListView
38 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is a characteristic of external storage?

  • It can be removed or transferred to another device if needed. (correct)
  • It is private to your app.
  • It is always present in every device.
  • It is wiped when the app is uninstalled in all cases.
  • What is required to read/write the device's external storage?

  • No permission is required.
  • Only read permission is required.
  • Explicit permission must be requested in the app's AndroidManifest.xml file. (correct)
  • The user's permission is not required.
  • What is a difference between internal and external storage?

  • External storage is always present in every device.
  • Internal storage is private to your app.
  • Internal storage is smaller than external storage. (correct)
  • External storage is private to your app.
  • What is used to read a file in internal storage?

    <p>Scanner</p> Signup and view all the answers

    What is used to write a short text file to internal storage?

    <p>PrintStream</p> Signup and view all the answers

    What happens to the external storage when the app is uninstalled?

    <p>It is not wiped in any case.</p> Signup and view all the answers

    What is the size of internal storage?

    <p>It is smaller than external storage.</p> Signup and view all the answers

    What is the purpose of openFileInput and openFileOutput methods?

    <p>To read and write files from internal storage.</p> Signup and view all the answers

    What is the purpose of passing a unique ID number to startActivityForResult?

    <p>To specify the request code for the result</p> Signup and view all the answers

    What happens when startActivityForResult is called?

    <p>The calling activity continues executing immediately</p> Signup and view all the answers

    What is the purpose of the onActivityResult method?

    <p>To check for the unique ID and retrieve extra data from the intent</p> Signup and view all the answers

    How does the called activity send a result back?

    <p>By calling setResult and finish</p> Signup and view all the answers

    What is the purpose of the Intent in the onActivityResult method?

    <p>To store extra data to be sent back</p> Signup and view all the answers

    Why is it necessary to modify the called activity to send a result back?

    <p>To allow the calling activity to retrieve the result</p> Signup and view all the answers

    What is the purpose of an adapter in a ListView?

    <p>To help turn list data into list view items</p> Signup and view all the answers

    What is the difference between a static list and a dynamic list?

    <p>Static lists are fixed and dynamic lists are generated at runtime</p> Signup and view all the answers

    What is the purpose of the android:entries attribute in a ListView?

    <p>To specify the array of options to appear in the list</p> Signup and view all the answers

    What is the default layout for lists in a ListView?

    <p>android.R.layout.simple_list_item_1</p> Signup and view all the answers

    What is the method used to attach an adapter to a ListView?

    <p>setAdapter()</p> Signup and view all the answers

    What is the type of array that can be used to create an ArrayAdapter?

    <p>Either String[] or ArrayList</p> Signup and view all the answers

    What is the purpose of the android:id attribute in a ListView?

    <p>To set the unique ID for the ListView for use in Java code</p> Signup and view all the answers

    What is the location of the file that contains the list of options for a dynamic list?

    <p>res/raw/oses.txt</p> Signup and view all the answers

    What method is used to check if the external storage is writable?

    <p>Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())</p> Signup and view all the answers

    What permission is required to access web data in Android?

    <p>INTERNET</p> Signup and view all the answers

    How are sound files referred to in Java code?

    <p>R.raw.filename</p> Signup and view all the answers

    What is the purpose of the setLooping method in MediaPlayer?

    <p>To control the looping of the media</p> Signup and view all the answers

    What is the purpose of the Scanner class in reading web data?

    <p>To read the data from a URL</p> Signup and view all the answers

    What is the purpose of the getCurrentPosition method in MediaPlayer?

    <p>To get the current position of the media</p> Signup and view all the answers

    What is the purpose of the isPlaying method in MediaPlayer?

    <p>To check if the media is playing</p> Signup and view all the answers

    How do activities communicate with each other in Android?

    <p>Through intents</p> Signup and view all the answers

    What is the primary difference between internal and external storage?

    <p>Internal storage is private to each app.</p> Signup and view all the answers

    What is the main purpose of the java.io.File class?

    <p>To represent a file or directory.</p> Signup and view all the answers

    What is the purpose of the getFilesDir() method?

    <p>To get the internal storage directory for the app.</p> Signup and view all the answers

    What is the purpose of the java.io.InputStream and OutputStream classes?

    <p>To represent flows of data bytes from a source or destination.</p> Signup and view all the answers

    What happens to the internal storage when an app is uninstalled?

    <p>It is wiped out.</p> Signup and view all the answers

    What is the purpose of the getResources().openRawResource(R.raw.id) method?

    <p>To read a raw resource file.</p> Signup and view all the answers

    How much storage is typically available in internal storage?

    <p>1-4 GB</p> Signup and view all the answers

    What is the purpose of the openFileInput("name", mode) method?

    <p>To read a file from internal storage.</p> Signup and view all the answers

    Study Notes

    ListView

    • An ordered collection of selectable choices
    • Key attributes in XML:
      • android:clickable to set the list to be clickable or not
      • android:id to assign a unique ID for use in Java code
      • android:entries to set the options to appear in the list, which must match an array in strings.xml

    Static Lists

    • Content is fixed and known before the app runs
    • Declare the list elements in the strings.xml resource file
    • Example: Android, iPhone, Max OS X

    Dynamic Lists

    • Content is read or generated as the program runs
    • Comes from a data file, internet, etc.
    • Must be set in Java code
    • Example: reading a file oses.txt to create a list

    List Adapters

    • Helps turn list data into list view items
    • Common adapters: ArrayAdapter, CursorAdapter
    • Syntax for creating an adapter: ArrayAdapter name = new ArrayAdapter(activity, layout, array);
    • Attach the adapter to the list by calling the setAdapter method of the ListView object in Java code

    Waiting for a Result

    • If the calling activity wants to wait for a result from the called activity:
      • Call startActivityForResult instead of startActivity
      • Pass a unique ID number to represent the action being performed
      • Write an onActivityResult method that will be called when the second activity is done
      • Check for the unique ID as was passed to startActivityForResult
      • Ask the intent for any extra data
      • Modify the called activity to send a result back using setResult and finish methods

    Files and Storage

    • Android can read/write files from two locations:
      • Internal storage
      • External storage

    Internal Storage

    • Built into the device
    • Guaranteed to be present
    • Typically smaller (~1-4 GB)
    • Can't be expanded or removed
    • Specific and private to each app
    • Wiped out when the app is uninstalled

    File and Streams

    • java.io.File represents a file or directory
    • Methods: canRead, canWrite, create, delete, exists, getName, getParent, getPath, isFile, isDirectory, lastModified, length, listFiles, mkdir, mkdirs, renameTo
    • java.io.InputStream, OutputStream represent flows of data bytes from/to a source or destination
    • Stream objects are often passed as a parameter to other objects like java.util.Scanner, java.io.BufferedReader, java.io.PrintStream to do the actual reading/writing

    Using Internal Storage

    • An activity has methods to read/write files:
      • getFilesDir() returns internal directory for the app
      • getCacheDir() returns a "temp" directory for scrap files
      • getResources().openRawResource(R.raw.id) reads an input file from res/raw/
      • openFileInput("name", mode) opens a file for reading
      • openFileOutput("name", mode) opens a file for writing
    • Can use these methods to read/write files on the device
    • Many methods return standard java.io.File objects
    • Some return java.io.InputStream or OutputStream objects, which can be used with standard classes like Scanner, BufferedReader, and PrintStream to read/write files

    Internal Storage Examples

    • Example 1: read a file and put its contents into a TextView
    • Example 2: write a short text file to the internal storage and read it back

    External Storage

    • External storage: Card that is inserted into the device (such as a MicroSD card)
    • Can be much larger than internal storage (~8-32 GB)
    • Can be removed or transferred to another device if needed
    • May not be present, depending on the device
    • Read/writable by other apps and users; not private to the app
    • Not wiped when the app is uninstalled, except in certain cases

    External Storage Permission

    • If the app needs to read/write the device's external storage, it must explicitly request permission to do so in the AndroidManifest.xml file
    • On install, the user will be prompted to confirm the app's permissions

    Checking if Storage is Available

    • isExternalStorageWritable() method to check if external storage is writable
    • isExternalStorageReadable() method to check if external storage is readable

    Accessing Web Data

    • To read data from the web, request the INTERNET permission in the AndroidManifest.xml file
    • Use the java.net.URL class to connect to a file or page at a given URL and read its data

    Playing Sound Effects

    • Find sound files such as .WAV, .MP3
    • Put sound files in the project folder app/src/main/res/raw
    • Refer to audio files in Java code as R.raw.filename
    • Load and play clips using Android's MediaPlayer class
    • Methods: create, start, stop, pause, isLooping, isPlaying, getCurrentPosition, release, seekTo, setDataSource, setLooping

    Multiple Activities

    • Many apps have multiple activities
    • Example: address book app with main activity for listing contacts and another activity for viewing details
    • An activity can launch another activity in response to an event
    • The activity can pass data to the other activity
    • The second activity can send data back to the first activity when it is done

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Understanding the attributes and functionality of ListView in Android development.

    More Like This

    Android ListView
    99 questions
    Android ListView and Adapters
    29 questions

    Android ListView and Adapters

    JubilantFrenchHorn287 avatar
    JubilantFrenchHorn287
    Use Quizgecko on...
    Browser
    Browser