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

huangapple go评论107阅读模式
英文:

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

问题

我已经翻译了您提供的代码部分,以下是翻译好的内容:

我已经创建了一个用于显示文本视图和图像视图中数据的recycler view”,但是我的适配器中的onBindViewHolder()”方法没有运行我的适配器中的所有其他方法都可以正常运行我找不到onBindViewHolder()”不运行的原因似乎一切都正常工作只是我的数据没有显示在recycler view的卡片视图中如果有任何帮助将不胜感激谢谢

适配器代码

```java
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);
        }
    }

}

数据库代码:

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() {
        // 省略了图片资源的加载代码
    }

    public byte[] convertToByte() {
        // 省略了图片转换为字节数组的代码
    }

    public static Bitmap getImages(byte[] image) {
        // 省略了获取图片的代码
    }

    public void addItems(SQLiteDatabase db) {
        // 省略了向数据库中插入数据的代码
    }

    @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 代码:

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();
        }
    }
}

希望这有助于解决您的问题。如果您需要更多帮助,请随时提问。

英文:

I have created a recycler view that is supposed to display data in a text view and an image view. But the onBindViewHolder () for my adapter does not run. 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. 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 &lt;captionedImagesAdapter.ViewHolder&gt; {
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 = &quot;starbuzz&quot;;
private static final int DB_VERSION = 5;
private Context context;
private ArrayList &lt;Bitmap&gt; 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(&quot;CREATE TABLE GROCERY_ITEMS (&quot; + &quot;_id INTEGER PRIMARY KEY AUTOINCREMENT, &quot; + &quot;NAME TEXT, &quot; + &quot;IMAGE_RESOURCE_ID BLOB);&quot;);
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 &lt;Bitmap&gt; ();
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, &quot;Faan&quot;, faan);
insertItems (db, &quot;Milk&quot;, milk);
insertItems (db, &quot;Egg&quot;, egg);
insertItems (db, &quot;Toilet Tissue&quot;, toilet_tissue);
insertItems (db, &quot;Kitchen Tissue&quot;, kitchen_tissue);
insertItems (db, &quot;Bread&quot;, bread);
insertItems (db, &quot;Potatoe&quot;, potatoe);
insertItems (db, &quot;Onion&quot;, onion);
insertItems (db, &quot;Flour&quot;, flour);
insertItems (db, &quot;Tomatoe&quot;, tomatoe);
insertItems (db, &quot;Corriandor&quot;, corriandor);
}
public void insertItems (SQLiteDatabase db, String name, byte image) {
ContentValues contentValues = new ContentValues();
contentValues.put (&quot;NAME&quot;, name);
contentValues.put (&quot;IMAGE_RESOURCE_ID&quot;, image);
db.insert (&quot;GROCERY_ITEMS&quot;, null, contentValues);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(&quot;CREATE TABLE GROCERY_ITEMS (&quot; + &quot;_id INTEGER PRIMARY KEY AUTOINCREMENT, &quot; + &quot;NAME TEXT, &quot; + &quot;IMAGE_RESOURCE_ID BLOB);&quot;);
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 (&quot;GROCERY_ITEMS&quot;, new String[] {&quot;NAME&quot;, &quot;IMAGE_RESOURCE_ID&quot;}, null, null, null, null, null);
} catch (SQLiteException e) {
e.printStackTrace();
}
}
}

答案1

得分: 1

你立即从 onBindViewHolder() 返回:

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

moveToPosition() 在成功时返回 true。

英文:

You're returning from onBindViewHolder() immediately:

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

moveToPosition() returns true on success.

huangapple
  • 本文由 发表于 2020年8月10日 21:25:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63341169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定