Używam RecyclerView, aby pokazać dane, które są pobierane z bazy danych czasu rzeczywistego Firebase. Kiedy otwieram działanie widoku recyklera, nic nie pokazuje, ale kiedy przechodzę do poprzedniej czynności, przyjdź ponownie, to pokazuje.

TO JEST CZYNNOŚĆ, W KTÓREJ DANE MUSZĄ BYĆ WYKAZANE-

   public class BrakesItemActivity extends AppCompatActivity {
   private RecyclerView recyclerView;
  private FloatingActionButton floatingActionButton;
    private FirebaseDatabase database;
    private DatabaseReference BrakestransactionDb, brakesDb;
   private FirebaseRecyclerOptions<Transaction> options;
    private FirebaseRecyclerAdapter<Transaction, TransactionViewHolder> adapter;
   int sumofbrakescost = 0;
   String sumofbrakescostvalue;

BottomAppBar bottomAppBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_last2);
    bottomAppBar = findViewById(R.id.bottom_app_bar);
    recyclerView = findViewById(R.id.recycler_view);
    floatingActionButton = findViewById(R.id.floating_button);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

    setSupportActionBar(bottomAppBar);

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(BrakesItemActivity.this, BInputActivity.class);
            startActivity(intent);
        }
    });

    database = FirebaseDatabase.getInstance();
    BrakestransactionDb = database.getReference().child("Brakes Items Transaction");

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(BrakesItemActivity.this));

    BrakestransactionDb.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                Map<String, Object> map = (Map<String, Object>) ds.getValue();
                Object totalcost = map.get("totalcoststring");
                int totalcostvalue = Integer.parseInt(String.valueOf(totalcost));
                sumofbrakescost += totalcostvalue;
                sumofbrakescostvalue = String.valueOf(sumofbrakescost);

            }
            brakesDb = database.getReference().child("Department").child("Brakes").child("Cost");
            brakesDb.setValue(sumofbrakescostvalue);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
    showTask();

}

private void showTask() {
    options = new FirebaseRecyclerOptions.Builder<Transaction>().setQuery(BrakestransactionDb, Transaction.class).build();
    adapter = new FirebaseRecyclerAdapter<Transaction, TransactionViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull TransactionViewHolder holder, int position, @NonNull Transaction model) {
            holder.item_name_card.setText(model.getNameofitemstring());
            holder.bought_by_card.setText(model.getBoughtbystring());
            holder.bought_from_card.setText(model.getBoughtfromstring());
            holder.date_card.setText(model.getDatestring());
            holder.time_card.setText(model.getTimestring());
            holder.cost_of_one_item_card.setText(model.getCostofoneitemstring());
            holder.number_of_items_card.setText(model.getNumberofitemstring());
            holder.total_cost_card.setText(model.getTotalcoststring());

        }

        @NonNull
        @Override
        public TransactionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.transaction_row, parent, false);
            return new TransactionViewHolder(itemView);
        }
    };
    recyclerView.setAdapter(adapter);
}

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

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

@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
    if (item.getTitle().equals("Delete")) {
        deleteTransaction(adapter.getRef(item.getOrder()).getKey());
    }

    return super.onContextItemSelected(item);
}

private void deleteTransaction(String key) {
    BrakestransactionDb.child(key).removeValue();
}

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

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == R.id.delete_all) {
        BrakestransactionDb.removeValue();
    }
    if (item.getItemId() == R.id.home_button) {
        Intent intent = new Intent(this, NavActivity.class);
        finish();
        startActivity(intent);
        if (item.getItemId() == R.id.back_item_activity) {
            Intent intent1 = new Intent(this, Department.class);
            finish();
            startActivity(intent1);
        }
    }
    return super.onOptionsItemSelected(item);
}

}


TO JEST MODEL WYKONANY PRZEZ KLASĘ I-

