2
2
3
3
import android .content .Context ;
4
4
import android .os .Bundle ;
5
+ import android .util .Log ;
5
6
import android .view .LayoutInflater ;
6
7
import android .view .View ;
7
8
import android .view .ViewGroup ;
20
21
import java .util .ArrayList ;
21
22
22
23
import com .example .menumap .R ;
24
+ import com .google .android .gms .tasks .OnCompleteListener ;
25
+ import com .google .android .gms .tasks .OnFailureListener ;
26
+ import com .google .android .gms .tasks .Task ;
27
+ import com .google .firebase .auth .FirebaseAuth ;
28
+ import com .google .firebase .auth .FirebaseUser ;
29
+ import com .google .firebase .firestore .DocumentReference ;
30
+ import com .google .firebase .firestore .DocumentSnapshot ;
31
+ import com .google .firebase .firestore .FirebaseFirestore ;
32
+ import com .google .firebase .firestore .QueryDocumentSnapshot ;
33
+ import com .google .firebase .firestore .QuerySnapshot ;
23
34
24
35
import java .util .List ;
25
36
26
37
public class HomeFragment extends Fragment {
38
+ private static final String SOURCE_TEXT = "sourceText" ;
39
+ private static final String SOURCE_LANGUAGE = "sourceLang" ;
40
+ private static final String RESULT_TEXT = "resultText" ;
41
+ private static final String RESULT_LANGUAGE = "resultLang" ;
42
+ private static final String COLLECTION_PATH = "translations" ;
43
+
44
+ private FirebaseFirestore mDB ;
45
+ private DocumentSnapshot mTranslationsDB ;
46
+ ListView listView ;
27
47
28
- private HomeViewModel homeViewModel ;
29
48
30
- ListView listView ;
31
49
32
50
ArrayList <String > foodList ;
33
51
ArrayAdapter <String > arrayAdapter ;
34
52
35
53
public View onCreateView (@ NonNull LayoutInflater inflater ,
36
54
ViewGroup container , Bundle savedInstanceState ) {
37
- homeViewModel =
38
- ViewModelProviders .of (this ).get (HomeViewModel .class );
55
+
39
56
View root = inflater .inflate (R .layout .fragment_home , container , false );
57
+ mDB = FirebaseFirestore .getInstance ();
40
58
41
59
listView = root .findViewById (R .id .ListView );
42
60
43
61
foodList = new ArrayList <>();
44
- foodList .add ("Apple" );
45
- foodList .add ("Salad" );
46
- foodList .add ("Junk food" );
47
- foodList .add ("Burrito" );
48
62
49
63
arrayAdapter = new ArrayAdapter <>(getActivity (), android .R .layout .simple_list_item_1 , foodList );
50
64
@@ -56,9 +70,40 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
56
70
String footName = foodList .get (i );
57
71
}
58
72
});
59
-
73
+ makeListFromDB ();
60
74
return root ;
61
75
}
62
76
77
+ private void makeListFromDB () {
78
+
79
+ mDB .collection (COLLECTION_PATH ).get ().addOnCompleteListener (new OnCompleteListener <QuerySnapshot >() {
80
+ @ Override
81
+ public void onComplete (@ NonNull Task <QuerySnapshot > task ) {
82
+ if (task .isSuccessful ()) {
83
+ for (QueryDocumentSnapshot document : task .getResult ()) {
84
+ StringBuilder fields = new StringBuilder ("" );
85
+ fields .append ("Original Text: " ).append (document .getString (SOURCE_TEXT ));
86
+ fields .append ("\n Translation: " ).append (document .getString (RESULT_TEXT ));
87
+ fields .append ("\n Original Language: " ).append (document .getString (SOURCE_LANGUAGE ));
88
+ fields .append ("\n Translated to: " ).append (document .getString (RESULT_LANGUAGE ));
89
+ arrayAdapter .add (fields .toString ());
90
+
91
+
92
+ }
93
+ } else {
94
+ Log .d ("collection" , "Error getting documents: " , task .getException ());
95
+ }
96
+ }
97
+ })
98
+ .addOnFailureListener (new OnFailureListener () {
99
+ @ Override
100
+ public void onFailure (@ NonNull Exception e ) {
101
+ Log .d ("collection" , "Error getting documents: " );
102
+ }
103
+ });
104
+
105
+
106
+ }
107
+
63
108
64
109
}
0 commit comments