Java—Create a location book using Android

Java—Create a location book using Android … here is a solution to the problem.

Java—Create a location book using Android

I’m new to Android and I’m working on an app that should display a list of places (class extension activity list), when the user selects a place, it opens a new activity with place details (name, address, phone number, service…).
I’m actually looking for an easy way to store these predefined location details + an easy way to display them later on the app.

Solution

Actually, the best solution I’ve found is:

  1. Save a static file in your application at compile time, saving the file in your project’s res/raw/ directory.

  2. Open the file using openRawResource() to pass the R.raw. Resource number. This method returns an InputStream that you can use to read the file (but not write to the original file).

InputStream dataIS =
getResources().openRawResource(R.raw.location);

  1. Convert the input stream into a buffered reader, and then you can store data in Sqlite3 tables and use them wherever you want.
public void fillDB(){
        InputStreamReader in= new InputStreamReader(dataIS);
        BufferedReader dataBR= new BufferedReader(in);
        String dataLine;
        try {
        while ((dataLine = dataBR.readLine()) != null)   {
           split the data line
          dataLineTokenizer = new StringTokenizer(dataLine, ":");
                SQL query + save data to database
          String sql =  "INSERT INTO location ...";
          execute query
          Log.v("Test Saving", sql);
          clubberDB.execSQL(sql);
        }
        } catch (IOException e) {
           TODO Auto-generated catch block
          e.printStackTrace();
        }
    }

Related Problems and Solutions