package com.example.android.zfr.Model;

 public class Transaction {
private String nameofitemstring,boughtbystring,boughtfromstring,costofoneitemstring,datestring,
        timestring,totalcoststring,numberofitemstring;

public String getNameofitemstring() { return nameofitemstring; }

public void setNameofitemstring(String nameofitemstring) { this.nameofitemstring = nameofitemstring; }

public String getBoughtbystring() {return boughtbystring; }

public void setBoughtbystring(String bought string) {
    this.boughtbystring = boughtbystring;
}

public String getBoughtfromstring() { return boughtfromstring; }

public void setBoughtfromstring(String boughtfromstring) { this.boughtfromstring = boughtfromstring; }

public String getCostofoneitemstring() {
    return costofoneitemstring;
}

public void setCostofoneitemstring(String costofoneitemstring) { this.costofoneitemstring = costofoneitemstring; }

public String getDatestring() { return datestring; }

public void setDatestring(String datestring) { this.datestring = datestring; }

public String getTimestring() { return timestring; }

public void setTimestring(String timestring) { this.timestring = timestring; }

public String getTotalcoststring() { return totalcoststring; }

public void setTotalcoststring(String totalcoststring) { this.totalcoststring = totalcoststring; }

public String getNumberofitemstring() { return numberofitemstring; }

public void setNumberofitemstring(String numberofitemstring) { this.numberofitemstring = numberofitemstring; }

public Transaction() { }

public Transaction(String nameofitemstring,String boughtbystring,String boughtfromstring,String costofoneitemstring, String timestring,
                 String datestring ,String totalcoststring , String numberofitemstring ){
    this.nameofitemstring=nameofitemstring;
    this.boughtbystring=boughtbystring;
    this.boughtfromstring=boughtfromstring;
    this.costofoneitemstring=costofoneitemstring;
    this.datestring=datestring;
    this.timestring=timestring;
    this.numberofitemstring=numberofitemstring;
    this.totalcoststring=totalcoststring;
}

}


TO KLASA WIDZÓW-

 package com.example.android.zfr.ViewHolder;

   import android.view.ContextMenu;
import android.view.View;
import android.widget.TextView;

 import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.android.zfr.R;
 import com.facebook.shimmer.ShimmerFrameLayout;

 public class TransactionViewHolder extends RecyclerView.ViewHolder implements 
View.OnCreateContextMenuListener{

public TextView item_name_card,bought_by_card,bought_from_card,date_card,
        time_card,cost_of_one_item_card,number_of_items_card,total_cost_card;

public TransactionViewHolder(@NonNull View itemView) {
    super(itemView);
    item_name_card=itemView.findViewById(R.id.item_name_card);
    bought_by_card=itemView.findViewById(R.id.bought_by_card);
    bought_from_card=itemView.findViewById(R.id.bought_from_card);
    date_card=itemView.findViewById(R.id.date_card);
    time_card=itemView.findViewById(R.id.time_card);
    cost_of_one_item_card=itemView.findViewById(R.id.cost_of_one_item_card);
    number_of_items_card=itemView.findViewById(R.id.number_of_items_card);
    total_cost_card=itemView.findViewById(R.id.total_cost_card);

itemView.setOnCreateContextMenuListener(this);
}
@Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) 
 {  menu.add(0,0,getAdapterPosition(),"Delete");  }
}

Proszę pomóż.

0
sharib ahmed 16 marzec 2020, 23:34

2 odpowiedzi

Najlepsza odpowiedź

To działało przez umieszczenie

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(BrakesItemActivity.this));

W metodzie onDatachange w następujący sposób:

 BrakestransactionDb.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            Map<String, Object> map = (Map<String, Object>) ds.getValue();
            Object totalcost = map.get("totalcoststring");
            int totalcostvalue = Integer.parseInt(String.valueOf(totalcost));
            sumofbrakescost += totalcostvalue;
            sumofbrakescostvalue = String.valueOf(sumofbrakescost);

        }
        brakesDb = database.getReference().child("Department").child("Brakes").child("Cost");
        brakesDb.setValue(sumofbrakescostvalue);

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(BrakesItemActivity.this));
    }
0
sharib ahmed 17 marzec 2020, 01:51

Musisz zdać sobie sprawę, że Firebase jest asynchroniczny, co oznacza, że funkcja showTask () jest wywoływana, zanim Twoja baza danych będzie w stanie przesłać Ci dane. Aby uniknąć tego problemu, musisz umieścić swoją showTask () wewnątrz addValueEventListener. Spróbuj tego:

BrakestransactionDb.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                Map<String, Object> map = (Map<String, Object>) ds.getValue();
                Object totalcost = map.get("totalcoststring");
                int totalcostvalue = Integer.parseInt(String.valueOf(totalcost));
                sumofbrakescost += totalcostvalue;
                sumofbrakescostvalue = String.valueOf(sumofbrakescost);

            }
            brakesDb = database.getReference().child("Department").child("Brakes").child("Cost");
            brakesDb.setValue(sumofbrakescostvalue);
            showTask();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
0
Yunus Kulyyev 16 marzec 2020, 20:59