Я создал приложение, в котором я извлек несколько пользователей из базы данных, в качестве базы данных я использую firebase. Я хочу перенести данные из моего вида карты в другой вид деятельности, если щелкнуть вид карты. Когда я нажимаю на представление карты, я получаю все данные о пользователях из базы данных.
Это пользователи
Это данные внутри пользователя
Ниже приведен код вида переработчика:
public class Search_Profile extends AppCompatActivity {
DatabaseReference databaseReference;
Firebase firebase;
FirebaseAuth mAuth;
ProgressDialog progressDialog;
List<User> list = new ArrayList<User>();
RecyclerView recyclerView;
FirebaseUser firebaseUser;
RecyclerView.Adapter adapter ;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_detail);
b1=(Button)findViewById(R.id.viewProfile);
mAuth = FirebaseAuth.getInstance();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(Search_Profile.this));
progressDialog = new ProgressDialog(Search_Profile.this);
progressDialog.setMessage("Loading Data from Firebase Database");
progressDialog.show();
databaseReference = FirebaseDatabase.getInstance().getReference().child(Dashboard.Database_Path);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
User user = dataSnapshot.getValue(User.class);
assert user != null;
System.out.println(user.getFirst_name()+" "+user.getLast_name());
// System.out.println();
System.out.println(user.getDate());
System.out.println(user.getUser_id());
System.out.println(user.getHeight());
System.out.println(user.getHighest_education());
System.out.println(user.getOccupation());
list.add(user);
}
adapter = new RecyclerViewAdapter(Search_Profile.this,list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
}
Это код адаптера вида Recycler:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
DatabaseReference databaseReference;
Firebase firebase;
FirebaseAuth mAuth;
Context context;
List<User> MainImageUploadInfoList;
RecyclerViewAdapter(Context context, List<User> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final User user = MainImageUploadInfoList.get(position);
holder.getLayoutPosition();
holder.mCardView.setTag(position);
holder.FirstNameTextView.setText(user.getFirst_name() + " " + user.getLast_name());
holder.DateTextView.setText(user.getDate());
holder.HeightTextView.setText(user.getHeight());
holder.EducationTextView.setText(user.getHighest_education());
holder.OccupationTextView.setText(user.getOccupation());
holder.UserIDTextView.setText(user.getUser_id());
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
User user = new User();
public TextView FirstNameTextView;
public TextView DateTextView;
public TextView HeightTextView;
public TextView EducationTextView;
public TextView OccupationTextView;
public TextView UserIDTextView;
public Button ViewProfile;
public View mCardView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mCardView = (CardView) itemView.findViewById(R.id.cardview1);
FirstNameTextView = (TextView) itemView.findViewById(R.id.textView1);
DateTextView = (TextView) itemView.findViewById(R.id.textView3);
HeightTextView = (TextView) itemView.findViewById(R.id.textView4);
EducationTextView = (TextView) itemView.findViewById(R.id.textView5);
OccupationTextView = (TextView) itemView.findViewById(R.id.textView6);
UserIDTextView = (TextView) itemView.findViewById(R.id.textView7);
ViewProfile = (Button) itemView.findViewById(R.id.viewProfile);
mCardView.setOnClickListener(this);
}
@Override
public void onClick(final View v) {
final int position = (int) itemView.getTag();
Toast.makeText(itemView.getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
databaseReference = FirebaseDatabase.getInstance().getReference().child(Dashboard.Database_Path);
final Intent it = new Intent (v.getContext(), ViewProfile.class);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = null;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
user = snapshot.getValue(User.class);
if (getLayoutPosition() == 0) {
it.putExtra("first_name", user.getFirst_name());
it.putExtra("last_name", user.getLast_name());
it.putExtra("date", user.getDate());
it.putExtra("height", user.getHeight());
it.putExtra("city_state", user.getCity_state());
it.putExtra("hobbies", user.getHobbies());
it.putExtra("highest_education", user.getHighest_education());
it.putExtra("occupation", user.getOccupation());
it.putExtra("income", user.getIncome());
it.putExtra("marital_status", user.getMarital_status());
it.putExtra("family_members", user.getFamily_members());
it.putExtra("fathers_name", user.getFathers_name());
it.putExtra("mothers_name", user.getMothers_name());
it.putExtra("fathers_occupation", user.getFathers_occupation());
it.putExtra("mothers_occupation", user.getMothers_occupation());
it.putExtra("user_id", user.getUser_id());
it.putExtra("positionGroup", position);
context.startActivity(it);
} else if (getLayoutPosition() == 1) {
it.putExtra("first_name", user.getFirst_name());
it.putExtra("last_name", user.getLast_name());
it.putExtra("date", user.getDate());
it.putExtra("height", user.getHeight());
it.putExtra("city_state", user.getCity_state());
it.putExtra("hobbies", user.getHobbies());
it.putExtra("highest_education", user.getHighest_education());
it.putExtra("occupation", user.getOccupation());
it.putExtra("income", user.getIncome());
it.putExtra("marital_status", user.getMarital_status());
it.putExtra("family_members", user.getFamily_members());
it.putExtra("fathers_name", user.getFathers_name());
it.putExtra("mothers_name", user.getMothers_name());
it.putExtra("fathers_occupation", user.getFathers_occupation());
it.putExtra("mothers_occupation", user.getMothers_occupation());
it.putExtra("user_id", user.getUser_id());
it.putExtra("positionGroup", position);
context.startActivity(it);
} else if (getLayoutPosition() == 2) {
// Bundle dataBundle = new Bundle();
it.putExtra("first_name", user.getFirst_name());
it.putExtra("last_name", user.getLast_name());
it.putExtra("date", user.getDate());
it.putExtra("height", user.getHeight());
it.putExtra("city_state", user.getCity_state());
it.putExtra("hobbies", user.getHobbies());
it.putExtra("highest_education", user.getHighest_education());
it.putExtra("occupation", user.getOccupation());
it.putExtra("income", user.getIncome());
it.putExtra("marital_status", user.getMarital_status());
it.putExtra("family_members", user.getFamily_members());
it.putExtra("fathers_name", user.getFathers_name());
it.putExtra("mothers_name", user.getMothers_name());
it.putExtra("fathers_occupation", user.getFathers_occupation());
it.putExtra("mothers_occupation", user.getMothers_occupation());
it.putExtra("user_id", user.getUser_id());
it.putExtra("positionGroup", position);
context.startActivity(it);
} else if (getLayoutPosition() == 3) {
} else if (getLayoutPosition() == 4) {
} else if (getLayoutPosition() == 5) {
}
//or you can use For loop if you have long list of items. Use its length or size of the list as
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
}
Это код следующего действия:
public class ViewProfile extends AppCompatActivity {
String user_id;
ImageView imageView;
TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8,tv9,tv10,tv11,tv12,tv13,tv14,tv15,tv16;
FirebaseAuth mAuth;
//DatabaseReference uidRef;
FirebaseDatabase database;
User user=new User();
DatabaseReference reference;
public static final String Firebase_Server_URL = "https://baghbanshadi-25553.firebaseio.com/User/";
Firebase firebase;
public static final String Database_Path = "User";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_profile);
imageView = findViewById(R.id.imageView4);
tv1 = findViewById(R.id.textView14);
tv2 = findViewById(R.id.textView15);
tv3 = findViewById(R.id.textView16);
tv4 = findViewById(R.id.textView17);
tv5 = findViewById(R.id.textView18);
tv6 = findViewById(R.id.textView19);
tv7 = findViewById(R.id.textView20);
tv8 = findViewById(R.id.textView21);
tv9 = findViewById(R.id.textView22);
tv10 = findViewById(R.id.textView23);
tv11 = findViewById(R.id.textView24);
tv12 = findViewById(R.id.textView25);
tv13 = findViewById(R.id.textView26);
tv14 = findViewById(R.id.textView27);
tv15 = findViewById(R.id.textView28);
tv16 = findViewById(R.id.textView29);
String first_name = getIntent().getExtras().getString("first_name");
String last_name = getIntent().getExtras().getString("last_name");
String date= getIntent().getExtras().getString("date");
String user_id= getIntent().getExtras().getString("user_id");
String height= getIntent().getExtras().getString("height");
String highest_education= getIntent().getExtras().getString("highest_education");
String occupation= getIntent().getExtras().getString("occupation");
String city_state= getIntent().getExtras().getString("city_state");
String hobbies= getIntent().getExtras().getString("hobbies");
String income= getIntent().getExtras().getString("income");
String marital_status= getIntent().getExtras().getString("marital_status");
String family_members= getIntent().getExtras().getString("family_members");
String fathers_name= getIntent().getExtras().getString("fathers_name");
String mothers_name= getIntent().getExtras().getString("mothers_name");
String fathers_occupation= getIntent().getExtras().getString("fathers_occupation");
String mothers_occupation= getIntent().getExtras().getString("mothers_occupation");
tv1.setText(first_name);
tv2.setText(last_name);
tv3.setText(date);
tv4.setText(user_id);
tv5.setText(height);
tv6.setText(highest_education);
tv7.setText(occupation);
tv8.setText(city_state);
tv9.setText(hobbies);
tv10.setText(income);
tv11.setText(marital_status);
tv12.setText(family_members);
tv13.setText(fathers_name);
tv14.setText(mothers_name);
tv15.setText(fathers_occupation);
tv16.setText(mothers_occupation);
}
}
Я хочу, чтобы один пользователь просматривал одну карту, и я получаю несколько пользователей для каждого отдельного просмотра карты. Что мне делать?