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

Community
  • 1
  • 1
Lucas
  • 71
  • 1
  • 2
  • 6

3 Answers3

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);
}
Lucas
  • 71
  • 1
  • 2
  • 6
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.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
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);
tster
  • 17,883
  • 5
  • 53
  • 72