简体   繁体   English

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

[英]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. 当我在开头添加一些项目时,我的代码有效,但是在1、2或3个项目之后,它不会显示新项目,而是继续显示旧项目。

RecycleView Class: 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 adapter: 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);
        }
    }
}

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: 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. 您必须添加notifyOnDataSetChanged。我会给您示例希望对您有用的示例。

In the WatchedfilmsAdapter notifyOnDataSetChanged to know that data is changed 在WatchedfilmsAdapter中,notifyOnDataSetChanged知道数据已更改

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

} }

Add refresh method when your data gonna update eveytime. 当您的数据将更新eveytime时,请添加刷新方法。 In your case probably in ReadData() 在您的情况下,可能在ReadData()中

adapterW.refresh(productList);

After adding new data to list call adapter.notifyDataSetChanged(); 将新数据添加到列表后,调用adapter.notifyDataSetChanged(); that will display your new data also 还将显示您的新数据

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

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