شرح تحويل list الى gson وتخزينها داخل ال sharedPreferences في اندرويد ستوديو
هل جربت في يوم من الايام ان تقوم بتخزين ملف gson كامل في الsharedPreferences في البداية سوف تجد ان الامر صعب لان الsharedPreferences يقوم على تخزين عنصر واحد فقط ولكن ماذا لو قمت بتحويل جميع النصوص الخاصه بك الى عنصر واحد وتخزينها باستخدام sharedPreferences هل سيحدث مشكلة ؟ بالطبع لا لانه سوف يعتبر ان جميع العناصر عباره عن عنصر واحد وبذلك سوف يقوم بتخزينها بدون اي مشاكل وهذا ما سوف نعمل عليه اننا سوف نحول list الى gson وبعدها سوف نقوم بتخزينها بداخل الsharedPreferences .
لأن Android هو أكثر أنظمة تشغيل الهواتف الذكية شيوعًا ، ولأن نشر تطبيق على متجر Google Play أمر بسيط ، فهو أفضل مكان للبدء ، خاصة وأن المنافسة شرسة ، والفرص كثيرة ، وتنوع الأجهزة التي تدعم هذا النظام يجعله الخيار الأفضل. بالإضافة إلى حقيقة أن الاستفادة من تطبيقات Android أمر بسيط للغاية ، ما عليك سوى تطوير تطبيق يوفر خدمة أو لعبة وتسويقها لتحقيق النجاح ، وهذه هي أبسط الطرق ، فمعظم الشركات ، سواء كانت كبيرة أو غيورة ، تميل للدخول في مجال تطبيقات الهواتف الذكية والمنصة الأولى ، وهذه هي أبسط الطرق ، بالإضافة إلى توجه معظم الشركات سواء كانت كبيرة أو غيورة لدخول مجال تطبيقات الهواتف الذكية.
اضافة مكتبة gson
في البداية عليك بتفعيل المكتبة التاليه لديك في الكود .
dependencies {
implementation 'com.google.code.gson:gson:2.8.9'
}
تحويل الlist الى gson وتخزينها بداخل الsharedPreferences
الان ما سوف نقوم به اننا سوف ندخل اسم وعنوان وبعدها نضيف المحتوى الخاص بهم الى list وفي النهايه سوف نقوم بحفظ الList , مبدئيا سوف نقوم بعمل ميثود باسم saveData ومن خلال هذه الميثود سوف يتم تعريف الsharedPreferences ويعدها يتم تعريف Gson وبعدها نقوم بتحويل الList الى gson وفي النهايه تقوم بتخزينها , والميثود LoadData يتم من خلالها استدعاء الlist من الsharedPreferences وتحويلها الى list لكي تظهر معنا كما هو موضح وهذه هيا فكرة الكود .
main_activity.java
public class TestActivity extends AppCompatActivity {
ActivityTestBinding binding ;
AdapterTest adapterTest;
ArrayList<DataTest> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_test);
loadData();
adapterTest = new AdapterTest(list);
binding.recycler.setLayoutManager(new LinearLayoutManager(this));
binding.recycler.setAdapter(adapterTest);
setInsertButton();
binding.save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveData();
}
});
}
private void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences("data" , MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString("list" , json);
editor.apply();
}
private void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences("data" , MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("list" , null);
Type type = new TypeToken<ArrayList<DataTest>>() {}.getType();
list = gson.fromJson(json , type);
if (list == null) {
list = new ArrayList<>();
}
}
private void setInsertButton() {
binding.insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertItem(binding.title.getText().toString(), binding.body.getText().toString());
}
});
}
private void insertItem(String line1, String line2) {
list.add(new DataTest(line1, line2));
adapterTest.notifyItemInserted(list.size());
}
}
تصميم صفحة activity main
main_activity.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/card"
android:layout_width="match_parent"
android:layout_height="0dp" />
<androidx.cardview.widget.CardView
app:cardCornerRadius="15dp"
app:cardElevation="20dp"
android:id="@+id/card"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<androidx.constraintlayout.widget.ConstraintLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/title"
android:hint="title"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/white"
android:textColorHint="@color/dark"
android:backgroundTint="@color/purple_500"
app:layout_constraintBottom_toTopOf="@id/body"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="220dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/body"
android:hint="body"
android:textColor="@color/white"
android:textColorHint="@color/dark"
android:backgroundTint="@color/purple_500"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="220dp"
android:layout_height="wrap_content" />
<com.google.android.material.button.MaterialButton
android:id="@+id/save"
android:layout_marginEnd="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/insert"
android:text="save"
android:layout_marginStart="10dp"
android:backgroundTint="@color/primary_red"
app:layout_constraintStart_toEndOf="@id/body"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<com.google.android.material.button.MaterialButton
android:id="@+id/insert"
android:layout_marginEnd="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="insert"
android:layout_marginStart="10dp"
app:layout_constraintStart_toEndOf="@id/body"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
لمزيد من الشروحات
- تصميم صفحة setting باستخدام android studio بإستخدام xml
- اضافة زر للتبديل بين الى الليلي في اندرويد ستوديو | Dark and white mode android
- شرح كيفية عمل ImageSlide بعدة اشكال وبدون مكتبات في الاندرويد ستديو
- تصميم برنامج باستخدام برنامج اندرويد ستوديو لارسال الاشعارات في وقت محدد
- نقل البيانات والصور من صفحة للاخرى اثناء الصغط على الrecyclerview
- شرح كيفية نقل بيانات الcolors من api من activity الى اي مكان اخر Android studio