Hello dear forum users! I ask for your help! I have a fragment with RecyclerView
Code:
package com.starikov.tester; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class PizzaMaterialFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RecyclerView pizzaRecycler = (RecyclerView)inflater.inflate(R.layout.fragment_pizza_material, container, false); int[] pizzaImages = new int[Pizza.pizzas.length]; for (int i = 0; i < pizzaImages.length; i++){ pizzaImages[i] = Pizza.pizzas[i].getImageResourceId(); } CaptionedImagesAdapter adapter = new CaptionedImagesAdapter(pizzaImages); pizzaRecycler.setAdapter(adapter); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); pizzaRecycler.setLayoutManager(layoutManager); return pizzaRecycler; } } Markup:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pizza_recycler" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/> There is a Pizza class:
package com.starikov.tester; public class Pizza { private int imageResourceId; public static final Pizza[] pizzas = { new Pizza(R.drawable.formul_electricheskii_zaryad_ydra_atoma), new Pizza(R.drawable.formul_simvol_himicheskogo_elementa), new Pizza(R.drawable.formul_energia_svyzi_yadra), new Pizza(R.drawable.formul_massovoe_chislo), new Pizza(R.drawable.formul_defekt_mass) }; private Pizza(int imageResourceId) { this.imageResourceId = imageResourceId; } public int getImageResourceId() { return imageResourceId; } } There is a CaptionedImagesAdapter (Custom RecyclerView Adapter):
package com.starikov.tester; import android.graphics.drawable.Drawable; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import android.widget.ImageView; class CaptionedImagesAdapter extends RecyclerView.Adapter<CaptionedImagesAdapter.ViewHolder> { private int[] imageIds; public static class ViewHolder extends RecyclerView.ViewHolder{ private CardView cardView; public ViewHolder(CardView v){ super(v); cardView = v; } } public CaptionedImagesAdapter(int[] imageIds){ this.imageIds = imageIds; } @Override public CaptionedImagesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { CardView cv = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false); return new ViewHolder(cv); } @Override public void onBindViewHolder(ViewHolder holder, int position) { CardView cardView = holder.cardView; ImageView imageView = (ImageView) cardView.findViewById(R.id.info_image); Drawable drawable = cardView.getResources().getDrawable(imageIds[position]); imageView.setImageDrawable(drawable); } @Override public int getItemCount() { return imageIds.length; } } I need to redo it so that RecyclerView takes data from the database and not from a simple list in Pizza.java. I rummaged Google, but did not understand what was. I ask for your help!
Here is the database code:
package com.starikov.tester; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class PizzaDatabaseHelper extends SQLiteOpenHelper{ private static final String DB_NAME = "pizza"; private static final int DB_VERSION = 1; PizzaDatabaseHelper(Context context){ super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE FORMULS (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "IMAGE_RESOURCE_ID INTEGER);"); insertFormul(db, R.drawable.formul_electricheskii_zaryad_ydra_atoma); insertFormul(db, R.drawable.formul_simvol_himicheskogo_elementa); insertFormul(db, R.drawable.formul_energia_svyzi_yadra); insertFormul(db, R.drawable.formul_massovoe_chislo); insertFormul(db, R.drawable.formul_defekt_mass); } @Override public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { } private static void insertFormul(SQLiteDatabase db, int resourceId){ ContentValues formulValues = new ContentValues(); formulValues.put("IMAGE_RESOURCE_ID", resourceId); db.insert("FORMULS", null, formulValues); } } Please help! Give the full code, with an explanation, I am a beginner and I don’t understand everything right away! Thanks in advance!
Дейтел П., Дейтел Х., Уолд Э. - Android для разработчиков (Библиотека программиста) - 2016in theAddress Bookchapter, everything is explained in detail with explanations. - Evgen Orlovsky