marblewhite
marblewhite

Reputation: 45

Using Singleton methods in activities (Android)

So I have been working on this android project in Android Studio where I let users create to-do lists. It's a class assignment and I have to work with a Singleton. I have been reading a lot of info and questions/answers about it but it has only made me progressively more confused.

In my MainActivity I tried to call the method ReadTitlesFromFile, but I get the errors:

Modifier 'public' not allowed here

and

Inner classes cannot have static declarations

Even though the code is exactly the same as the example that I found somewhere. Can somebody help me with fixing the error?

This is my Singleton:

public class ToDoManagerSingleton {
private ArrayList<ToDoList> listTitles;

// One single instance for the entire app
private static ToDoManagerSingleton ourInstance = new ToDoManagerSingleton();

// Constructor
private ToDoManagerSingleton() {
    // Empty ArrayList with in it the different lists
    listTitles = new ArrayList<ToDoList>();
}

// Method to get the one single instance
public static ToDoManagerSingleton getInstance() {
    return ourInstance;
}

// Getter and setter
public void setListTitles (ArrayList listTitlesArg){
    listTitles = listTitlesArg;
}

public ArrayList getListTitles(){
    return listTitles;
}

// Add new title to list
public void AddNewTitle(ToDoList title){
    listTitles.add(title);
}

// Write list titles to a file
public void WriteListTitlesToFile (Context context){
    PrintStream outstream = null;
    try{
        outstream = new PrintStream(context.openFileOutput("titlesfile.txt", Context.MODE_PRIVATE));
        for (ToDoList listTitle : listTitles) {
            outstream.println(listTitle);
        }
    }
    catch (FileNotFoundException e){
        e.printStackTrace();
    }
}

// Read titlesfile.txt
public void ReadTitlesFromFile(Context context){
    try {
        FileInputStream inStream = context.openFileInput("titlesfile.txt");
        InputStreamReader inReader = new InputStreamReader(inStream);
        BufferedReader bufferedReader = new BufferedReader(inReader);
        StringBuilder sBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null){
            sBuilder.append(line).append("\n");
        }
    }
    catch (FileNotFoundException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
}

// Read itemsfile.txt
public void ReadItemsFromFile(Context context){
    try {
        FileInputStream inStream = context.openFileInput("itemsfile.txt");
        InputStreamReader inReader = new InputStreamReader(inStream);
        BufferedReader bufferedReader = new BufferedReader(inReader);
        StringBuilder sBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null){
            sBuilder.append(line).append("\n");
        }
    }
    catch (FileNotFoundException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
}

}

And this is the MainActivity class:

public class MainActivity extends AppCompatActivity {

ListView showSavedFilesListview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize listview
    showSavedFilesListview = (ListView) findViewById(R.id.ListView1);

    // Read titles from titlesfile.txt
    public class ReadTitles {
        public static void main (Context context) {
            ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
            tmp.ReadTitlesFromFile(Context context);
        }
    }

}

I'm sorry if this is a stupid question, or if it has already been asked before, but I'm new to programming and all the info tends to confuse me.

Upvotes: 1

Views: 873

Answers (2)

Vedant Agarwala
Vedant Agarwala

Reputation: 18819

You have placed a class inside a method. That's why you are getting the error: "Modifier 'public' not allowed here"

You don't need the class ReadTitles anyway; and even if you do use it, remove the the static modifier from the main method.

Simply, instead of this:

// Read titles from titlesfile.txt
public class ReadTitles {
    public static void main (Context context) {
        ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
        tmp.ReadTitlesFromFile(Context context);
    }
}

put this:

// Read titles from titlesfile.txt
ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
tmp.ReadTitlesFromFile(this); // use the Activity context

Upvotes: 1

TEK
TEK

Reputation: 1265

As previously mentioned, you are attempting to declare a class of type ReadTitles within the onCreate method. You cannot do this.

Instead of:

public class ReadTitles {
    public static void main (Context context) {
        ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
        tmp.ReadTitlesFromFile(Context context);
    }
}

You can simply call your Singletons instance within onCreate. However, you need to obtain the application Context before you can call your ReadTitlesFromFile method. Presently, you're attempting to explicitly declare the type when passing the parameter to the ReadTitlesFromFile. You do not do this, as it will result in a compiler error. Instead, use the getApplicationContext() method to obtain the Context. See below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize listview
    showSavedFilesListview = (ListView) findViewById(R.id.ListView1);

    // Read titles from titlesfile.txt
    ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
    Context context = getApplicationContext();
    tmp.ReadTitlesFromFile(context);
}

Upvotes: 0

Related Questions