Greetings I have 2 databases in the first 100 points and this is a list of chapters, in the second more than 250 are the contents of the chapters, I need to distribute these 250 into 100 chapters. For example, in the first chapter 3 items of the contents of the chapters, in the second 1, and thus a different number in different chapters. I can not understand the algorithm for such processing. My adapter returns the length of the database array, how do I properly distribute in the ratio I need? enter image description here

Addition:

DB class:

class HeadsSQLiteOpenHelper extends SQLiteOpenHelper { private static final int mDBVersion = 1; private static final String mDBName = "MYTESTDB"; private static final String mTableName = "TEST_TABLE"; private static final String mId = "_id"; private static final String mLINE1 = "LINE1"; private static final String mLINE2 = "LINE2"; private static final String[] myColumns = {mId, mLINE1, mLINE2}; private static final String myTable = "CREATE TABLE " + mTableName + "(" + mId + " INTEGER PRIMARY KEY AUTOINCREMENT, " + mLINE1 + " TEXT, " + mLINE2 + " TEXT)"; HeadsSQLiteOpenHelper(Context context) { super(context, mDBName, null, mDBVersion); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(myTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + mTableName); this.onCreate(db); } List<Heads> getHeadsItem(){ SQLiteDatabase db = this.getReadableDatabase(); @SuppressLint("Recycle") Cursor cursor = db.query(mTableName, // a. table myColumns, null, null, null, null, null, null); List<Heads> headsItem = new ArrayList<>(); if(cursor.moveToFirst()){ while(!cursor.isAfterLast()){ headsItem.add(new Heads( cursor.getString(cursor.getColumnIndex(mLINE1)), cursor.getString(cursor.getColumnIndex(mLINE2)))); cursor.moveToNext(); } } return headsItem; } 

Adapter class:

 class TheHeadsAdapter extends RecyclerView.Adapter<TheHeadsAdapter.ViewHolder> { private List<Heads> mHeads; TheHeadsAdapter(List<Heads> headsItem) { this.mHeads = headsItem; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate( R.layout.head_items, parent, false); return new ViewHolder(itemView); } @Override public void onBindViewHolder(final ViewHolder viewHolder, final int position) { viewHolder.line1.setText(Html.fromHtml(mHeads.get(position).getLine1())); viewHolder.line2.setText(Html.fromHtml(mHeads.get(position).getLine2())); } @Override public int getItemCount() { return mHeads.size(); } static class ViewHolder extends RecyclerView.ViewHolder { TextView line1, line2; ViewHolder(View itemView) { super(itemView); line1 = (TextView) itemView.findViewById(R.id.line1); line2 = (TextView) itemView.findViewById(R.id.line2); } } 

Thus, the method

 public int getItemCount() { return mHeads.size(); } 

Returns the entire length of positions from the database to the specified RecyclerView and this is a list of chapters where there are 100 positions. Now the task is that I can, in each of these positions, load the amount I need from another similar adapter. For example, in 1 position of chapters 3 positions of the contents of chapters, in 2 - 5, in 3 - 2, and thus they differ.

  • And why create two bases for the Chapters and the Content Chapters, is it really impossible to do with two tables? - DevOma
  • Well, this is not so important, for me the main thing is to understand how to load the necessary number of points into the necessary chapter. - McDaggen

2 answers 2

You need to create a database of a certain structure, in which there will be 2 tables.

Table of Title:

 | _id | title | --------------- | 1 | Глава1 | | 2 | Глава2 | 

Content Table:

 | _id | content | id_title | ---------------------------- | 1 | контент1| 1 | | 2 | контент2| 1 | | 3 | контент3| 2 | | 4 | контент4| 2 | | 5 | контент5| 2 | 

The Content table contains a one-to-many relationship with the Title table through the id_title field (column), in which the chapter ID identifies which chapters are associated with which content.

Here: content1 and 2 are connected with the first chapter, content3, 4, 5 - with the second chapter.

Further, after clicking on one of the chapter items, we get the ID of this chapter and transfer it to the second activation to display a list of contents, where we query the database for the Content table with a selection of values ​​corresponding to the table field (column) of the table id_title .

  • How simple it is! You are a genius! - McDaggen
  • @McDaggen is a standard DBMS pattern (one-to-many connection) known for decades and my merit here is only in what I know about it. If you want to work with relational databases, I advise you to read the excellent book by O'Reilly for authorship Bailey L. - "Learning SQL" - 2012. Where you can learn everything about the design of the database structure, types of relationships, making complex queries and all the rest in an accessible and "live" manner of presentation. - pavlofff
  • just it in my turn to read and study. - McDaggen

There is an assumption that the database has connections by id. For example, the contents of the chapter and the number of the chapter in which it is written. Then we read the list of chapters and write to the list with our id. Then we read the database with the contents. We take the next content and write to the chapter that has the corresponding number. Without any connections, it is not clear how to relate them. Hope that helped.