-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathQuestions.java
More file actions
executable file
·90 lines (68 loc) · 3 KB
/
Questions.java
File metadata and controls
executable file
·90 lines (68 loc) · 3 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
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.gdgsrilanka.io.subpages;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import org.gdgsrilanka.io.R;
import org.gdgsrilanka.io.subpages.questions.Question;
import java.util.HashMap;
import java.util.Map;
/**
* A simple {@link Fragment} subclass.
*/
public class Questions extends Fragment {
public Questions() {
// Required empty public constructor
}
ImageView bg;
EditText question;
Button submit;
FirebaseDatabase database;
DatabaseReference myRef;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.fragment_questions, container, false);
question = v.findViewById(R.id.question);
submit = v.findViewById(R.id.qsubmit);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("messages");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(question.getText().toString().trim().length() == 0){
Toast.makeText(getContext(), getString(R.string.toast_enter_question), Toast.LENGTH_SHORT).show();
} else {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is signed in
String email = user.getEmail();
String name = user.getDisplayName();
Long tsLong = System.currentTimeMillis()/1000;
String timestamp = tsLong.toString();
String prashne = question.getText().toString();
String key = myRef.child("messages").push().getKey();
Question questiono = new Question(name, email, prashne, timestamp);
Map<String, Object> postValues = questiono.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(key, postValues);
myRef.updateChildren(childUpdates);
Toast.makeText(getContext(),getString(R.string.toast_question_posted),Toast.LENGTH_SHORT).show();
question.setText("");
}
}
}
});
return v;
}
}