我正在开发一个婚姻应用程序,其中已在recyclerview的cardview中检索了所有用户数据。每张卡上都有一个按钮,该按钮将显示您单击过的卡的那个人的完整个人资料。但是我正在该页面上获取所有用户数据。我该怎么做才能解决这个问题?
这是recyclerview的代码:
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();
}
});
}
}
这是Recyclerview适配器的代码:
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, int position) {
final User user = MainImageUploadInfoList.get(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.ViewProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
databaseReference = FirebaseDatabase.getInstance().getReference().child(Dashboard.Database_Path);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (user.getUser_id()!=null){
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
Intent it = new Intent(v.getContext(), ViewProfile.class);
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());
context.startActivity(it);
} }}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
@Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView FirstNameTextView;
//public TextView LastNameTextView;
public TextView DateTextView;
public TextView HeightTextView;
public TextView EducationTextView;
public TextView OccupationTextView;
public TextView UserIDTextView;
public Button ViewProfile;
public ViewHolder(@NonNull View itemView) {
super(itemView);
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);
}
}
}
这是视图配置文件的代码:
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 = (ImageView) findViewById(R.id.imageView4);
tv1 = (TextView) findViewById(R.id.textView14);
tv2 = (TextView) findViewById(R.id.textView15);
tv3 = (TextView) findViewById(R.id.textView16);
tv4 = (TextView) findViewById(R.id.textView17);
tv5 = (TextView) findViewById(R.id.textView18);
tv6 = (TextView) findViewById(R.id.textView19);
tv7 = (TextView) findViewById(R.id.textView20);
tv8 = (TextView) findViewById(R.id.textView21);
tv9 = (TextView) findViewById(R.id.textView22);
tv10 = (TextView) findViewById(R.id.textView23);
tv11 = (TextView) findViewById(R.id.textView24);
tv12 = (TextView) findViewById(R.id.textView25);
tv13 = (TextView) findViewById(R.id.textView26);
tv14 = (TextView) findViewById(R.id.textView27);
tv15 = (TextView) findViewById(R.id.textView28);
tv16 = (TextView) findViewById(R.id.textView29);
String first_name = getIntent().getExtras().getString("first_name");
tv1.setText(first_name);
String last_name = getIntent().getExtras().getString("last_name");
tv2.setText(last_name);
String date= getIntent().getExtras().getString("date");
tv3.setText(date);
String user_id= getIntent().getExtras().getString("user_id");
tv4.setText(user_id);
String height= getIntent().getExtras().getString("height");
tv5.setText(height);
String highest_education= getIntent().getExtras().getString("highest_education");
tv6.setText(highest_education);
String occupation= getIntent().getExtras().getString("occupation");
tv7.setText(occupation);
String city_state= getIntent().getExtras().getString("city_state");
tv8.setText(city_state);
String hobbies= getIntent().getExtras().getString("hobbies");
tv9.setText(hobbies);
String income= getIntent().getExtras().getString("income");
tv10.setText(income);
String marital_status= getIntent().getExtras().getString("marital_status");
tv11.setText(marital_status);
String family_members= getIntent().getExtras().getString("family_members");
tv12.setText(family_members);
String fathers_name= getIntent().getExtras().getString("fathers_name");
tv13.setText(fathers_name);
String mothers_name= getIntent().getExtras().getString("mothers_name");
tv14.setText(mothers_name);
String fathers_occupation= getIntent().getExtras().getString("fathers_occupation");
tv15.setText(fathers_occupation);
String mothers_occupation= getIntent().getExtras().getString("mothers_occupation");
tv16.setText(mothers_occupation);
}
}
答案 0 :(得分:1)
是的,您可以通过获取当前位置来实现
adapter.get(position);