1

I am working in a translator kind of app and i need some help. I have a class with getters and setters for my Array List objects. Each object has a phrase, a meaning, and usage.

so i have this to create my list:

ArrayList<PhraseCollection> IdiomsList = new ArrayList<PhraseCollection>();

now how do i add these objects to the list, each object containing the phrase, its meaning, and a use in a sentence?

For Example: The Layout would be something like this

Phrase

Kick the bucket

Meaning

When someone dies

Usage

My grandfather kicked the bucket

Thanks a lot

3 Answers 3

2

this is what i came up with that worked for me

private void loadIdioms() {

    //creating new items in the list
    Idiom i1 = new Idiom();
    i1.setPhrase("Kick the bucket");
    i1.setMeaning("When someone dies");
    i1.setUsage("My old dog kicked the bucket");
    idiomsList.add(i1);
}
Sign up to request clarification or add additional context in comments.

Comments

1

ArrayList has a method call add() or add(ELEMENT,INDEX); In order to add your objects you must first create them

PhraseCollection collection=new PhraseCollection();

then create the ArrayList by

ArrayList<PhraseCollection> list=new ArrayList<PhraseCollection>();

add them by :

list.add(collection);

Last if you want to render that in your ListView item, you must override the toString() in your PhraseCollection.

1 Comment

but how do i add the specific fields to my object
0

I suppose you would use the add(E) method (http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E)).

Here is an example using your example provided.

public class Phrase {
    public final String phrase, meaning, usage;
    //TODO: implement getters?

    public Phrase(String phrase, meaning, usage) {
        this.phrase = phrase;
        this.meaning = meaning;
        this.usage = usage;
    }
}

and use it like this:

// create the list
ArrayList<Phrase> idiomsList = new ArrayList<Phrase>();

// create the phrase to add
Phrase kick = new Phrase("kick the bucket", "When someone dies", "My grandfather kicked the bucket");

// add the phrase to the list
idiomsList.add(kick);

3 Comments

Generally prefer List<X> mylist = new ArrayList<X>(); See stackoverflow.com/questions/9852831/…
how do you retrieve say the meaning?
you could use: kick.meaning

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.