-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity1.java
183 lines (141 loc) · 6.47 KB
/
MainActivity1.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.example.teamfinding;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
//import package.InfoSet;
public class MainActivity1 extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_TOPIC = "topic";
private EditText editTextName;
private EditText editTextEmail;
private TextView textViewData;
private EditText editTextTopic;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private DocumentReference infoRef = db.collection("Data").document("Data set");
private CollectionReference allInfoRef = db.collection("Data");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.edit_text_title);
editTextEmail = findViewById(R.id.edit_text_email);
textViewData = findViewById(R.id.text_view_data);
editTextTopic = findViewById(R.id.edit_text_topic);
}
protected void onStart() {
super.onStart();
allInfoRef.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(e!=null) {
return;
}
String data = "";
for(QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots) {
InfoSet info = documentSnapshot.toObject(InfoSet.class);
//info.setDocumentId(documentSnapshot.getId());
//String documentID = info.getDocumentId();
String title = info.getTitle();
String email = info.getEmail();
String topic = info.getTopic();
data+= "Title" + title + "\nEmail: " + email + "\nTopic" + topic;
}
textViewData.setText(data);
}
});
}
public void updateDescription(View v) {
String topic = editTextTopic.getText().toString();
infoRef.update(KEY_TOPIC, topic);
}
public void addInfo(View v) {
String title = editTextName.getText().toString();
String email = editTextEmail.getText().toString();
String topic = editTextTopic.getText().toString();
InfoSet info = new InfoSet(title, email, topic);
// Map<String, Object> info = new HashMap<>();
// info.put(KEY_NAME, title);
// info.put(KEY_AGE, age);
//db.document("Data/First Data");
allInfoRef.add(info);
infoRef.set(info).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity1.this, "Info saved", Toast.LENGTH_SHORT).show();
}
} )
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity1.this, "Error",Toast.LENGTH_SHORT).show();
Log.d(TAG, e.toString());
}
});
}
public void loadSingleInfo(View v) {
infoRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()) {
String title = documentSnapshot.getString(KEY_NAME);
String email = documentSnapshot.getString(KEY_EMAIL);
String topic = documentSnapshot.getString(KEY_TOPIC);
textViewData.setText("Name" + title + "\n" + "Email" + email + "\nTopic" + topic);
} else {
Toast.makeText(MainActivity1.this, "Document does not exist", Toast.LENGTH_SHORT).show();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity1.this, "Error", Toast.LENGTH_SHORT).show();
Log.d(TAG, e.toString());
}
});
}
public void loadInfo(View v) {
allInfoRef.whereEqualTo("Topic", editTextTopic).orderBy("Topic", Query.Direction.ASCENDING)
.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
//Map<String, Object> info = new HashMap<>();
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String data = "";
for(QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots) {
InfoSet info = documentSnapshot.toObject(InfoSet.class);
//info.setDocumentId(documentSnapshot.getId());
// String documentID = info.getDocumentId();
String title = info.getTitle();
String email = info.getEmail();
String topic = info.getTopic();
data+= "Name" + title + "\nEmail: " + email + "\nTopic" + topic;
}
textViewData.setText(data);
}
});
}
}