单击 cardview 时如何将数据从 recyclerview 传输到下一个 activity
How to transfer data from recyclerview to next activity when clicked on cardview
我创建了一个应用程序,我在其中从数据库中检索了多个用户,我使用 firebase 作为我的数据库。单击卡片视图时,我想将数据从我的卡片视图传输到另一个 activity。当我点击卡片视图时,我从数据库中获取了所有用户数据。
These are the users
This the data inside user
下面是recycler视图的代码:
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) {
}
});
}
}
}
这是下一个activity的代码:
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);
}
}
我想要单个用户在单个卡片视图上,我在每个单个卡片视图上都有多个用户。我该怎么办?
创建一个接口,当您单击您的适配器项目时,然后在接口的帮助下在 activity 中发送所需的详细信息:-
这是界面:
public interface ItemClickListner {
void onClick(String itemId);
}
在您的 parent activity 上的 recyclerview 中额外设置数据;-
private ItemClickListner itemClickListner = new ItemClickListner() {
@Override
public void onClick(String itemId) {
Intent intent = new Intent(ListActivity.this, ItemDetailActivity.class);
intent.putExtra("itemId", itemId);
startActivity(intent);
}
};
并在第二个 activity 中获取详细信息:-
String id = getIntent().getStringExtra("itemId");
从 onBindViewHolder
传递 user_id
而不是 position
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.itemView.setTag(user.getUser_id());
}
然后在 onclick
中更改您的查询以仅获取相应的用户数据,而不是所有用户。
@Override
public void onClick(final View v) {
final String user_id = (String) itemView.getTag();
....
databaseReference.orderByChild("user_id").equalTo(user_id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = null;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
user = snapshot.getValue(User.class);
}
if(user != null) {
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 {
//Toast here if needed
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
我找到了我的答案并有效,下面是代码:
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.itemView.setTag(user.getUser_id());
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.income.setText(user.getIncome());
holder.city_state.setText(user.getCity_state());
holder.hobbies.setText(user.getHobbies());
holder.marital_status.setText(user.getMarital_status());
holder.family_members.setText(user.getFamily_members());
holder.mothers_name.setText(user.getMothers_name());
holder.fathers_name.setText(user.getFathers_name());
holder.fathers_occupation.setText(user.getFathers_occupation());
holder.mothers_occupation.setText(user.getMothers_occupation());
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 send_invitation,shortlist_profile;
public TextView income;
public TextView city_state;
public TextView hobbies;
public TextView marital_status;
public TextView family_members;
public TextView mothers_name;
public TextView fathers_name;
public TextView fathers_occupation;
public TextView mothers_occupation;
public CardView mCardView;
// RelativeLayout parentLayout;
public ViewHolder(@NonNull final 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);
income = (TextView) itemView.findViewById(R.id.income);
city_state = (TextView) itemView.findViewById(R.id.city_state);
hobbies = (TextView) itemView.findViewById(R.id.hobbies);
marital_status = (TextView) itemView.findViewById(R.id.marital_status);
family_members = (TextView) itemView.findViewById(R.id.family_members);
mothers_name = (TextView) itemView.findViewById(R.id.mothers_name);
fathers_name = (TextView) itemView.findViewById(R.id.fathers_name);
fathers_occupation = (TextView) itemView.findViewById(R.id.fathers_occupation);
mothers_occupation = (TextView) itemView.findViewById(R.id.mothers_occupation);
send_invitation = (Button) itemView.findViewById(R.id.sendinvitation);
shortlist_profile=(Button)itemView.findViewById(R.id.shortlistprofile);
send_invitation=(Button)itemView.findViewById(R.id.sendinvitation);
shortlist_profile=(Button)itemView.findViewById(R.id.shortlistprofile);
shortlist_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent it=new Intent(v.getContext(),Shortlisted_Profile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
Toast.makeText(itemView.getContext(), "Data added to shortlisted profile", Toast.LENGTH_SHORT).show();
context.startActivity(it);
}
});
//parentLayout=itemView.findViewById(R.id.parent);
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();
if (getLayoutPosition() == 0) {
final Intent it = new Intent(v.getContext(), ViewProfile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
it.putExtra("positionGroup", position);
context.startActivity(it);
} else if (getLayoutPosition() == 1) {
final Intent it = new Intent(v.getContext(), ViewProfile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
// it.putExtra("last_name", user.getLast_name());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
it.putExtra("positionGroup", position);
context.startActivity(it);
} else if (getLayoutPosition() == 2) {
final Intent it = new Intent(v.getContext(), ViewProfile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
// it.putExtra("last_name", user.getLast_name());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
it.putExtra("positionGroup", position);
context.startActivity(it);
}
}
}
}
>in the onBindview u can perfoam the action listner and tarnsfer the data
@Override
public void onBindViewHolder(@NonNull EnglishAdapter.ViewHolder holder, final int position) {
final EnglishItem englishItem = englishItems.get(position);
readCursorData(englishItem,holder);
holder.imageView.setImageResource(englishItem.getImageResourse());
holder.titletextview.setText(englishItem.getTitle());
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str_pos = String.valueOf(position);
Intent intent = new Intent(v.getContext(),English_Tenses_Details.class);
intent.putExtra("myKey", str_pos);
v.getContext().startActivity(intent);
}
});
}
我创建了一个应用程序,我在其中从数据库中检索了多个用户,我使用 firebase 作为我的数据库。单击卡片视图时,我想将数据从我的卡片视图传输到另一个 activity。当我点击卡片视图时,我从数据库中获取了所有用户数据。
These are the users
This the data inside user
下面是recycler视图的代码:
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) {
}
});
}
}
}
这是下一个activity的代码:
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);
}
}
我想要单个用户在单个卡片视图上,我在每个单个卡片视图上都有多个用户。我该怎么办?
创建一个接口,当您单击您的适配器项目时,然后在接口的帮助下在 activity 中发送所需的详细信息:-
这是界面:
public interface ItemClickListner {
void onClick(String itemId);
}
在您的 parent activity 上的 recyclerview 中额外设置数据;-
private ItemClickListner itemClickListner = new ItemClickListner() {
@Override
public void onClick(String itemId) {
Intent intent = new Intent(ListActivity.this, ItemDetailActivity.class);
intent.putExtra("itemId", itemId);
startActivity(intent);
}
};
并在第二个 activity 中获取详细信息:-
String id = getIntent().getStringExtra("itemId");
从 onBindViewHolder
传递 user_id
而不是 position
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.itemView.setTag(user.getUser_id());
}
然后在 onclick
中更改您的查询以仅获取相应的用户数据,而不是所有用户。
@Override
public void onClick(final View v) {
final String user_id = (String) itemView.getTag();
....
databaseReference.orderByChild("user_id").equalTo(user_id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = null;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
user = snapshot.getValue(User.class);
}
if(user != null) {
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 {
//Toast here if needed
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
我找到了我的答案并有效,下面是代码:
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.itemView.setTag(user.getUser_id());
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.income.setText(user.getIncome());
holder.city_state.setText(user.getCity_state());
holder.hobbies.setText(user.getHobbies());
holder.marital_status.setText(user.getMarital_status());
holder.family_members.setText(user.getFamily_members());
holder.mothers_name.setText(user.getMothers_name());
holder.fathers_name.setText(user.getFathers_name());
holder.fathers_occupation.setText(user.getFathers_occupation());
holder.mothers_occupation.setText(user.getMothers_occupation());
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 send_invitation,shortlist_profile;
public TextView income;
public TextView city_state;
public TextView hobbies;
public TextView marital_status;
public TextView family_members;
public TextView mothers_name;
public TextView fathers_name;
public TextView fathers_occupation;
public TextView mothers_occupation;
public CardView mCardView;
// RelativeLayout parentLayout;
public ViewHolder(@NonNull final 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);
income = (TextView) itemView.findViewById(R.id.income);
city_state = (TextView) itemView.findViewById(R.id.city_state);
hobbies = (TextView) itemView.findViewById(R.id.hobbies);
marital_status = (TextView) itemView.findViewById(R.id.marital_status);
family_members = (TextView) itemView.findViewById(R.id.family_members);
mothers_name = (TextView) itemView.findViewById(R.id.mothers_name);
fathers_name = (TextView) itemView.findViewById(R.id.fathers_name);
fathers_occupation = (TextView) itemView.findViewById(R.id.fathers_occupation);
mothers_occupation = (TextView) itemView.findViewById(R.id.mothers_occupation);
send_invitation = (Button) itemView.findViewById(R.id.sendinvitation);
shortlist_profile=(Button)itemView.findViewById(R.id.shortlistprofile);
send_invitation=(Button)itemView.findViewById(R.id.sendinvitation);
shortlist_profile=(Button)itemView.findViewById(R.id.shortlistprofile);
shortlist_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent it=new Intent(v.getContext(),Shortlisted_Profile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
Toast.makeText(itemView.getContext(), "Data added to shortlisted profile", Toast.LENGTH_SHORT).show();
context.startActivity(it);
}
});
//parentLayout=itemView.findViewById(R.id.parent);
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();
if (getLayoutPosition() == 0) {
final Intent it = new Intent(v.getContext(), ViewProfile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
it.putExtra("positionGroup", position);
context.startActivity(it);
} else if (getLayoutPosition() == 1) {
final Intent it = new Intent(v.getContext(), ViewProfile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
// it.putExtra("last_name", user.getLast_name());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
it.putExtra("positionGroup", position);
context.startActivity(it);
} else if (getLayoutPosition() == 2) {
final Intent it = new Intent(v.getContext(), ViewProfile.class);
it.putExtra("first_name", FirstNameTextView.getText().toString());
// it.putExtra("last_name", user.getLast_name());
it.putExtra("date", DateTextView.getText().toString());
it.putExtra("height", HeightTextView.getText().toString());
it.putExtra("city_state", city_state.getText().toString());
it.putExtra("hobbies", hobbies.getText().toString());
it.putExtra("highest_education", EducationTextView.getText().toString());
it.putExtra("occupation", OccupationTextView.getText().toString());
it.putExtra("income", income.getText().toString());
it.putExtra("marital_status", marital_status.getText().toString());
it.putExtra("family_members", family_members.getText().toString());
it.putExtra("fathers_name", fathers_name.getText().toString());
it.putExtra("mothers_name", mothers_name.getText().toString());
it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
it.putExtra("user_id", UserIDTextView.getText().toString());
it.putExtra("positionGroup", position);
context.startActivity(it);
}
}
}
}
>in the onBindview u can perfoam the action listner and tarnsfer the data
@Override
public void onBindViewHolder(@NonNull EnglishAdapter.ViewHolder holder, final int position) {
final EnglishItem englishItem = englishItems.get(position);
readCursorData(englishItem,holder);
holder.imageView.setImageResource(englishItem.getImageResourse());
holder.titletextview.setText(englishItem.getTitle());
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str_pos = String.valueOf(position);
Intent intent = new Intent(v.getContext(),English_Tenses_Details.class);
intent.putExtra("myKey", str_pos);
v.getContext().startActivity(intent);
}
});
}