theXs
theXs

Reputation: 437

Open file in a class

Howdy, I currently have my main activity in Android. It's called start activity. My programm is designed so that it loads an XML File from the web, and parses it.

Now in future I want to add also the ability that the program loads the XML file and instead of parsing it every runtime it loads the data from an SQLLite database.

However for that I need to write an interface which capsulates the different functionalities.

In case that the XML needs to be used I wrote me a small class which should cover the XML loading - but I cannot use openFileInput(...) ... since that class is not an activity ( does it need to be one?? ).

package com.android.mensa.handledata;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.app.Activity;

import com.android.mensa.datastructure.LunchPlace;
import com.android.mensa.getData.XMLHandler;
import com.android.mensa.interfaces.Data;

/**
 * This class implements the interface Data
 * and gathers the necessary informations out of the XML File on the actual device.
 */
public class XMLData implements Data{

    @Override
    public void getMenuforDay() {
        // TODO Auto-generated method stub

    }

    public TreeMap<String, LunchPlace> open()
    {
        File test = new File("lunchfile");
        FileInputStream fis;
        TreeMap<String, LunchPlace> places = new TreeMap<String, LunchPlace>();
        try {
            fis = openFileInput("lunchfile");
            XMLHandler xmlhandler = new XMLHandler();
            places  = xmlhandler.parse(fis);            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return places;
    }
}

I guess my question is pretty basic: does the XMLhandler helper class ( which is supposed to return a TreeMap ) needs to be an activity or how else can I achieve my functionality?

Upvotes: 0

Views: 217

Answers (1)

Anton
Anton

Reputation: 378

Of the top of my head I can think of two ways to to it:

The first one is to just use:

FileInputStream fis = new FileInputStream("lunchfile");

That will however read the file "lunchfile" from the root of the sd-card, I'm not entierly sure if that's what you want, but if you do it this way I'd recommend changing it "/yourapplication/lunchfile" instead.

The other is to just pass the activity over to the reader class.

public TreeMap<String, LunchPlace> open(Context activity)
{
        [...]
        fis = activity.openFileInput("lunchfile");
        [...]
}

Hope it helps :)

Upvotes: 1

Related Questions