简体   繁体   中英

main activity reads the data again from the firebase database after coming back to the mainactivity

this is my MainActivity

private DatabaseReference mDatabaseReference;
private RecyclerView recyclerView;
private PlaceRecyclerAdapter placeRecyclerAdapter;
private List<Places> placesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Places");

    placesList = new ArrayList<>();

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu,menu);
    return super.onCreateOptionsMenu(menu);

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.action_add)
    {
        startActivity(new Intent(MainActivity.this,AddPostActivity.class));
        finish();
    }
    return super.onOptionsItemSelected(item);

}

@Override
protected void onStart() {
    super.onStart();
    mDatabaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Places places = dataSnapshot.getValue(Places.class);
            placesList.add(places);
            placeRecyclerAdapter = new PlaceRecyclerAdapter(MainActivity.this,placesList);
            recyclerView.setAdapter(placeRecyclerAdapter);
            placeRecyclerAdapter.notifyDataSetChanged();
        }

I am using this RecyclerAdapter to load cardview cards in the main activity

 public PlaceRecyclerAdapter(Context context, List<Places> placesList) {
    this.context = context;
    this.placesList = placesList;
}

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

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Places places = placesList.get(position);
    //String imageUrl= null;

    holder.place.setText(places.getPlace());
    holder.desc.setText(places.getDesc());
    //imageUrl= places.getImage();

    //todo: Use piccasso library to load images
    //Picasso.with(context).load(imageUrl).into(holder.image);


}

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


public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView place;
    public TextView desc;
    //public ImageView image;

    public ViewHolder(View view) {
        super(view);

        place = (TextView) view.findViewById(R.id.postTitleList);
        desc = (TextView) view.findViewById(R.id.postDescList);
        //image = (ImageView) view.findViewById(R.id.postImageList);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                int pos = getAdapterPosition();
                if (pos != RecyclerView.NO_POSITION) {
                    Places clickedDataItem = placesList.get(pos);
                    //Toast.makeText(v.getContext(), "You clicked " + clickedDataItem.getPlace(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, Details.class);
                    intent.putExtra("NAME", clickedDataItem.getPlace());
                    intent.putExtra("DESC", clickedDataItem.getDesc());
                    intent.putExtra("IMG", clickedDataItem.getImage());
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(intent);

                }
            }

and here is my Details activity

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    dPlace = (TextView) findViewById(R.id.detail_title);
    dDesc = (TextView) findViewById(R.id.detail_desc);
    dImage = (ImageView) findViewById(R.id.detail_image);
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String name = bundle.getString("NAME");
        String desc = bundle.getString("DESC");
        String img = bundle.getString("IMG");
        dPlace.setText(name);
        dDesc.setText(desc);
        Picasso.with(this).load(img).into(dImage);

now, clicking on a item in MainActivity I am able to go to the Details activity. suppose there are 3 items in database, and at first main activity shows only 3 items. but after going to Details activity, and then coming back to main activity, there are 6 items, the earlier 3 items are repeated. and if again I go to the Details activity and come back, there will be 9 items. I used (Activity)context).finish(); in RecyclerViewAdapter to finish the main activity, but I think it finishes the context from which I am able to get the details. please help. Sorry for my bad english.

Your firebase loading data items needs to go inside onCreate() as it will only gets called only once if its on backstack an onStart() will get called twice. So just implement the data item loading logic in onCreate instead of onStart()

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        mDatabaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Places places = dataSnapshot.getValue(Places.class);
                placesList.add(places);
                placeRecyclerAdapter = new PlaceRecyclerAdapter(MainActivity.this,placesList);
                recyclerView.setAdapter(placeRecyclerAdapter);
                placeRecyclerAdapter.notifyDataSetChanged();
            }
    }

Update

placesList.clear();
placesList.add(places);

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