RecyclerView doesn’t show data from DB

  android, android-recyclerview, arraylist, java

I get data from DataBase(it works correctly), and must show it in RecyclerView, but my RecyclerView doesnt show anything, altough it isnt empty. My adapter is very simple:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { 
 
 ArrayList<Template> templates = new ArrayList<>(); 
 
 
 public RecyclerViewAdapter(){} 
 
 public RecyclerViewAdapter(ArrayList<Template> myTemplates) { 
 this.templates = myTemplates; 
 } 
 
 
 @NonNull 
 @Override 
 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 
 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); 
 return new ViewHolder(view); 
 } 
 
 @Override 
 public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 
 holder.myTitle.setText(templates.get(position).getTitle()); 
 holder.myLink.setText(templates.get(position).getImageLink()); 
 } 
 
 @Override 
 public int getItemCount() { 
 return 0; 
 } 
 
 public class ViewHolder extends RecyclerView.ViewHolder { 
 
 TextView myTitle, myLink; 
 
 public ViewHolder(@NonNull View itemView) { 
 super(itemView); 
 
 myTitle = itemView.findViewById(R.id.title); 
 myLink = itemView.findViewById(R.id.link); 
 } 
 } 
} 
 

Class Template is veru easy either, and I dont think I need show it.

Here is my MainActivity, where my RecyclerView is given data

public class MainActivity extends AppCompatActivity { 
 
 private TemplateDatabaseHelper mDBHelper; 
 private SQLiteDatabase mDb; 
 RecyclerView rv; 
 RecyclerViewAdapter adapter; 
 ArrayList<Template> myTemplates; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 
 myTemplates = new ArrayList<>(); 
 mDBHelper = new TemplateDatabaseHelper(this); 
 rv = findViewById(R.id.rv); 
 
 try { 
 mDBHelper.updateDataBase(); 
 } catch (IOException mIOException) { 
 throw new Error("UnableToUpdateDatabase"); 
 } 
 
 try { 
 mDb = mDBHelper.getWritableDatabase(); 
 } catch (SQLException mSQLException) { 
 throw mSQLException; 
 } 
 
 Cursor cursor = mDb.rawQuery("SELECT * FROM titles", null); 
 cursor.moveToFirst(); 
 while (!cursor.isAfterLast()) { 
 Template template = new Template(cursor.getString(1), cursor.getString(2)); 
 
 myTemplates.add(template); 
 cursor.moveToNext(); 
 } 
 
 rv.setHasFixedSize(true); 
 rv.setLayoutManager(new LinearLayoutManager(this)); 
 adapter = new RecyclerViewAdapter(myTemplates); 
 rv.setAdapter(adapter); 
 } 
} 
 

I used debugger, and everything works correctly, it gets data and my ArrayList isnt empty, all my code is supposed to work correctly, but my RecyclerView remains empty. Help, please

Source: Android Questions

LEAVE A COMMENT