我正在从sqlite数据库获取空白的recycleview吗?

时间:2019-01-31 19:07:05

标签: java android android-studio android-recyclerview

我想在此RecycleView中从SqliteDatabase检索我的数据,但是我得到了空白列表? 我在SqliteManager中看到数据库,并且数据已正确插入,但无法在布局中显示。 在开始阶段,它仅适用于2个项目,现在不起作用,它的RecycicleView为空。

有帮助吗?

数据库

//Database

public class FilmaDb extends SQLiteOpenHelper {

    private static final String DATABASE_NAME="Filma_db";
    private static final Integer DATABASE_VERSION=2;

    public static final String TABLE1="CREATE TABLE Filmi(Id INTEGER PRIMARY KEY AUTOINCREMENT,Emri TEXT)";
    public static final String WatchedMovie="CREATE TABLE WatchedMovie(ID INTEGER PRIMARY KEY AUTOINCREMENT,Emri TEXT,Rate DOUBLE,Photo BLOB)";

    public FilmaDb(Context context,String name,SQLiteDatabase.CursorFactory factory, int version) {
        super(context,DATABASE_NAME, null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL(TABLE1);
      db.execSQL(WatchedMovie);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS Filmi");
        db.execSQL("DROP TABLE IF EXISTS WatchedMovie");
        onCreate(db);
    }
}
  

Java << / p>

//Java class for RecycleView

public class FilmateShikuara {
    private  String Emri;
    private  Double Rating;
    private  Bitmap Image;

    public String getEmri() {
        return Emri;
    }

    public Double getRating() {
        return Rating;
    }

    public Bitmap getImage() {
        return Image;
    }

    public FilmateShikuara(String emri, Double rating, Bitmap image) {
        Emri = emri;
        Rating = rating;
        Image = image;


    }
}
  

RecycleView适配器>

public class WatchedfilmsAdapter extends RecyclerView.Adapter<WatchedfilmsAdapter.MyViewHolder>{

    private Context mContext;
    private List<FilmateShikuara> mData;

    public WatchedfilmsAdapter(Context mContext, List<FilmateShikuara> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater=LayoutInflater.from(mContext);
        View view=inflater.inflate(R.layout.layout_product,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.movieImage.setImageBitmap(mData.get(i).getImage());
        myViewHolder.movieName.setText(mData.get(i).getEmri());
        String rating=mData.get(i).getRating().toString();
        myViewHolder.movieRating.setText(rating);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView movieImage;
        TextView movieName;
        TextView movieRating;

        MyViewHolder(@NonNull View itemView) {
            super(itemView);

            movieImage=(ImageView)itemView.findViewById(R.id.MovieImage);
            movieName=(TextView)itemView.findViewById(R.id.MovieName);
            movieRating=(TextView)itemView.findViewById(R.id.Movie_Rating);
        }
    }
}
  

布局类<< / p>

public class Watched_MoviesList extends AppCompatActivity{

    List<FilmateShikuara> productList;
    SQLiteDatabase db;
    FilmaDb dbHelper;
    RecyclerView recyclerView;
    static WatchedfilmsAdapter adapterW;
    ImageView homeIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watched__movies_list);

        homeIcon=(ImageView)findViewById(R.id.HomeIcon);
        homeIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(),MainActivity.class);
                startActivity(intent);
            }
        });
        dbHelper=new FilmaDb(this,"Filma_db",null,2);
        db=dbHelper.getWritableDatabase();
        productList=new ArrayList<>();
        recyclerView= findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ReadData();
        adapterW=new WatchedfilmsAdapter(this,productList);
        recyclerView.setAdapter(adapterW);
    }

    public void ReadData()
    {
        Cursor c=db.rawQuery("SELECT * FROM WatchedMovie",null);
        while (c.moveToNext())
        {
            String moviename=c.getString(c.getColumnIndex("Emri"));
            Double rate=c.getDouble(c.getColumnIndex("Rate"));
            byte[] image=c.getBlob(c.getColumnIndex("Photo"));
            Bitmap bitmap=BitmapFactory.decodeByteArray(image,0,image.length);
            productList.add(new FilmateShikuara(moviename,rate,bitmap));
        }
        c.close();
    }
}

1 个答案:

答案 0 :(得分:0)

Change ReadData method like below:
     public void ReadData()
    {
        Cursor c=db.rawQuery("SELECT * FROM WatchedMovie",null);
        if (c. moveToFirst())
        {
            do {
                String moviename=c.getString(c.getColumnIndex("Emri"));
                Double rate=c.getDouble(c.getColumnIndex("Rate"));
                byte[] image=c.getBlob(c.getColumnIndex("Photo"));
                Bitmap bitmap=BitmapFactory.decodeByteArray(image,0,image.length);
                productList.add(new FilmateShikuara(moviename,rate,bitmap));
            }
            while (c.moveToNext());
        }
        c.close();
    }