【问题标题】:Image not showing in recyclerview from Sqlite图片未显示在 Sqlite 的 recyclerview 中
【发布时间】:2019-02-10 03:11:49
【问题描述】:

我分别有一些数据和图像。我已将其保存到 Sqlite 中。从数据库中弹出后,所有数据都显示给我,但没有显示图像。 我已经保存了图像 sqlite 抛出代码:

 holder.favoutitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            holder.tPicture.buildDrawingCache();
            Bitmap bitmap = holder.tPicture.getDrawingCache();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] data = baos.toByteArray();
            databaseHelper=new DatabaseHelper(context);

            boolean insert=databaseHelper.addToDb(subjects.get(position).getId(),subjects.get(position).getRoomid(),
                    subjects.get(position).getHonorname(),subjects.get(position).getRoomname(),subjects.get(position).getLocation(), subjects.get(position).getHonorphno(),
                    subjects.get(position).getPrice(),subjects.get(position).getGenralLocation() ,subjects.get(position).getLongitudeLatitude(),data, "data");
           // app:sparkbutton_inActiveImage=""
            if (insert)
            {
                Toast.makeText(context, "Data inserted into Fav", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(context, "Not inserted", Toast.LENGTH_SHORT).show();
            }
        }
    });

它是recyclerview适配器,用于将数据与图像一起插入到sqlite 现在这里是从 sqlite 获取数据的代码。所有数据都完美显示,而不是图像。

recyclerView = (RecyclerView) view.findViewById(R.id.favRoomRecyclerView);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    db = new DatabaseHelper(getActivity());
    // int coul= Integer.parseInt(db.COL11);
    SQLiteDatabase sqLiteDatabase = db.getReadableDatabase();
    cursor = db.getData("data");
    cursor.moveToFirst();
    //Toast.makeText(this, "Id is :"+cursor.getColumnName(Integer.parseInt("ID")), Toast.LENGTH_SHORT).show();


    if (cursor.getCount() > 0) {
        do {
            byte[] blob = cursor.getBlob(cursor.getColumnIndex(db.COL11));
            ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            RoomGetrSetr room = new RoomGetrSetr(cursor.getInt(1), cursor.getInt(2),
                    cursor.getString(3), cursor.getString(4), cursor.getString(5),
                    cursor.getString(6), cursor.getString(7), cursor.getString(8),
                    cursor.getString(9),
                    cursor.getBlob(cursor.getColumnIndex(db.COL11)));
            arrayList.add(room);
        } while (cursor.moveToNext());
        db.close();

    }

    cursor.close();
    adapter = new Fav_Room_Adapter(arrayList, getActivity());
    recyclerView.setAdapter(adapter);

getter 和 setter 类:

 public RoomGetrSetr(int id, int roomid , String honorname, String roomname, String location,
                    String honorphno, String price, String genralLocation, String LongitudeLatitude , byte[] imgae_url) {
    this.id=id;
    this.roomid=roomid;
    this.honorname = honorname;
    this.roomname = roomname;
    this.location = location;
    this.honorphno = honorphno;
    this.price = price;
    this.genralLocation=genralLocation;
    this.LongitudeLatitude=LongitudeLatitude;
    this.imgae_url=imgae_url;
}
 public byte[] getImgae() {
    return imgae_url;
}

DatabaseHelper 类

 @Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL2 + " TEXT, " + COL3 + " TEXT, " + COL4 + " TEXT, " + COL5 + " TEXT, " + COL6 + " TEXT, " + COL7 + " TEXT, " + COL8 + " TEXT, " +
            COL9 + " TEXT, " + COL10 + " TEXT, " + COL11 + " BLOB NOT NULL, " + COL12 + " TEXT )";
    db.execSQL(createTable);
    System.out.println("Table Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
    onCreate(db);
}
public boolean addToDb(int id,int roomid, String honorname, String roomname,
                       String location, String honorphno,String price,String genrallocation,
                       String LongitudeLatitude, byte[] image, String user_id){
    ContentValues cv = new  ContentValues();

    System.out.println("Null pointer Exception");
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, id);
    contentValues.put(COL3, roomid);
    contentValues.put(COL4, honorname);
    contentValues.put(COL5, roomname);
    contentValues.put(COL6, location);
    contentValues.put(COL7, honorphno);
    contentValues.put(COL8,price);
    contentValues.put(COL9,genrallocation);
    contentValues.put(COL10,LongitudeLatitude);
    contentValues.put(COL11,image);
    contentValues.put(COL12,user_id);
    //counterForDeleteSubject=counterForDeleteSubject+1;

    System.out.println("User Id is:"+user_id);
    Log.d("DatabaseHelper", "addData: Adding to " + TABLE_NAME);

    long result = db.insert(TABLE_NAME, null, contentValues);
    //SubjectStatusChecked();
    System.out.println("Counter is --="+counterForDeleteSubject);
    ///Checking status if subject is added to SQLite then status is updated so that button is clicked




    if (result == -1) {
        return false;
    } else {
        return true;
    }
}
public Cursor getData(String userId){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + COL12 + " = '" + userId + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}

这是我用于设置数据和图像的适配器:

 @Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    RoomGetrSetr list=subjects.get(position);
    holder.roonName.setText(subjects.get(position).getRoomname());
    holder.honorName.setText(subjects.get(position).getHonorname());
    holder.loation.setText(subjects.get(position).getLocation());
    holder.price.setText(subjects.get(position).getPrice());

现在在最后的代码中,我如何将图像设置到这个 adpater。 我在片段中显示这个。

【问题讨论】:

  • 我想你忘记在这个 'onBindViewHolder' 方法中设置图像视图内的图像
  • @Lucefer holder.tPicture.setImageBitmap(subjects.get(position).getImgae());它不工作我要求它如何设置图像
  • 您是否调试过该代码并检查了 subject.get(position).getImgae() 中的内容?
  • 这个方法“imageview can't apply to byte[]”下面的红线一样给我错误
  • 尝试这个stackoverflow.com/questions/9357668/…的解决方案,在sqlite db中保存和检索图像

标签: android sqlite android-sqlite android-imageview


【解决方案1】:

我有两个关于你的代码的 cmets。

1.- 在调用 databaseHelper.addToDb 方法时,您使用相同的用户 ID "data",您的用例可以吗?

2.- 您的方法onBindViewHolder 没有使用属性imgae_url 也没有使用您的类getImgae() 的方法RoomGetrSetr 来将其绑定到ImageView,我想您在创建时错过了那部分这个问题。

能否提供您的onBindViewHolder 方法的完整代码?

【讨论】:

  • 1- 对我来说没问题,因为这是我的逻辑,我稍后必须更改它。 2- holder.tPicture.setImageBitmap(subjects.get(position).getImgae());但它不起作用。
猜你喜欢
  • 2021-03-17
  • 2020-06-24
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多