So i was following this video https://www.youtube.com/watch?v=7AxlMtksJUs
I am trying to get Sqlite data to show in a RecyclerView inside a Fragment.
I have not taken any inputs from the user.
I just want the table that i have made in Sqlite to be displayed in RecyclerView.
I have copied the database file and put it in the assets folder.
I have tried my best to replicate it, so far there was no Errors until I run it.
Any help would be appreciated, im fairly new to this, i have checked all solutions, nothing seems to be working.
This is my last resort.
I opened Logcat and found out these Errors:
E/SQLiteLog: (1) no such table: EnglishSongs in "SELECT song_no, song_title FROM EnglishSongs"
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gospelsongbook, PID: 3312
android.database.sqlite.SQLiteException: no such table: EnglishSongs (code 1 SQLITE_ERROR): , while compiling: SELECT song_no, song_title FROM EnglishSongs
The below 2 errors were in blue:
at com.example.gospelsongbook.EngsongDatabaseAdapter.getAllEngsong(EngsongDatabaseAdapter.java:26)
at com.example.gospelsongbook.EnglishSongsFragment.onCreateView(EnglishSongsFragment.java:33)
This is the fragment in which I want to display the data in the RecyclerView
EnglishSongsFragment.java:
public class EnglishSongsFragment extends Fragment {
EngsongDatabaseAdapter engsongDatabaseAdapter;
RecyclerView rvEnglish;
RecyclerView.LayoutManager layoutManager;
EngsongAdapter engsongAdapter;
List<Engsong> engsongList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_english_songs, container, false);
DatabaseAccess.copyDB(requireActivity());
engsongDatabaseAdapter = new EngsongDatabaseAdapter(getContext());
engsongList = engsongDatabaseAdapter.getAllEngsong();
rvEnglish = (RecyclerView) view.findViewById(R.id.rvEnglish);
rvEnglish.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
rvEnglish.setLayoutManager(layoutManager);
engsongAdapter = new EngsongAdapter(getContext(), engsongList, rvEnglish);
rvEnglish.setAdapter(engsongAdapter);
return view;
}
}
Constructors and Getter
Engsong.java
public class Engsong {
private final long song_no;
private final String song_title;
public Engsong(long song_no, String song_title)
{
this.song_no = song_no;
this.song_title = song_title;
}
public long getSong_no() {
return song_no;
}
public String getSong_title() {
return song_title;
}
RecyclerView adapter
EngsongAdapter.java:
public class EngsongAdapter extends RecyclerView.Adapter<EngsongAdapter.ViewHolder> {
Context context;
List<Engsong> engsongList;
RecyclerView rvEnglish;
final View.OnClickListener onClickListener = new EngOnClickListener();
public static class ViewHolder extends RecyclerView.ViewHolder
{
TextView rowEngsongno;
TextView rowEngsongtitle;
public ViewHolder(@NonNull View itemView) {
super(itemView);
rowEngsongno = itemView.findViewById(R.id.eng_song_no);
rowEngsongtitle = itemView.findViewById(R.id.eng_song_title);
}
}
public EngsongAdapter (Context context, List<Engsong> engsongList, RecyclerView rvEnglish)
{
this.context = context;
this.engsongList = engsongList;
this.rvEnglish = rvEnglish;
}
@NonNull
@Override
public EngsongAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.single_item, parent, false);
view.setOnClickListener(onClickListener);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull EngsongAdapter.ViewHolder holder, int position) {
Engsong engsong = engsongList.get(position);
holder.rowEngsongno.setText(""+engsong.getSong_no());
holder.rowEngsongtitle.setText(engsong.getSong_title());
}
@Override
public int getItemCount() {
return engsongList.size();
}
private class EngOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
int itemPosition = rvEnglish.getChildLayoutPosition(view);
String item = engsongList.get(itemPosition).getSong_title();
Toast.makeText(context, item, Toast.LENGTH_SHORT).show();
}
}
}
DataBase Adapter
EngsongDatabaseAdapter.java:
public class EngsongDatabaseAdapter {
DatabaseHelper helper;
SQLiteDatabase db;
List<Engsong> engsongList = new ArrayList<>();
public EngsongDatabaseAdapter(Context context)
{
helper = new DatabaseHelper(context);
db = helper.getWritableDatabase();
}
public List<Engsong> getAllEngsong()
{
String[] columns = {DatabaseHelper.KEY_ROWENGSONGNO, DatabaseHelper.KEY_ENGSONGTITLE};
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, columns, null,null,null,null, null, null);
while(cursor.moveToNext())
{
int index1 = cursor.getColumnIndex(DatabaseHelper.KEY_ROWENGSONGNO);
int rowsongno = cursor.getInt(index1);
int index2 = cursor.getColumnIndex(DatabaseHelper.KEY_ENGSONGTITLE);
String songtitle = cursor.getString(index2);
Engsong engsong = new Engsong(rowsongno, songtitle);
engsongList.add(engsong);
}
cursor.close();
return engsongList;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "GospelSongBookData.db";
private static final String TABLE_NAME = "EnglishSongs";
private static final int DATABASE_VERSION = 5;
private static final String KEY_ROWENGSONGNO = "song_no";
private static final String KEY_ENGSONGTITLE = "song_title";
private final Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
}
DatabaseAccess:
public class DatabaseAccess {
public static void copyDB(Context context)
{
try
{
String destPath = "/data/data/"+ context.getPackageName()
+ "/databases";
File f = new File(destPath);
if(!f.exists()){
f.mkdir();
// Copy the db from assets folder into the databases folder
rawCopy(context.getAssets().open("GospelSongBookData.db"), new FileOutputStream(destPath + "/GospelSongBookData.db"));
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void rawCopy(InputStream inputStream, OutputStream outputStream) throws IOException
{
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}