简体   繁体   English

为什么我的 onBindViewHolder () 根本没有运行,即使我的所有其他回收器视图方法都运行了?

[英]Why does my onBindViewHolder () not run at all even though all my other recycler view methods run?

I have created a recycler view that is supposed to display data in a text view and an image view.我创建了一个recycler view ,它应该在文本视图和图像视图中显示数据。 But the onBindViewHolder () for my adapter does not run.但是我的适配器的onBindViewHolder ()没有运行。 All the other methods in my adapter run.我的适配器中的所有其他方法都运行。 I cannot find the reason why the onBindViewHolder () does not run, everything else seems to be working fine it's just my data is not displayed in the card views for my recycler view.我找不到onBindViewHolder ()不运行的原因,其他一切似乎都工作正常,只是我的数据没有显示在我的回收站视图的卡片视图中。 Any help would be appreciated thanks.任何帮助将不胜感激。

Adapter code:适配器代码:

package com.myapps.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.cardview.widget.CardView;

public class captionedImagesAdapter extends RecyclerView.Adapter <captionedImagesAdapter.ViewHolder> {

private Context context;
private Cursor cursor;

public captionedImagesAdapter (Context context, Cursor cursor) {
    this.context = context;
    this.cursor = cursor;
}

public captionedImagesAdapter.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    CardView cv = (CardView) inflater.inflate(R.layout.card_view, parent, false);

    return new ViewHolder (cv);
}

public void onBindViewHolder(ViewHolder holder, int position) {
    if (cursor.moveToPosition(position)) {
        return;
    }

    String info_text = cursor.getString (0);
    byte [] info_image = cursor.getBlob(1);

    Bitmap bitmap = MyDatabaseHelper.getImages(info_image);

    holder.textView.setText(info_text);
    holder.imageView.setImageBitmap(bitmap);
}

public int getItemCount() {
    return cursor.getCount();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    private ImageView imageView;
    private TextView textView;

    public ViewHolder(CardView view) {
        super(view);
        imageView = view.findViewById(R.id.info_image);
        textView = view.findViewById(R.id.info_text);
    }
}

}

Database code:数据库代码:

package com.myapps.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class MyDatabaseHelper extends SQLiteOpenHelper {

private static final String DB_NAME = "starbuzz";
private static final int DB_VERSION = 5;
private Context context;
private ArrayList <Bitmap> bitmapArray;
private byte [] byteArray;

public MyDatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE GROCERY_ITEMS (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "NAME TEXT, " + "IMAGE_RESOURCE_ID BLOB);");

    upgradeDatabase(db);
}

public void upgradeDatabase (SQLiteDatabase db) {
    convertToBitmap();
    byteArray = convertToByte();
    addItems(db);
}

public void convertToBitmap () {
    Bitmap faan = BitmapFactory.decodeResource(context.getResources(), R.drawable.faan);
    Bitmap milk = BitmapFactory.decodeResource (context.getResources(), R.drawable.milk);
    Bitmap egg = BitmapFactory.decodeResource (context.getResources(), R.drawable.egg);
    Bitmap toilet_tissue = BitmapFactory.decodeResource (context.getResources(), R.drawable.toilet_tissue);
    Bitmap kitchen_tissue = BitmapFactory.decodeResource (context.getResources(), R.drawable.kitchen_tissue);
    Bitmap bread = BitmapFactory.decodeResource (context.getResources(), R.drawable.bread);
    Bitmap potatoe = BitmapFactory.decodeResource (context.getResources(), R.drawable.potatoe);
    Bitmap onion = BitmapFactory.decodeResource (context.getResources(), R.drawable.onion);
    Bitmap flour = BitmapFactory.decodeResource (context.getResources(), R.drawable.flour);
    Bitmap tomatoe = BitmapFactory.decodeResource (context.getResources(), R.drawable.tomatoe);
    Bitmap corriandor = BitmapFactory.decodeResource (context.getResources(), R.drawable.corriandor);

    bitmapArray = new ArrayList <Bitmap> ();

    bitmapArray.add(faan);
    bitmapArray.add (milk);
    bitmapArray.add (egg);
    bitmapArray.add (toilet_tissue);
    bitmapArray.add (kitchen_tissue);
    bitmapArray.add (bread);
    bitmapArray.add (potatoe);
    bitmapArray.add (onion);
    bitmapArray.add (flour);
    bitmapArray.add (tomatoe);
    bitmapArray.add (corriandor);

}

