当前位置: 首页 > 知识库问答 >
问题:

当恢复activity时,RecyclerView返回null

计承德
2023-03-14

我有一个RecolyerView,当activity启动时,它工作得很好。当我从那个activity去另一个再回来时,recyclerView是空的。它什么也不显示,但有时它返回值,但布局受到干扰,即项目之间有空隙,或布局将有一个很大的空隙,从activity顶部。

类的代码是:

public class product_page extends AppCompatActivity {

  FirebaseAuth mAuth;
  String s, product;
  ProgressDialog progressDialog;
  private RecyclerView recyclerView;
  FirebaseDatabase firebaseDatabase;
  DatabaseReference databaseReference;
  DatabaseReference db;
  FirebaseRecyclerAdapter<ProductItemsConstructor, productViewHolder> FirebaseAdapter;

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

        Intent intent = getIntent();
        s = intent.getExtras().getString("pos");
        product = intent.getExtras().getString("product");

    mAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();

    RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
   // RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());


    Query query = firebaseDatabase.getReference().child("Products").child(s).child(product).orderByKey();

    FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<ProductItemsConstructor>().setQuery(query,ProductItemsConstructor.class).build();

    FirebaseAdapter = new FirebaseRecyclerAdapter<ProductItemsConstructor, productViewHolder>(options) {


        @Override
        public productViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items,parent,false);
            return new productViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull productViewHolder holder, int position, @NonNull ProductItemsConstructor model) {
            holder.Heading(model.getHeading());
            holder.Price(model.getPrice());
            holder.Prates(model.getPprice());
            holder.Rating(model.getRating());
            holder.setImg(getApplicationContext(),model.getImageUrl());
        }
    };
    recyclerView.setAdapter(FirebaseAdapter);

}


@Override
protected void onStart() {
    super.onStart();

    FirebaseAdapter.startListening();
    recyclerView.setAdapter(FirebaseAdapter);
}

@Override
protected void onStop() {
    super.onStop();
    FirebaseAdapter.stopListening();
}

@Override
protected void onResume() {
    super.onResume();

    FirebaseAdapter.notifyDataSetChanged();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent in = new Intent(product_page.this, MainActivity.class);
    startActivity(in);
}

public class productViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView head, rate, Prate, rating;
    ImageView img;


    public productViewHolder(final View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
    }

    private void Heading(String heading) {
        head = (TextView) itemView.findViewById(R.id.Pheading);
        head.setText(heading);
    }

    private void Price(String price) {
        rate = (TextView) itemView.findViewById(R.id.Pprice);
        rate.setText(price);
    }

    private void Prates(String prate){
        Prate = (TextView)itemView.findViewById(R.id.pRate);
        Prate.setText(prate);
    }

    private void Rating(String rting){
        rating = (TextView)itemView.findViewById(R.id.Rating);
        rating.setText(rting);
    }

    private void setImg(Context ctx, String img1) {
        img = (ImageView) itemView.findViewById(R.id.PImage);
        Picasso.with(ctx).load(img1).placeholder(R.drawable.notification).into(img);
        // progressDialog.dismiss();
    }
}

这是activity首先启动时的影像

在此输入图像说明

这是恢复activity时的形象

在此输入图像说明

日志如下所示

null

如有任何帮助或帮助,我们将不胜感激。

共有2个答案

许庆
2023-03-14

只对onResume()方法进行这些更改。

@Override
protected void onResume() {
super.onResume();

   //FirebaseAdapter.notifyDataSetChanged();
   getData();
   }
}
private void getData() {
    RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2, android.support.v7.widget.StaggeredGridLayoutManager.VERTICAL);
    // RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView = (android.support.v7.widget.RecyclerView) findViewById(R.id.recyclerView1);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new android.support.v7.widget.DefaultItemAnimator());

    Query query = firebaseDatabase.getReference().child("Products").child(s).child(product).orderByKey();

    FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<ProductItemsConstructor>().setQuery(query, ProductItemsConstructor.class).build();

    FirebaseAdapter = new FirebaseRecyclerAdapter<ProductItemsConstructor, productViewHolder>(options) {

        @Override
        public productViewHolder onCreateViewHolder(android.view.ViewGroup parent, int viewType) {
            View view = android.view.LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items, parent, false);
            return new productViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@android.support.annotation.NonNull productViewHolder holder, int position, @android.support.annotation.NonNull ProductItemsConstructor model) {
            holder.Heading(model.getHeading());
            holder.Price(model.getPrice());
            holder.Prates(model.getPprice());
            holder.Rating(model.getRating());
            holder.setImg(getApplicationContext(), model.getImageUrl());
        }
    };
    recyclerView.setAdapter(FirebaseAdapter);
    FirebaseAdapter.notifyDataSetChanged();
}
邵飞宇
2023-03-14

