0

I am a true beginner to programming, so forgive me.

I have a String Array filled with a set of quotes that I have a method randomly picking one to display on the screen. This all works perfectly, I'd like to take the next step now. I would like to have the ability to add text to this array that a user inputs on an Activity that I have created. I understand that Arrays are Immutable, but I am failing to figure out how to create an ArrayList, pre-fill it with my 50ish quotes and then have the ability to add more through the app later.

Here is the code I currently have...

public class FactBook {
public String[] mFacts = {
            "Quote 1.",
            "Quote 2.", };

public String getFact() {

    String fact = "";
    Random randomGenerator = new Random();
    int randomNumber = randomGenerator.nextInt(mFacts.length);
    fact = mFacts[randomNumber];

    return fact;

    }
4
  • Use ArrayList. Commented Oct 25, 2016 at 20:46
  • @AdrianColomitchi but I am failing to figure out how to create an ArrayList read the question. Commented Oct 25, 2016 at 22:31
  • @SomePerson I hope that my answer provided enough explanation for that (about 2 hours before you comment). Commented Oct 26, 2016 at 4:00
  • @AdrianColomitchi I didn't see it because I was in Triage :P Commented Oct 26, 2016 at 12:24

3 Answers 3

1

References

ArrayList

Arrays

import java.util.ArrayList;
import java.util.Arrays;

public class FactBook {
    // Public data members are not recommended.
    // Make it at least protected and arrange controlled access to it
    // by specific methods
    public ArrayList<String> mFacts = 
       new ArrayList<String>(
                Arrays.asList("Quote 1.", "Quote 2.")
       )
    };

    public String getFact() {

        String fact = "";
        // Do you need to create a new Random every time?
        // Perhaps creating it only once and storing it in a static
        // (class wide) data member will be just as good: create it once,
        // reuse it later.
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(mFacts.size());
        fact = mFacts.get(randomNumber);

        return fact;

    }

    // how to add
    public void add(String newQuip) {
        // Don't accept null or "all-white character" quotes
        if(null!=newQuip && newQuip.trim().length()>0) {
            this.mFacts.add(newQuip);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well that was amazing. Thank you. That worked like a charm. Now, I finally understand ArrayLists...Running into a new problem now though. When I click the Save button on my EditText Activity, it doesn't update my ArrayList stored on another Activity's Class...it doesn't seem to do anything, I think my method is flawed.
@ChazzRomeo "I think my method is flawed." Well, best to open another question with it (as opposed to editing this one).
0

Look at Arrays class.

It has a helper method exactly for those cases:

List<String> facts = Arrays.asList("string1", "string2", ...);

Comments

0

Here's a simple method that pre-populates an ArrayList with your values, and then allows you to add more values to it later on

private ArrayList<String> createPrePopulatedList() {
    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("Quote 1.");
    ///Add any more you want to prepopulate like this
    return arrayList;
}

You can call this method like so.

private ArrayList<String> myArrayList = createPrepopulatedList();

Now you can simply add whatever you want to it dynamically with add().

You should probably do some reading on Java data structures before you jump into Android programming. Here's some help with ArrayList https://www.tutorialspoint.com/java/java_arraylist_class.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.