Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. enter code here
  2.  
  3. private Intent takePictureIntent;
  4. FirebaseDatabase database = FirebaseDatabase.getInstance();
  5. DatabaseReference myRef;
  6. private String pathFotoTemporal;
  7. private static final int REQUEST_TAKE_PHOTO = 1;
  8. private View view;
  9. private String key;
  10.  
  11. //Expandable list Adapter
  12. ExpandableListAdapter listAdapter;
  13. ExpandableListView expListView;
  14. List<String> listDataHeader;
  15. HashMap<String, List<String>> listDataChild;
  16.  
  17. //Users
  18. private List <User> items = new ArrayList<>();
  19.  
  20. //Obejeto a pasar
  21. User user = new User();
  22. String password;
  23.  
  24.  
  25. //Botones
  26. private EditText TextPass;
  27. private EditText TextDNI;
  28. private Button fichar;
  29.  
  30. // Adding child data
  31. List<String> top250 = new ArrayList<String>();
  32.  
  33.  
  34. @Override
  35. public View onCreateView(LayoutInflater inflater, final ViewGroup container,
  36. Bundle savedInstanceState) {
  37. view = inflater.inflate(R.layout.fragment_main, container, false);
  38. LeerFirebase();
  39.  
  40. //Botones
  41. TextPass = (EditText) view.findViewById(R.id.IdpasswordUser);
  42. //TextDNI = (EditText) view.findViewById(R.id.DNI);
  43.  
  44. // get the listview
  45. expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
  46.  
  47. // preparing list data
  48. prepareListData();
  49.  
  50. listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
  51. //expListView.setAdapter(listAdapter);
  52.  
  53. // setting list adapter
  54. expListView.setAdapter(listAdapter);
  55.  
  56. //Write in database mesage myRef.setValue("Hello, World!");
  57.  
  58. //Llamamos al boton fichar
  59. fichar = (Button) view.findViewById(R.id.fichar);
  60. fichar.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. key = TextDNI.getText().toString();
  64. password = TextPass.getText().toString();
  65. //SuperUsuario
  66. if(key.equalsIgnoreCase("cesi.tic")&&password.equalsIgnoreCase("123456789")){
  67. ((MainActivity) getActivity()).EditarUsuario();
  68. fichar.setVisibility(view.INVISIBLE);
  69. TextPass.setVisibility(view.INVISIBLE);
  70. TextDNI.setVisibility(view.INVISIBLE);
  71. }else{
  72. LeerFirebase();
  73. }
  74.  
  75. //Para pasar de fragment hay que comprobar el dni y el password
  76. /*((MainActivity) getActivity()).AbrirFichar();
  77. fichar.setVisibility(view.INVISIBLE);
  78. TextPass.setVisibility(view.INVISIBLE);*/
  79.  
  80. }
  81. });
  82. return view;
  83. }
  84.  
  85. private void LeerFirebase() {
  86. myRef = database.getReference("Usuario" +"_"+ key+"/password");
  87. //+"/password"
  88. // Read from the database
  89. myRef.addValueEventListener(new ValueEventListener() {
  90. @Override
  91. public void onDataChange(DataSnapshot dataSnapshot) {
  92. String value = dataSnapshot.getValue(String.class);
  93. password = TextPass.getText().toString();
  94. if(password.equalsIgnoreCase(value)){
  95. //Pasamos por pantalla
  96. Toast.makeText(getActivity(),"El usuario introducido es correcto",Toast.LENGTH_SHORT).show();
  97. //Pasamos al otro Fragment
  98. ((MainActivity) getActivity()).AbrirFichar(TextDNI.getText().toString());
  99. fichar.setVisibility(view.INVISIBLE);
  100. TextPass.setVisibility(view.INVISIBLE);
  101. TextDNI.setVisibility(view.INVISIBLE);
  102. }else {
  103. Toast.makeText(getActivity(),"El usuario introducido o la Contraseña no son correctos ",Toast.LENGTH_SHORT).show();
  104. }
  105. }
  106.  
  107. @Override
  108. public void onCancelled(DatabaseError error) {
  109. Toast.makeText(getActivity(),"Hay un error de lectura ",Toast.LENGTH_SHORT).show();
  110. }
  111. });
  112. }
  113.  
  114. //Al iniciarse crea el boton Visible
  115. @Override
  116. public void onStart() {
  117. final Button fichar = (Button) getView().findViewById(R.id.fichar);
  118. EditText getpassword = (EditText) getView().findViewById(R.id.IdpasswordUser);
  119. fichar.setVisibility(getView().VISIBLE);
  120. getpassword.setVisibility(getView().VISIBLE);
  121. super.onStart();
  122. }
  123.  
  124. private File createImageFile() throws IOException {
  125. // Create an image file name
  126. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  127. String imageFileName = "JPEG_" + timeStamp + "_";
  128. File storageDir = Environment.getExternalStoragePublicDirectory(
  129. Environment.DIRECTORY_PICTURES);
  130. File image = File.createTempFile(
  131. imageFileName, /* prefix */
  132. ".jpg", /* suffix */
  133. storageDir /* directory */
  134. );
  135.  
  136. // Save a file: path for use with ACTION_VIEW intents
  137. pathFotoTemporal = "file:" + image.getAbsolutePath();
  138. return image;
  139. }
  140.  
  141. private void dispatchTakePictureIntent() {
  142. takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  143. // Ensure that there's a camera activity to handle the intent
  144. if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
  145. // Create the File where the photo should go
  146. File photoFile = null;
  147. try {
  148. photoFile = createImageFile();
  149. } catch (IOException ex) {
  150. // Error occurred while creating the File
  151. ex.printStackTrace();
  152. }
  153. // Continue only if the File was successfully created
  154. if (photoFile != null) {
  155. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
  156. Uri.fromFile(photoFile));
  157. startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
  158. }
  159. }
  160. }
  161.  
  162.  
  163. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  164. super.onActivityResult(requestCode, resultCode, intent);
  165.  
  166. try {
  167.  
  168. if (requestCode == REQUEST_TAKE_PHOTO) {
  169. if (resultCode == RESULT_OK) {
  170. //Glide.with(getContext()).load(pathFotoTemporal).into(imageView);
  171. //Falta guardar la Url en la base de datos.
  172. }
  173. }
  174. } catch (Exception e) {
  175. e.printStackTrace();
  176.  
  177. }
  178. }
  179.  
  180.  
  181. //List Adapter
  182. private void prepareListData() {
  183. listDataHeader = new ArrayList<String>();
  184. listDataChild = new HashMap<String, List<String>>();
  185.  
  186. // Adding child data
  187. listDataHeader.add("User Name");
  188.  
  189. //Firebase carga
  190.  
  191. DatabaseReference ListaRef = database.getReference();
  192.  
  193. // Read from the database
  194. ListaRef.addValueEventListener(new ValueEventListener() {
  195. @Override
  196. public void onDataChange(DataSnapshot dataSnapshot) {
  197. // This method is called once with the initial value and again
  198. // whenever data at this location is updated.
  199.  
  200. items.clear();
  201. User usuarios;
  202.  
  203. for( DataSnapshot i : dataSnapshot.getChildren()) {
  204. usuarios = i.getValue(User.class);
  205. items.add(usuarios);
  206. top250.add(" " + usuarios.getName() + " " + usuarios.getLastname());
  207. }
  208. Toast.makeText(getContext(), "Se han bajado " + items.size() + " Usuario", Toast.LENGTH_SHORT).show();
  209. }
  210.  
  211. @Override
  212. public void onCancelled(DatabaseError error) {
  213. Toast.makeText(getContext(), "Error descargando las recetas", Toast.LENGTH_SHORT).show();
  214. }
  215. });
  216. List<String> top = new ArrayList<String>();
  217.  
  218. for(int i = 0 ; i < top250.size(); i++){
  219. if (top250.get(i).isEmpty()||top250.equals(null)||top250.get(i).equals(null)||top250.get(i).equalsIgnoreCase("null null")){
  220. System.out.println("Noooo");
  221. }else{
  222. top.add(top250.get(i));
  223. }
  224. }
  225. listDataChild.put(listDataHeader.get(0), top); // Header, Child data
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement