如何将数据从片段发送到另一个活动

时间:2019-12-04 10:33:29

标签: java android android-fragments google-cloud-firestore

在我的应用程序中,我有一个活动,该活动在片段中包含recyclerview,并且recycleview从云存储Firebase检索数据。我想打开一个新活动,该活动将根据用户单击的链接(在“回收者”视图上显示)来检索数据。 如何获取数据并将其显示在新活动中?

从ForumTitle.java>显示ReviewFragment.java>用户单击一个值>显示ForumInterface.java

ForumTitle.java

How to Solve This Error , i am tired to solve this error.
i am using visual studio 2019 and version - 16.1.2 and .NET Framework version 4.8.03761
and also project name is diffrent "Girly". and install the package
Install-Package UmbracoCms -Version 8.3.0

ReviewFragment.java

public class ForumTitle extends AppCompatActivity {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();


    ImageButton IVReview,IVTechnical,IVHardware;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forum_title);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment selectedFragment = null;
                if (v == findViewById(R.id.iBReview)){
                    selectedFragment = new ReviewFragment();
                }
                else if (v == findViewById(R.id.iBTech)){
                    selectedFragment = new TechnicalSupportFragment();
                }
                else if (v == findViewById(R.id.iBHardware)){
                    selectedFragment = new HardwareFragment();
                }
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.fragment_container,selectedFragment);
                transaction.commit();
            }

        };
        IVReview = (ImageButton)findViewById(R.id.iBReview);
        IVTechnical = (ImageButton)findViewById(R.id.iBTech);
        IVHardware = (ImageButton)findViewById(R.id.iBHardware);
        IVReview.setOnClickListener(listener);
        IVTechnical.setOnClickListener(listener);
        IVHardware.setOnClickListener(listener);
    }
}

ForumInterface.java

public class ReviewFragment extends Fragment {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference userRef = db.collection("Review");
    private ForumAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_review,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Query query = userRef.orderBy("DatePosted",Query.Direction.DESCENDING);
        FirestoreRecyclerOptions<Forum> options = new
                FirestoreRecyclerOptions.Builder<Forum>()
                .setQuery(query,Forum.class)
                .build();
        adapter = new ForumAdapter(options);
        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.rvReview);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new ForumAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                Forum forum = documentSnapshot.toObject(Forum.class);
                String title = forum.getTitle();
                String id = documentSnapshot.getId();
                Intent intent = new Intent(getActivity(), ForumInterface.class);
                Bundle extras = intent.getExtras();
                extras.putString("FORUM_TYPE","Review");
                extras.putString("FORUM_ID",id);
                extras.putString("TITLE",title);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }

ForumAdapter.java

public class ForumInterface extends AppCompatActivity {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String forum_title = extras.getString("TITLE");
    String forum_type = extras.getString("FORUM_TYPE");
    String forum_id = extras.getString("FORUM_ID");
    private CollectionReference userRef = db.collection(forum_type).document(forum_id).collection(forum_title);
    private ForumAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forum_interface);


        TextView test = (TextView)findViewById(R.id.tvForumTitle);
        test.setText(forum_title);
    }
}

使用上面的代码运行它,我得到了一个错误

public class ForumAdapter extends FirestoreRecyclerAdapter<Forum,ForumAdapter.ForumHolder> {
    private OnItemClickListener listener;

    public ForumAdapter(FirestoreRecyclerOptions<Forum> options) {
        super(options);
    }

    @Override
    public void onBindViewHolder(ForumHolder forumHolder, int i, Forum forum) {
        forumHolder.textViewTitle.setText(forum.getTitle());
        forumHolder.textViewDescription.setText(forum.getDescription());
        forumHolder.timeStamp.setText(forum.getDatePosted().toString());
    }

    @NonNull
    @Override
    public ForumHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
        return new ForumHolder(v);
    }
    class ForumHolder extends RecyclerView.ViewHolder{
        TextView textViewTitle;
        TextView textViewDescription;
        TextView timeStamp;
        public ForumHolder(View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.title);
            textViewDescription = itemView.findViewById(R.id.description);
            timeStamp = itemView.findViewById(R.id.timestamp);

            textViewTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    // NO_POSITION to prevent app crash when click -1 index
                    if(position != RecyclerView.NO_POSITION && listener !=null ){
                        listener.onItemClick(getSnapshots().getSnapshot(position),position);
                    }
                }
            });
        }
    }
    public interface OnItemClickListener{
        void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }
    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}

我的应用图片 ForumTitle

ReviewFragment

当我单击标题“ GTX 1660 Ti Gaming X”时 Error message

1 个答案:

答案 0 :(得分:1)

您尝试在Bundle的{​​{1}}中的null的{​​{1}}上设置内容。使用ReviewFragment而不是onClick实例化new Bundle()

intent.getExtras();

此动作在Bundle活动的Bundle extras = new Bundle(); extras.putString("FORUM_TYPE","Review"); extras.putString("FORUM_ID",id); extras.putString("TITLE",title); //You have to set the bundle to intent intent.putExtras(extras); startActivity(intent); 内部的代码下方

onCreate

这是完整的活动。

ForumInterface