请尝试使用以下代码:

public class product_page extends AppCompatActivity {


FirebaseAuth mAuth;
String s, product;
ProgressDialog progressDialog;
private RecyclerView recyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
DatabaseReference db;
FirebaseRecyclerAdapter<ProductItemsConstructor, productViewHolder> FirebaseAdapter;

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

        Intent intent = getIntent();
        s = intent.getExtras().getString("pos");
        product = intent.getExtras().getString("product");

    mAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();

    RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
   // RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());


    Query query = firebaseDatabase.getReference().child("Products").child(s).child(product).orderByKey();

    FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<ProductItemsConstructor>().setQuery(query,ProductItemsConstructor.class).build();

    FirebaseAdapter = new FirebaseRecyclerAdapter<ProductItemsConstructor, productViewHolder>(options) {


        @Override
        public productViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items,parent,false);
            return new productViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull productViewHolder holder, int position, @NonNull ProductItemsConstructor model) {
            holder.Heading(model.getHeading());
            holder.Price(model.getPrice());
            holder.Prates(model.getPprice());
            holder.Rating(model.getRating());
            holder.setImg(getApplicationContext(),model.getImageUrl());
        }
    };
    recyclerView.setAdapter(FirebaseAdapter);

}


@Override
protected void onStart() {
    super.onStart();

    FirebaseAdapter.startListening();
    recyclerView.setAdapter(FirebaseAdapter);
}

@Override
protected void onDestroy() {
    FirebaseAdapter.stopListening();
    super.onDestroy();
}

@Override
protected void onResume() {
    super.onResume();

    FirebaseAdapter.notifyDataSetChanged();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent in = new Intent(product_page.this, MainActivity.class);
    startActivity(in);
}

public class productViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView head, rate, Prate, rating;
    ImageView img;


    public productViewHolder(final View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
    }

    private void Heading(String heading) {
        head = (TextView) itemView.findViewById(R.id.Pheading);
        head.setText(heading);
    }

    private void Price(String price) {
        rate = (TextView) itemView.findViewById(R.id.Pprice);
        rate.setText(price);
    }

    private void Prates(String prate){
        Prate = (TextView)itemView.findViewById(R.id.pRate);
        Prate.setText(prate);
    }

    private void Rating(String rting){
        rating = (TextView)itemView.findViewById(R.id.Rating);
        rating.setText(rting);
    }

    private void setImg(Context ctx, String img1) {
        img = (ImageView) itemView.findViewById(R.id.PImage);
        Picasso.with(ctx).load(img1).placeholder(R.drawable.notification).into(img);
        // progressDialog.dismiss();
    }
 类似资料:
  • 我试过使用descendantFocusability=“blocksdescendents”,但这不起作用。 这是我的XML代码,可以在其中找到RecyclerView 用于向RecyclersView添加数据的代码是 所有应用程序都工作得很好,唯一的问题是滚动行为。当我在HIM中添加新数据时,我希望滚动位置不会出现在RecyclersView列表的顶部。

  • 我有一个,当用户浏览应用程序时,我将当前片段替换为用户选择的片段,并添加将交易添加到背包。一切正常,用户可以通过按下返回按钮返回到以前的片段。 当设备方向改变时会出现问题:假设用户看到的第一个片段是A,然后他导航到B,并从B导航到C。在横向模式下,可以更好地欣赏C中的内容,并且C有一个特殊的横向布局,因此用户旋转设备。我希望用户使用C语言进行新的定位。相反,用户看到的是景观。现在他需要再次从A导航

  • 是否有一种方法可以在完成刷取并对实例调用之后恢复刷取操作并将视图保持器还原到其初始位置?我使用了、和实例来完美地协同工作,在某些情况下,我只需要恢复swipe操作,而不删除被刷过的项。

  • 我是SpringBoot的新手,正在寻找一种在endpoint超过3秒才能返回响应时超时的方法。我尝试添加属性“server.servlet.session.timeout”,但仍然没有运气。如何做到这一点?谢谢。 Application.properties

  • 我正在尝试如何暂停一首确定的歌曲,存储它的内容,这样就可以加载另一首歌曲,播放新歌曲,恢复第一首歌曲中保存的内容并继续它。 有更好的办法吗? 谢了。

  • 我正在使用JdbcTemplate。query(sql、args、rowMapper)方法调用以返回对象列表。在某些情况下,我希望跳过一行,而不将其添加到返回的列表中。在这些情况下,我想到了两种解决方案: 让行映射器返回null 让RowMapper抛出一个异常(我知道SQLExceptions已被处理,所以这是一种可能性) 我的问题是:当返回null时,JdbcTemboard是否将其添加到列表