android - 单击cardview时如何将数据从recyclerview传输到下一个 Activity

标签 android android-intent firebase-realtime-database android-cardview

我创建了一个应用程序,我在其中从数据库中检索了多个用户,我使用 firebase 作为我的数据库。单击卡片 View 时,我想将数据从我的卡片 View 传输到另一个 Activity 。当我点击卡片 View 时,我从数据库中获取所有用户数据。

These are the users

This the data inside user

下面是recycler View 的代码:

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 View 适配器的代码:

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);

    }
}

我希望单个用户在单个卡片 View 上显示,而我在每个单个卡片 View 上都有多个用户。我该怎么办?

最佳答案

创建一个接口(interface),当你点击你的适配器项目然后在接口(interface)的帮助下在 Activity 中发送所需的详细信息:-

这是界面:

public interface ItemClickListner {
    void onClick(String  itemId);
}

在 recyclerview 上的父 Activity 中设置 intent extra 中的数据;-

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");

关于android - 单击cardview时如何将数据从recyclerview传输到下一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58799601/

相关文章:

android - 如何使用自定义 Xml 资源文件中的资源?

android - 在线程 2017 中显示进度对话框

android - 重新启动任务中的所有 Activity

java - 尝试使用通知

ios - firebase 和 swift - 此类不符合 key 的键值编码 -----

java - Android AsyncTask-to-Activity 回调 nullPointerException

android - 对于 recyclerView 中的 TextView,textIsSelectable() 在 "floating text selection toolbar"中缺少项目

android - 如何在android中使用kso​​ap解析调用方法时发送参数

java - 无法将 java.lang.String 类型的对象转换为 com.rafaquarta.whatsapp.model.Usuario 类型

javascript - 从 firebase for javascript 获取数据多个数据