简体   繁体   中英

RecycleView displays only some item in the beginning, after that when I add new item is not showing them?

When I add some item in the beginning, my code works, but after 1, 2 or maybe 3 items, it's not showing the new items and instead keeps showing old ones.

RecycleView Class:

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 adapter:

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

Layout class:

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>

remove or change this line

recyclerView.setHasFixedSize(true);

to

recyclerView.setHasFixedSize(false);

Every-time when you data is changed, the adapter should know the changes. You have to add notifyOnDataSetChanged.I will give you example hope that works for you.

In the WatchedfilmsAdapter notifyOnDataSetChanged to know that data is changed

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

}

Add refresh method when your data gonna update eveytime. In your case probably in ReadData()

adapterW.refresh(productList);

After adding new data to list call adapter.notifyDataSetChanged(); that will display your new data also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM