0

I am not sure if I should use two dimensional array or something different. I have a word, for example apple , and I have two definitions for this word - ex. I like eating the apple and An apple is red.

And I want to display this word either with first sentence or second sentence.

Simply, I want to link this word with two definitions and display it easily. How can I do that?

1
  • 2D array sounds like it should work. You can always find alternatives, of course (such as Map<String, List<String>>). Commented Dec 20, 2016 at 22:11

3 Answers 3

1

In xml:

<Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Sentence" />

In JAVA:

private View view;
     private HashMap<String, ArrayList<String>> words;
            private Boolean isClicked = false;
    @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.your_layout, container, false);
            initView();
            return view;
        }

        private void initView() {
    final Button button1 = (Button) view.findViewById(R.id.button1);
    button1.setOnClickListener(this);
    words = new HashMap<>();
            final ArrayList<String> appleDefs = new ArrayList<>();

            appleDefs.add("def one");
            appleDefs.add("def two");

            words.put("Apple", appleDefs);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
     case R.id.button1:
                    clickCheck();
                    break;
    }
    }
 private void clickCheck() {
        if (isClicked) {
            if (words.get("Apple").size() > 0) {
                Log.d(TAG, words.get("Apple").get(1));
            }
            isClicked = false;

        } else {
            if (words.get("Apple").size() > 0) {
                Log.d(TAG, words.get("Apple").get(0));
            }
            isClicked = true;
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

That was really usefull! Thank you friend :)
1

Use a Map of Lists, its easier to navigate, e.g. Map<String, List<String>> words = new HashMap<>();

Usage:

List<String> appleDefs = new ArrayList<>();

appleDefs.add("def one");
appleDefs.add("def two");

words.put("Apple", appleDefs);

3 Comments

It seems to be good. How can I get values from this map of lists?? For ex. if i click button, i get first definition, and if I click button again, I get second definition?
Well, one approach would be to remove(index)the displayed definition from the List instead of simply get(index)-ing it. Or add the displayed definitions to another arbitrary list and before displaying, make sure the current one to be displayed is not in the displayed collection. There are infinite ways to go about that, just pick one and run
Thank you for your effort. I think I got it.
1

If you're only ever going to have 2 sentences you could create a class to store the info e.g

public class WordObj {
    private String word;
    private String sentence1, sentence2;


    public WordObj(String word, String definition1, String definition2) {
        this.word = word;
        this.sentence1 = definition1;
        this.sentence2 = definition2;
    }

    public String getWord() {
        return word;
    }

    public String getSentence1() {
        return sentence1;
    }

    public String getSentence2() {
        return sentence2;
    }

}

Then use it like:

ArrayList<WordObj> words = new ArrayList<>();

words.add(new WordObj("Apple", "I like eating the apple", "An apple is red"));

EDIT: For a variable amount of sentences:

public class WordObj {
    private String word;
    private ArrayList<String> sentences;

    public WordObj(String word, ArrayList<String> sentences) {
        this.word = word;
        this.sentences = sentences;
    }

    public String getWord() {
        return word;
    }

    public ArrayList<String> getSentences() {
        return sentences;
    }

    public String getSentence(int position) {
        return sentences.get(position);
    }
}

Usage:

ArrayList<WordObj> words = new ArrayList<>();

ArrayList<String> appleSentences = new ArrayList<>();
appleSentences.add("I like eating the apple");
appleSentences.add("An apple is red");

words.add(new WordObj("Apple", appleSentences));

1 Comment

I sometimes have more than 2 sentences. Thank you for your answer tho.

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.