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!

  • You need to use the CursorLoader - how to do this, you can read in the book of Дейтел П., Дейтел Х., Уолд Э. - Android для разработчиков (Библиотека программиста) - 2016 in the Address Book chapter, everything is explained in detail with explanations. - Evgen Orlovsky

1 answer 1

Change your class:

 public class Pizza { private int imageResourceId; private Pizza(int imageResourceId) { this.imageResourceId = imageResourceId; } public int getImageResourceId() { return imageResourceId; } 

Then, in the PizzaDatabaseHelper class PizzaDatabaseHelper implement a method for retrieving data from the database:

Write down the name of the database table, and the data to be received:

 private static final String mTableName = "FORMULS"; private static final int mImgId = "IMAGE_RESOURCE_ID"; 

After the insertFormul method insertFormul add code that will receive a list of data and pass it to where you specify:

 List<Pizza > getItem(){ SQLiteDatabase db = this.getReadableDatabase(); @SuppressLint("Recycle") Cursor cursor = db.query(mTableName, // a. table mImgId, null, null, null, null, null, null); List<Pizza> headsItem = new ArrayList<>(); if(cursor.moveToFirst()){ while(!cursor.isAfterLast()){ headsItem.add(new Pizza( cursor.getInteger(cursor.getColumnIndex(mImgId)))); cursor.moveToNext(); } } return headsItem; } 

And further in the onCreate method of your fragment:

 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(); } List<Pizza> namesItem = db.getItem(); LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity, LinearLayoutManager.VERTICAL, false); pizzaRecycler.setLayoutManager(mLinearLayoutManager); pizzaRecycler.setAdapter(new CaptionedImagesAdapter(namesItem)); return pizzaRecycler; 

This is a method using another library for reading data from a database, but in general, the mechanism is as follows, arrange a little and arrange it as necessary.

  • And further in the onCreate method: in which file? RecyclerView pizzaRecycler = (RecyclerView) findViewById (R.id.recyclerView); DBAssetHelper dbSetup = new DBAssetHelper (this); Produces an error - Mark Starikov
  • And another conflict occurs in the PizzaDatabaseHelper list is a List <Pizza> and in the CaptionedImagesAdapter it is an int [] imageIds. How to fix it all? - Mark Starikov
  • Edited, try it now - McDaggen
  • LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager (this, LinearLayoutManager.VERTICAL, false); swears at this. but it still does not find pizzas in int [] pizzaImages = new int [Pizza.pizzas.length]; for (int i = 0; i <pizzaImages.length; i ++) {pizzaImages [i] = Pizza.pizzas [i] .getImageResourceId (); } 'List <Pizza> namesItem = db.getItem ();' but db is not implemented here - Mark Starikov
  • @ Mark Starikov Replace this with getActvity. I left your method, as you implemented in it, I do not know. - McDaggen