Reputation: 207
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?
Upvotes: 0
Views: 2847
Reputation: 731
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;
}
}
Upvotes: 1
Reputation: 1452
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));
Upvotes: 1
Reputation: 503
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);
Upvotes: 1