public byte [] convertToByte () {

    ByteArrayOutputStream stream = new ByteArrayOutputStream ();

    for (Bitmap bitmap : bitmapArray) {
        bitmap.compress (Bitmap.CompressFormat.PNG, 0, stream);
    }

    return stream.toByteArray();
}

public static Bitmap getImages (byte [] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

public void addItems (SQLiteDatabase db) {

    byte faan = byteArray [0];
    byte milk = byteArray [1];
    byte egg = byteArray [2];
    byte toilet_tissue = byteArray [3];
    byte kitchen_tissue = byteArray [4];
    byte bread = byteArray [5];
    byte potatoe = byteArray [6];
    byte onion = byteArray [7];
    byte flour = byteArray [8];
    byte tomatoe = byteArray [9];
    byte corriandor = byteArray [10];

    insertItems (db, "Faan", faan);
    insertItems (db, "Milk", milk);
    insertItems (db, "Egg", egg);
    insertItems (db, "Toilet Tissue", toilet_tissue);
    insertItems (db, "Kitchen Tissue", kitchen_tissue);
    insertItems (db, "Bread", bread);
    insertItems (db, "Potatoe", potatoe);
    insertItems (db, "Onion", onion);
    insertItems (db, "Flour", flour);
    insertItems (db, "Tomatoe", tomatoe);
    insertItems (db, "Corriandor", corriandor);

}

public void insertItems (SQLiteDatabase db, String name, byte image) {
    ContentValues contentValues = new ContentValues();
    contentValues.put ("NAME", name);
    contentValues.put ("IMAGE_RESOURCE_ID", image);

    db.insert ("GROCERY_ITEMS", null, contentValues);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("CREATE TABLE GROCERY_ITEMS (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "NAME TEXT, " + "IMAGE_RESOURCE_ID BLOB);");

    upgradeDatabase(db);
}
}

RecyclerView code:回收站查看代码:

package com.myapps.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class grocery_item extends AppCompatActivity {

SQLiteDatabase db;
Cursor cursor;

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_grocery);

    accessDataBase();

    RecyclerView groceryRecycler = (RecyclerView) findViewById(R.id.grocery_recycler_view);

    captionedImagesAdapter adapter = new captionedImagesAdapter (this, cursor);
    GridLayoutManager layoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
    groceryRecycler.setLayoutManager(layoutManager);
    groceryRecycler.setAdapter (adapter);
}

public void accessDataBase () {
    MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this);

    try {

        db = databaseHelper.getReadableDatabase();

        cursor = db.query ("GROCERY_ITEMS", new String[] {"NAME", "IMAGE_RESOURCE_ID"}, null, null, null, null, null);

    } catch (SQLiteException e) {
        e.printStackTrace();
    }
}
}

You're returning from onBindViewHolder() immediately:您将立即从onBindViewHolder()返回:

 if (cursor.moveToPosition(position)) { return; }

moveToPosition() returns true on success. moveToPosition()成功时返回 true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 回收站视图仅显示第一项,即使 onBindViewHolder 和 onCreateViewHolder 为所有项目运行 - Recycler view shows only first item, even though onBindViewHolder and onCreateViewHolder run for all of them 为什么即使使用javac编译正常,使用javapackager编译后我的程序仍无法运行? - Why is my program after compiling with javapackager couldn't run even though compiling with javac went fine? 为什么我的CheckTypeRoomForGuest()方法不循环所有来宾并且总是为true? - Why does my CheckTypeRoomForGuest() methods not loop all guest and always true? 为什么即使没有任何错误,编译器也不会运行程序? - Why does the compiler not run the program even though there are not any errors? 错误告诉我运行类中的方法未定义,即使它是 - Error telling me that the method in my run class is undefined even though it is 当我运行我的程序来计算所有偶数斐波那契数的总和时,为什么会得到一个负数 output? - Why do I get a negative output when I run my program to compute the sum of all even Fibonacci numbers? 为什么我的代码无法运行? - Why does my code not run? Recycler View 不调用 onBindviewHolder - Recycler View not calling onBindviewHolder 为什么paintComponent()运行,即使没有人调用它? - Why is paintComponent() run even though no one calls it? 尽管所有方法(似乎)都是正确的,但我的程序没有运行。 似乎是主要方法的问题? - My program doesn't run despite all methods being (seemingly) correct. seems to be a problem with the main method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM