RecycleView在开始时仅显示某些项目,之后在添加新项目时不显示它们吗?

时间:2019-01-31 20:38:05

标签: java android xml android-recyclerview

当我在开头添加一些项目时,我的代码有效,但是在1、2或3个项目之后,它没有显示新项目,而是一直显示旧项目。

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

    public void setEmri(String emri) {
        Emri = emri;
    }

    public void setRating(Double rating) {
        Rating = rating;
    }

    public void setImage(Bitmap image) {
        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);
        }
    }
}

布局类:

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

Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Watched_MoviesList"
    android:background="#1E1E2C">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            android:background="#141421"
            android:clickable="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="12dp"
                android:text="Watched movies"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                android:fontFamily="@font/roboto_bold"
                android:textStyle="bold"/>

            <ImageView
                android:id="@+id/HomeIcon"
                android:layout_width="29dp"
                android:layout_height="29dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_alignParentEnd="true"
                android:background="@drawable/edithomeicon"/>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/editlinearlayout"
                android:layout_margin="0dp">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_marginTop="20dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

删除或更改此行

recyclerView.setHasFixedSize(true);

recyclerView.setHasFixedSize(false);

答案 1 :(得分:0)

每次更改数据时,适配器都应知道更改。 您必须添加notifyOnDataSetChanged。我会给您示例希望对您有用的

在WatchedfilmsAdapter中,notifyOnDataSetChanged知道数据已更改

  public void refresh(List<FilmateShikuara> mData){
  this.mData = mData;
  notifyOnDataSetChanged();

}

当您的数据将更新eveytime时,添加刷新方法。在您的情况下,可能在ReadData()

adapterW.refresh(productList);

答案 2 :(得分:0)

在将新数据添加到列表调用adapter.notifyDataSetChanged();后,该调用还将显示您的新数据