I try a image data to sqlite database. and after i want take the data in database and use the recycler view. but other datas comes be successful image data is not comes to recycler view
This is my database class `public class DBOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "workifyDatabase";
public static final int DATABASE_VERSION = 1;
/* Constructor for the DBOpenHelper class, private to force use of singleton */
private DBOpenHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
/* Singleton approach (i.e. only 1 instance of DBOpenHelper exists)*/
private static volatile DBOpenHelper instance;
public static DBOpenHelper getInstance(Context context) {
if (instance==null) {
instance = new DBOpenHelper(context);
}
return instance;
}
/* If the database does not exist, then this method will be called */
/* ideal place to create the table(s) */
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS workifydatabase(id INTEGER PRIMARY KEY,workname VARCHAR,logoid BLOB,year VARCHAR,workinghours INTEGER) ");
}
/* If the database version is increased, then this method will be invoked */
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
/* Method to insert a row, using the convenience insert method */
/* this generates the underlying SQL and furthermore returns the id of the inserted row or -1 if the row was not inserted */
public long saveWorkRecord(String workname,byte[] logoid, String year,int workinghours) {
ContentValues cv = new ContentValues();
cv.put("workname",workname);
cv.put("logoid",logoid);
cv.put("year",year);
cv.put("workinghours",workinghours);
return this.getWritableDatabase().insert("workifydatabase",null,cv);
}
/* Example of retrieving data (might want to adapt this to return a TaskArrayList rather than a Cursor) */
public Cursor getAllDataAsCursor() {
return this.getWritableDatabase().query("workifydatabase",null /* all columns */,null /* all rows*/,null /* no selection args */, null,null,null);
}
}`
and this is my addind data class
`editTextTaskName = findViewById(R.id.editTextTaskName);
int selectedLogoId = LogoView.getId();
byte[] logoIdBytes = ByteBuffer.allocate(4).putInt(selectedLogoId).array();
Button saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String workName = editTextTaskName.getText().toString();
int workingHours = 0;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String formattedDate = dateFormat.format(date);
Cursor csr = db.getAllDataAsCursor();
boolean isDuplicate = false;
while (csr.moveToNext()) {
String existingWorkName = csr.getString(csr.getColumnIndex("workname"));
if (existingWorkName.equals(workName)) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
// Aynı isimde ödev yoksa kaydet
db.saveWorkRecord(workName, logoIdBytes, formattedDate, workingHours);
csr = db.getAllDataAsCursor();
DatabaseUtils.dumpCursor(csr);
finish();
} else {
Toast.makeText(CreateScreen.this, "There is already a task with this name!", Toast.LENGTH_SHORT).show();
}
}
});`
this is using class recycler view
`private void getData(){
try {
DBOpenHelper dbHelper = DBOpenHelper.getInstance(getContext());
//ArrayList<Task> taskList = new ArrayList<>();
Cursor cursor = dbHelper.getAllDataAsCursor();
int nameIx = cursor.getColumnIndex("workname");
int logoIdIx = cursor.getColumnIndex("logoid");
int yearIx = cursor.getColumnIndex("year");
int workingHoursIx = cursor.getColumnIndex("workinghours");
while (cursor.moveToNext()) {
String workName = cursor.getString(nameIx);
byte[] logoId = cursor.getBlob(logoIdIx);
Bitmap logoBitmap = BitmapFactory.decodeByteArray(logoId, 0, logoId.length);
String formattedDate = cursor.getString(yearIx);
int workingHours = cursor.getInt(workingHoursIx);
Task task = new Task(workName, logoBitmap, workingHours, formattedDate);
TaskArrayList.add(task);
}
taskAdapter.notifyDataSetChanged();
}catch (Exception e){
e.printStackTrace();
}
}`
and finally this is my recycler view adapter class
`public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
ArrayList<Task> TaskArrayList;
int enqueueNumber;
public TaskAdapter(ArrayList<Task> TaskArrayList) {
this.TaskArrayList = TaskArrayList;
}
@NonNull
@Override
public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task, parent, false);
return new TaskViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
holder.titleText.setText(TaskArrayList.get(position).getTaskName());
holder.infoText.setText("Total Working Hours: " + TaskArrayList.get(position).getWorkingHours());
holder.recyclerLogoView.setImageBitmap(TaskArrayList.get(position).getLogoBitmap());
}
@Override
public int getItemCount() {
return TaskArrayList.size();
}
class TaskViewHolder extends RecyclerView.ViewHolder {
TextView titleText;
TextView infoText;
ImageView imageViewTasks;
ImageView imageViewTasks2;
ImageView imageViewTasks8;
ImageView recyclerLogoView;
public TaskViewHolder(@NonNull View itemView) {
super(itemView);
titleText = itemView.findViewById(R.id.titleText);
infoText = itemView.findViewById(R.id.infoText);
imageViewTasks = itemView.findViewById(R.id.imageViewTasks);
imageViewTasks2 = itemView.findViewById(R.id.imageViewTasks2);
imageViewTasks8 = itemView.findViewById(R.id.imageViewTasks8);
recyclerLogoView = itemView.findViewById(R.id.recyclerLogoView);
Random random = new Random();
int randomNumber = random.nextInt(13)+1;
enqueueNumber = 15%randomNumber;
if (enqueueNumber == 0){
imageViewTasks8.setImageResource(R.drawable.orange);
}else if(enqueueNumber == 2){
imageViewTasks8.setImageResource(R.drawable.yellow);
}else if(enqueueNumber == 3){
imageViewTasks8.setImageResource(R.drawable.gray);
} else if (enqueueNumber == 1) {
imageViewTasks8.setImageResource(R.drawable.green);
}else if(enqueueNumber == 6){
imageViewTasks8.setImageResource(R.drawable.blue);
}else if(enqueueNumber == 5){
imageViewTasks8.setImageResource(R.drawable.pink);
}else if(enqueueNumber == 4){
imageViewTasks8.setImageResource(R.drawable.purple);
}else if(enqueueNumber == 7){
imageViewTasks8.setImageResource(R.drawable.red);
}
}
}
}`
i want to make visible recyclerviewlogo image on recyclerview
2
Answers
After getting the drawable paths as string data to the database, I did the following:
and then I converted the string data I received in the adapter class into a drawable file :
' @Override public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
The thing to note here is to get the name of the assets directly, not the drawable paths, so for example, not res/drawable/sun.png but only the sun name.
Here’s a modified version of your code snippets to give you an idea of how to achieve this:
Remember to integrate a library like Glide or Picasso into your project and handle permissions for reading and writing images on the filesystem.