-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordAdapter.java
More file actions
78 lines (63 loc) · 2.98 KB
/
WordAdapter.java
File metadata and controls
78 lines (63 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.simpleapps22.dholuo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import static androidx.core.content.ContextCompat.getColor;
/**
* {@link WordAdapter} is an {@link ArrayAdapter} that can provide the layout for each list item
* based on a data source, which is a list of {@link Word} objects.
*/
public class WordAdapter extends ArrayAdapter<Word> {
private final int mColorResourceId;
/**
* Create a new {@link WordAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param words is the list of {@link Word}s to be displayed.
* @param colorResourceId is the resource ID for the background color for this list of words
*/
public WordAdapter(Context context, ArrayList<Word> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
/**
* @return a list item that displays a word at the given position.
*/
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID dholuo_text_view.
TextView dholuoTextView = listItemView.findViewById(R.id.dholuo_text_view);
// Get the Dholuo translation from the currentWord object and set this text on
// the Dholuo TextView.
dholuoTextView.setText(currentWord.getDholuoTranslation());
// Find the TextView in the list_item.xml layout with the ID default_text_view.
TextView defaultTextView = listItemView.findViewById(R.id.default_text_view);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
defaultTextView.setText(currentWord.getDefaultTranslation());
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
return listItemView;
}
}