Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enter code here
- private Intent takePictureIntent;
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference myRef;
- private String pathFotoTemporal;
- private static final int REQUEST_TAKE_PHOTO = 1;
- private View view;
- private String key;
- //Expandable list Adapter
- ExpandableListAdapter listAdapter;
- ExpandableListView expListView;
- List<String> listDataHeader;
- HashMap<String, List<String>> listDataChild;
- //Users
- private List <User> items = new ArrayList<>();
- //Obejeto a pasar
- User user = new User();
- String password;
- //Botones
- private EditText TextPass;
- private EditText TextDNI;
- private Button fichar;
- // Adding child data
- List<String> top250 = new ArrayList<String>();
- @Override
- public View onCreateView(LayoutInflater inflater, final ViewGroup container,
- Bundle savedInstanceState) {
- view = inflater.inflate(R.layout.fragment_main, container, false);
- LeerFirebase();
- //Botones
- TextPass = (EditText) view.findViewById(R.id.IdpasswordUser);
- //TextDNI = (EditText) view.findViewById(R.id.DNI);
- // get the listview
- expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
- // preparing list data
- prepareListData();
- listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
- //expListView.setAdapter(listAdapter);
- // setting list adapter
- expListView.setAdapter(listAdapter);
- //Write in database mesage myRef.setValue("Hello, World!");
- //Llamamos al boton fichar
- fichar = (Button) view.findViewById(R.id.fichar);
- fichar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- key = TextDNI.getText().toString();
- password = TextPass.getText().toString();
- //SuperUsuario
- if(key.equalsIgnoreCase("cesi.tic")&&password.equalsIgnoreCase("123456789")){
- ((MainActivity) getActivity()).EditarUsuario();
- fichar.setVisibility(view.INVISIBLE);
- TextPass.setVisibility(view.INVISIBLE);
- TextDNI.setVisibility(view.INVISIBLE);
- }else{
- LeerFirebase();
- }
- //Para pasar de fragment hay que comprobar el dni y el password
- /*((MainActivity) getActivity()).AbrirFichar();
- fichar.setVisibility(view.INVISIBLE);
- TextPass.setVisibility(view.INVISIBLE);*/
- }
- });
- return view;
- }
- private void LeerFirebase() {
- myRef = database.getReference("Usuario" +"_"+ key+"/password");
- //+"/password"
- // Read from the database
- myRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- String value = dataSnapshot.getValue(String.class);
- password = TextPass.getText().toString();
- if(password.equalsIgnoreCase(value)){
- //Pasamos por pantalla
- Toast.makeText(getActivity(),"El usuario introducido es correcto",Toast.LENGTH_SHORT).show();
- //Pasamos al otro Fragment
- ((MainActivity) getActivity()).AbrirFichar(TextDNI.getText().toString());
- fichar.setVisibility(view.INVISIBLE);
- TextPass.setVisibility(view.INVISIBLE);
- TextDNI.setVisibility(view.INVISIBLE);
- }else {
- Toast.makeText(getActivity(),"El usuario introducido o la Contraseña no son correctos ",Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(DatabaseError error) {
- Toast.makeText(getActivity(),"Hay un error de lectura ",Toast.LENGTH_SHORT).show();
- }
- });
- }
- //Al iniciarse crea el boton Visible
- @Override
- public void onStart() {
- final Button fichar = (Button) getView().findViewById(R.id.fichar);
- EditText getpassword = (EditText) getView().findViewById(R.id.IdpasswordUser);
- fichar.setVisibility(getView().VISIBLE);
- getpassword.setVisibility(getView().VISIBLE);
- super.onStart();
- }
- private File createImageFile() throws IOException {
- // Create an image file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- String imageFileName = "JPEG_" + timeStamp + "_";
- File storageDir = Environment.getExternalStoragePublicDirectory(
- Environment.DIRECTORY_PICTURES);
- File image = File.createTempFile(
- imageFileName, /* prefix */
- ".jpg", /* suffix */
- storageDir /* directory */
- );
- // Save a file: path for use with ACTION_VIEW intents
- pathFotoTemporal = "file:" + image.getAbsolutePath();
- return image;
- }
- private void dispatchTakePictureIntent() {
- takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- // Ensure that there's a camera activity to handle the intent
- if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
- // Create the File where the photo should go
- File photoFile = null;
- try {
- photoFile = createImageFile();
- } catch (IOException ex) {
- // Error occurred while creating the File
- ex.printStackTrace();
- }
- // Continue only if the File was successfully created
- if (photoFile != null) {
- takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
- Uri.fromFile(photoFile));
- startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
- }
- }
- }
- public void onActivityResult(int requestCode, int resultCode, Intent intent) {
- super.onActivityResult(requestCode, resultCode, intent);
- try {
- if (requestCode == REQUEST_TAKE_PHOTO) {
- if (resultCode == RESULT_OK) {
- //Glide.with(getContext()).load(pathFotoTemporal).into(imageView);
- //Falta guardar la Url en la base de datos.
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //List Adapter
- private void prepareListData() {
- listDataHeader = new ArrayList<String>();
- listDataChild = new HashMap<String, List<String>>();
- // Adding child data
- listDataHeader.add("User Name");
- //Firebase carga
- DatabaseReference ListaRef = database.getReference();
- // Read from the database
- ListaRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- // This method is called once with the initial value and again
- // whenever data at this location is updated.
- items.clear();
- User usuarios;
- for( DataSnapshot i : dataSnapshot.getChildren()) {
- usuarios = i.getValue(User.class);
- items.add(usuarios);
- top250.add(" " + usuarios.getName() + " " + usuarios.getLastname());
- }
- Toast.makeText(getContext(), "Se han bajado " + items.size() + " Usuario", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onCancelled(DatabaseError error) {
- Toast.makeText(getContext(), "Error descargando las recetas", Toast.LENGTH_SHORT).show();
- }
- });
- List<String> top = new ArrayList<String>();
- for(int i = 0 ; i < top250.size(); i++){
- if (top250.get(i).isEmpty()||top250.equals(null)||top250.get(i).equals(null)||top250.get(i).equalsIgnoreCase("null null")){
- System.out.println("Noooo");
- }else{
- top.add(top250.get(i));
- }
- }
- listDataChild.put(listDataHeader.get(0), top); // Header, Child data
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement