How to Split FirebaseRecyclerOptions view for every user who logs in when they want to order an item, because all other user's order data shows up? [enter image description here][1]
1. MenuProses.java
package com.example.al_hafiz.User;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.al_hafiz.Common.NetworkChangeListener;
import com.example.al_hafiz.Common.StartUpScreen;
import com.example.al_hafiz.Database.PesanHelperClass;
import com.example.al_hafiz.Database.SessionManager;
import com.example.al_hafiz.Database.UserHelperClass;
import com.example.al_hafiz.HelperClasses.ProsesAdapter;
import com.example.al_hafiz.R;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.database.FirebaseDatabase;
public class MenuProses extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//variable network
NetworkChangeListener networkChangeListener = new NetworkChangeListener();
static final float END_SCALE = 0.7f;
//Draw menu
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
LinearLayout contentView;
TextView Username;
TextView nama, email, sandi, tgl, hp, kelamin;
//RecyclerView
RecyclerView recyclerView;
ProsesAdapter prosesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_menu_proses);
//RecyclerView
recyclerView = findViewById(R.id.recyclerviewproses);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<PesanHelperClass> options =
new FirebaseRecyclerOptions.Builder<PesanHelperClass>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Order"), PesanHelperClass.class)
.build();
prosesAdapter = new ProsesAdapter(options);
recyclerView.setAdapter(prosesAdapter);
//menu hooks
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
contentView = findViewById(R.id.content);
nama = findViewById(R.id.full_name_profile);
email = findViewById(R.id.email_profile);
Username = findViewById(R.id.username_field);
tgl = findViewById(R.id.ttl_profile);
hp = findViewById(R.id.phone_profile);
kelamin = findViewById(R.id.gender_profile);
sandi = findViewById(R.id.sandi_profile);
//toolbar
setSupportActionBar(toolbar);
//navigation menu
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navi_drawer_open, R.string.navi_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.bringToFront();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.Home);
animateNavigationDrawed();
BottomNavigationView bottom = findViewById(R.id.navigasi_bawah);
bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.Konfirmasi:
break;
case R.id.Packing:
Intent menupaking = new Intent(getApplicationContext(), MenuPacking.class);
startActivity(menupaking);
finish();
break;
case R.id.Kirim:
Intent menukirim = new Intent(getApplicationContext(), MenuPacking.class);
startActivity(menukirim);
finish();
break;
}
return true;
}
});
}
private void animateNavigationDrawed() {
drawerLayout.setScrimColor(getResources().getColor(R.color.colorAccent));
drawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
final float diffScaledOfset = slideOffset * (1 - END_SCALE);
final float offsetScale = 1 - diffScaledOfset;
contentView.setScaleX(offsetScale);
contentView.setScaleY(offsetScale);
final float xOffset = drawerView.getWidth() * slideOffset;
final float xOffsetDiff = contentView.getWidth() * diffScaledOfset / 2;
final float xTranslation = xOffset - xOffsetDiff;
contentView.setTranslationX(xTranslation);
}
});
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.Home:
Intent home = new Intent(getApplicationContext(), UserDashboard.class);
startActivity(home);
finish();
break;
case R.id.order1:
Intent order = new Intent(getApplicationContext(), MenuOrder.class);
startActivity(order);
finish();
break;
case R.id.riwayat1:
Intent riwayat = new Intent(getApplicationContext(), Riwayat.class);
startActivity(riwayat);
finish();
break;
case R.id.proses1:
break;
case R.id.profil1:
Intent profil = new Intent(getApplicationContext(), Profil_User.class);
startActivity(profil);
finish();
break;
case R.id.ganti_password1:
Intent password = new Intent(getApplicationContext(), GantiPassword.class);
startActivity(password);
finish();
break;
case R.id.logout1:
logout();
break;
case R.id.sahre1:
Toast.makeText(this, "Telah Dibagikan", Toast.LENGTH_SHORT).show();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
private void logout() {
SessionManager sessionManager = new SessionManager(MenuProses.this, SessionManager.SESSION_USERSESSION);
sessionManager.logoutFromSession();
startActivity(new Intent(getApplicationContext(), StartUpScreen.class));
finish();
Toast.makeText(MenuProses.this, "Behasil Logout !", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkChangeListener, filter);
super.onStart();
prosesAdapter.startListening();
}
@Override
protected void onStop() {
unregisterReceiver(networkChangeListener);
super.onStop();
prosesAdapter.stopListening();
}
}
2. AdapterProses
package com.example.al_hafiz.HelperClasses;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.al_hafiz.Database.PesanHelperClass;
import com.example.al_hafiz.R;
import com.example.al_hafiz.User.MenuProses;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.FirebaseDatabase;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ViewHolder;
import java.util.HashMap;
import java.util.Map;
public class ProsesAdapter extends FirebaseRecyclerAdapter<PesanHelperClass, ProsesAdapter.myViewHolder> {
/**
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
* {@link FirebaseRecyclerOptions} for configuration options.
*
* @param options
*/
public ProsesAdapter(@NonNull FirebaseRecyclerOptions<PesanHelperClass> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull myViewHolder holder, @SuppressLint("RecyclerView") final int position, @NonNull PesanHelperClass model) {
holder.nama.setText(model.getNamaorder());
holder.alamat.setText(model.getAlamatorder());
holder.telepon.setText(model.getTeleponorder());
holder.jumlah.setText(model.getJumlahorder());
holder.bayar.setText(model.getPilihpembayaran());
holder.btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DialogPlus dialogPlus = DialogPlus.newDialog(holder.btnEdit.getContext())
.setContentHolder(new ViewHolder(R.layout.update_popup))
.setExpanded(true, 1300)
.create();
View view = dialogPlus.getHolderView();
EditText nama = view.findViewById(R.id.namaproses_update);
EditText alamat = view.findViewById(R.id.alamatproses_update);
EditText telepon = view.findViewById(R.id.teleponproses_update);
EditText jumlah = view.findViewById(R.id.jumlahproses_update);
EditText bayar = view.findViewById(R.id.bayarproses_update);
Button btnupdate = view.findViewById(R.id.btn_update_pro);
nama.setText(model.getNamaorder());
alamat.setText(model.getAlamatorder());
telepon.setText(model.getTeleponorder());
jumlah.setText(model.getJumlahorder());
bayar.setText(model.getPilihpembayaran());
dialogPlus.show();
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> map = new HashMap<>();
map.put("namaorder", nama.getText().toString());
map.put("alamatorder", alamat.getText().toString());
map.put("teleponorder", telepon.getText().toString());
map.put("jumlahorder", jumlah.getText().toString());
map.put("pilihpembayaran", bayar.getText().toString());
FirebaseDatabase.getInstance().getReference().child("Order")
.child(getRef(position).getKey()).updateChildren(map)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(holder.nama.getContext(), "Data Berhasil Di Update", Toast.LENGTH_SHORT).show();
dialogPlus.dismiss();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(holder.nama.getContext(), "Data Gagal DiUpdate", Toast.LENGTH_SHORT).show();
dialogPlus.dismiss();
}
});
}
});
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(holder.nama.getContext());
builder.setTitle("Apakah Anda Yakin ?");
builder.setMessage("Data Yang Telah Terhapus Tidak Bisa Kembali Lagi.");
builder.setPositiveButton("Hapus", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FirebaseDatabase.getInstance().getReference().child("Order")
.child(getRef(position).getKey()).removeValue();
}
});
builder.setNegativeButton("Batal", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(holder.nama.getContext(), "Data Batal Dihapus", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
});
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_menu_proses_item, parent, false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder {
TextView nama, alamat, telepon, jumlah, bayar;
Button btnEdit, btnDelete;
public myViewHolder(@NonNull View itemView) {
super(itemView);
nama = itemView.findViewById(R.id.namaproses);
alamat = itemView.findViewById(R.id.alamatproses);
telepon = itemView.findViewById(R.id.telpproses);
jumlah = itemView.findViewById(R.id.jmlorder);
bayar = itemView.findViewById(R.id.cashorinvoice);
btnEdit = (Button) itemView.findViewById(R.id.button_edit_proses);
btnDelete = (Button) itemView.findViewById(R.id.button_delete_proses);
}
}
}
3. UserHelperClass
package com.example.al_hafiz.Database;
public class UserHelperClass {
String fullName, username, email, password, gender, date, phoneNo;
public UserHelperClass() {
}
public UserHelperClass(String fullName, String email, String username, String password, String date, String gender, String phoneNo) {
this.fullName = fullName;
this.username = username;
this.email = email;
this.password = password;
this.gender = gender;
this.date = date;
this.phoneNo = phoneNo;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
}
[enter link description here][2]
[1]: https://i.stack.imgur.com/sijuX.png picture view project
CodePudding user response:
You're currently loading all child nodes of /Order
into the recycler adapter here:
FirebaseRecyclerOptions<PesanHelperClass> options =
new FirebaseRecyclerOptions.Builder<PesanHelperClass>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Order"), PesanHelperClass.class)
.build();
prosesAdapter = new ProsesAdapter(options);
If each of these child nodes has a property that identifies the user the order belongs to, you can use a query to get only the orders for a specific user.
So for example, if you use Firebase Authentication and store the UID of the user in a property uid
of each order, you could get only the orders for the current user with:
if (FirebaseAuth.getInstance().getCurrentUser() <> null) {
//