簡體   English   中英

如何在 Intent intent = new Intent(mContext, c_Report_Activity.class); 處傳遞 putExtra;

[英]How can I pass putExtra at Intent intent = new Intent(mContext, c_Report_Activity.class);

public class c_Message_Activity extends AppCompatActivity {

    CircleImageView s_profile_image;
    TextView s_profile_name;

    ImageView c_btn_chat_send;
    EditText c_txt_send;

    FirebaseUser fuser;
    DatabaseReference reference;
    Intent intent;

    c_MessageAdapter messageAdapter;
    List<c_Chat> mchat;

    RecyclerView recyclerView;

    ValueEventListener seenListener;

    AlertDialog.Builder builder;

    private Context mContext;


    public c_Message_Activity(Context mContext) {
        this.mContext = mContext;
    }

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

        Toolbar toolbar = findViewById(R.id.c_chat_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(c_Message_Activity.this, c_MainActivty.class);
                startActivity(intent);
                finish();
            }
        });


        recyclerView = findViewById(R.id.c_chat_recycler_view);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        //Show user name
        s_profile_image = findViewById(R.id.s_profile_image);
        s_profile_name = findViewById(R.id.s_profile_name);

        intent = getIntent();
        final String userid = intent.getStringExtra("userid");

        fuser = FirebaseAuth.getInstance().getCurrentUser();
        reference = FirebaseDatabase.getInstance().getReference("User").child(userid);

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);

                s_profile_name.setText(user.getName());

                if(user.getImage() != null && user.getImage().equals("default")){
                    s_profile_image.setImageResource(R.mipmap.ic_launcher);
                }else {
                    Glide.with(getApplicationContext()).load(user.getImage()).into(s_profile_image);
                }

                readMessages(fuser.getUid(), userid, user.getImage());

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        seenMessage(userid);

        //Send Function
        c_btn_chat_send = findViewById(R.id.c_btn_chat_send);
        c_txt_send = findViewById(R.id.c_txt_send);

        c_btn_chat_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = c_txt_send.getText().toString();

                if(!msg.equals("")){

                    sendMessage(fuser.getUid(), userid, msg);

                }else{

                    Toast.makeText(c_Message_Activity.this, "You can't send empty message. Please try again~", Toast.LENGTH_SHORT).show();
                }

                c_txt_send.setText("");
            }
        });
    }
    }

    private void sendMessage(final String sender, final String receiver, String message){

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

        if(message.equals("close file") || message.equals("CLOSE FILE")){

            Toast.makeText(getApplicationContext(),"Generate Report Activated.",Toast.LENGTH_SHORT).show();

            builder = new AlertDialog.Builder(c_Message_Activity.this, R.style.MyDialogTheme);
            builder.setTitle("Generate Report")
                    .setMessage("Are confirm to generate report?")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(mContext, c_Report_Activity.class);
                    intent.putExtra("userid",receiver);
                    mContext.startActivity(intent);
                }
            })
                    .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .create()
            .show();

        }else {

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("sender", sender);
            hashMap.put("receiver", receiver);
            hashMap.put("message", message);
            hashMap.put("isseen", false);

            reference.child("Chats").push().setValue(hashMap);

            final DatabaseReference chatRef = FirebaseDatabase.getInstance().getReference("Chatlist")
                    .child(fuser.getUid())
                    .child(receiver);

            chatRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (!dataSnapshot.exists()) {
                        chatRef.child("id").setValue(receiver);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }
}

我試圖在 extends AppCompatActivity 傳遞 putExtra 並且我寫了 Intent intent = new Intent(mContext, c_Report_Activity.class); 它從接收器那里得到空值。

任何人都可以教我如何將值傳遞給 putExtra? 非常感謝

無法實例化活動 ComponentInfo{com.example.counselfyp2019/com.example.counselfyp2019.c_Message_Activity}:java.lang.InstantiationException:java.lang.Class 沒有零參數構造函數

您需要將此意圖接收到您的其他課程中

在這里有一個例子:)

放置額外:

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent(mContext, c_Report_Activity.class);
        intent.putExtra("userid", receiver);
        mContext.startActivity(intent);
    }
})

收據類中的 getExtra:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if(extras != null)
    String data = extras.getString("userid"); // retrieve the data using first paramater of putExtra
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM