Skip to content

Instantly share code, notes, and snippets.

//ONE
var listaObjetos = mutableListOf("uno", "dos", "tres")
var adaptador = ArrayAdapter(this, R.layout.prueba,R.id.textView, listaObjetos)
spinner.adapter = adaptador
//TWO
spinner.adapter = ArrayAdapter.createFromResource(this@MainActivity, R.array.arreglo, android.R.layout.simple_spinner_item)
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View,
@dandvl
dandvl / SealedClass.kt
Last active November 26, 2019 03:54
SealClass
//All these 4 classes MUST be in the same file
sealed class Product
data class AlphaProduct (var name: String, var description:String) : Product()
data class BetaProduct (var name: String, var description:String) : Product()
data class GameProduct (var name: String, var description:String) : Product()
var one = AlphaProduct("alpha", "one")
var two = BetaProduct("beta", "two")
/**
JAVA arguments are always pass by value (that means a copy of it)- either the arg is primitive value or an object’s reference like “object@1f89ab83” in the heap memory.
Therefore we can change the state of the object passed but not its reference, and if we create another reference with the keyboard “new”.
**/
public class JavaPassByValue{
public static void main(String[] args) {
Person person = new Person("Donatelo");
Person person2 = new Person("Leo");
@dandvl
dandvl / SOLID.md
Last active December 30, 2020 19:29

Single Responsibility Principle (SRP)

A Class shouldn't have more than one Responsibility
Violation - when the POJO class saves into the database and generates reports)

Open Closed Principle (OCP)

Open for extension, Closed for modification. Every time we add sibling classes, we shouldn't modify the existing methods that are working with them.
Violation - switch statements in the methods to call different behaviors for each sibling class, instead of using polymorphism

Liskov Substitution Principle (LSP)

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. Violation - when you pass a subclass to a method and obtain a different result when you pass the class

Interface Segregation Principle (ISP)

import androidx.lifecycle.Observer
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {
@Suppress("MemberVisibilityCanBePrivate")
var hasBeenHandled = false
private set // Allow external read but not write
@dandvl
dandvl / uiTestSample.kt
Created July 10, 2020 17:09
UI Test Sample
package com.example.borrador2
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.rule.ActivityTestRule
import org.junit.Rule
@dandvl
dandvl / NearbyWords.java
Created November 17, 2020 03:56 — forked from alexrios/NearbyWords.java
Facebook's How to Crush Your Coding Interview. Problem: Write a function that given a string, returns all nearby words. You are given the following helper functions: Set<String> getNearbyChars(char c); isWord(String word);
package facebook;
import java.util.HashSet;
import java.util.Set;
/**
* The complexity of that algorithm is O(n^m), where:
* n - the length of a string.
* m - number of permutations.
*/
@dandvl
dandvl / clean_code.md
Created December 9, 2020 03:48 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

class MyViewModel {
val result = liveData {
emit(doComputation)
}
}
//implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.google.android.material.snackbar.Snackbar
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)