简体   繁体   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) {

                }
            });
        }
    }
}

I am trying to pass the putExtra at extends AppCompatActivity and I write the Intent intent = new Intent(mContext, c_Report_Activity.class);我试图在 extends AppCompatActivity 传递 putExtra 并且我写了 Intent intent = new Intent(mContext, c_Report_Activity.class); and it get null from the receiver.它从接收器那里得到空值。

Anyone can teach me how to pass the value to putExtra?任何人都可以教我如何将值传递给 putExtra? Thank you very much非常感谢

Unable to instantiate activity ComponentInfo{com.example.counselfyp2019/com.example.counselfyp2019.c_Message_Activity}: java.lang.InstantiationException: java.lang.Class has no zero argument constructor无法实例化活动 ComponentInfo{com.example.counselfyp2019/com.example.counselfyp2019.c_Message_Activity}:java.lang.InstantiationException:java.lang.Class 没有零参数构造函数

You need to receipt this intent into you other class您需要将此意图接收到您的其他课程中

You have an exemple here :)在这里有一个例子:)

PutExtra :放置额外:

    @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 in receipt class:收据类中的 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.

相关问题 如何将类文件变量获取到另一个活动,并使用意图而不是putExtra通过按钮进行访问? - How do I get class file variable to another activity and access via button using intent not putExtra? 如何将活动意图传递给另一个班级? - How to pass an activity intent to another class? 如何将意图从活动传递给扩展LinearLayout的类 - How do I pass an intent from an activity to a class extending LinearLayout 如何从超类重用代码Intent intent = new Intent(context,Activity.class)到子类? - How to reuse the code Intent intent=new Intent(context,Activity.class) from a superclass to a subclass? 意图使用putExtra在新活动中显示特定布局 - Using putExtra in intent to display specific layout in new activity 我如何将通用类作为参数传递给Intent构造函数 - How can i pass a generic class as param to Intent constructor Intent.putExtra-如何将字符串发送到下一个活动? - Intent.putExtra - How to send a string to the next activity? 在listactivity中使用(Intent putextra)传递数据不起作用 - Pass data with (Intent putextra) in listactivity not working 如何转移图像新意图 - How can I transfer image new intent 如何在 Intent 中创建类变量: Intent intent = new Intent(getApplicationContext().this, variable.class); - How do you make a class varible in an Intent: Intent intent = new Intent(getApplicationContext().this, variable.class);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM