Skip to content

Instantly share code, notes, and snippets.

View travisdachi's full-sized avatar
🖖
Live long and prosper

Travis P travisdachi

🖖
Live long and prosper
View GitHub Profile
krefson["someKey"] = someData //put
val data = krefson["someKey", defalutValue] //get with default value
val anotherData: SomeData? = krefson["someOtherKey"] //get nullable
if ("someKey" in krefson) //check if "someKey" is exist
krefson.remove("someKey") //simply remove a key
inline fun <reified T> Gson.fromJson(string: String?): T? {
return fromJson(string, object : TypeToken<T>() {}.type)
}
open class Krefson(context: Context, val name: String, val gson: Gson = Gson()) {
val sharedPreference: SharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)
inline operator fun <reified T> get(key: String): T? {
return gson.fromJson(sharedPreference.getString(key, null))
}
inline operator fun <reified T> get(key: String, defaultValue: T): T {
return gson.fromJson(sharedPreference.getString(key, null)) ?: defaultValue
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.example.travis.playground.MainActivity">
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonHolder> {
private List<Person> list;
public PersonAdapter(@NonNull List<Person> list) {
this.list = list;
}
@Override
public PersonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Person> people;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
public class Person {
public String name;
public int age;
public boolean isChecked;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonHolder> {
private List<Person> list;
public PersonAdapter(@NonNull List<Person> list) {
this.list = list;
}
@Override
public PersonHolder onCreateViewHolder(ViewGroup parent, int viewType) {