0

我真的很困惑如何从另一个活动中添加我在这部分代码中不断收到错误:

Emails a = new Emails(intent.getStringExtra("inputEmail"), intent.getStringExtra("inputBody"), intent.getStringExtra("inputSubject"));

所以错误说“实际和形式参数列表的长度不同”。如果有人可以帮助我,那将是一个巨大的帮助,谢谢:)

这是我的代码:

主要活动:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private Button newEmail;
    private ListView listView;
    private EmailAdapter emailAdapter;
    private ArrayList<Emails> emailsArrayList;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();


         emailAdapter = new EmailAdapter(this, emailsArrayList);
         listView.setAdapter(emailAdapter);
         updateList();


        newEmail.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SendActivity.class);
                startActivity(intent);
            }
        });

    }

    private void init(){
        newEmail = (Button) findViewById(R.id.newBtn);
        listView = (ListView) findViewById(R.id.list);

        emailsArrayList = new ArrayList<>();

        Emails emails = new Emails ();
        emails.setEmails("josemari@yahey.com");
        emails.setSubject("Sample Data");
        emails.setBody("this is the sample data");
        emailsArrayList.add(emails);


    }

    private void updateList()
    {
        Bundle bundle = getIntent().getExtras();
        Intent intent = getIntent();
        if(bundle != null)
        {
            Emails a = new Emails(intent.getStringExtra("inputEmail"), intent.getStringExtra("inputBody"), intent.getStringExtra("inputSubject"));
            emailAdapter.add(a);
            emailAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);

        if(requestCode == 1 && resultCode == RESULT_OK)
        {
            Emails a = new Emails(intent.getStringExtra("inputEmail"), intent.getStringExtra("inputBody"), intent.getStringExtra("inputSubject"));
            emailAdapter.add(a);
            emailAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onStart() {
         super.onStart();
         Log.d("MainActivity","onStart invoked");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("MainActivity","onResume invoked");
    }

    @Override
    protected void onPause() {

        super.onPause();
        Log.d("MainActivity","onPause invoked");
    }

    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onRestart() {

        super.onRestart();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
}

这是添加项目活动:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class SendActivity extends AppCompatActivity {

    private Button send;
    private Button discard;
    private EditText email;
    private EditText subject;
    private EditText body;
    private ArrayList<Emails> emailsArrayList;


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

        send = (Button) findViewById(R.id.sendBtn);
        discard = (Button) findViewById(R.id.discardBtn);
        email = (EditText) findViewById(R.id.inputEmail);
        subject = (EditText) findViewById(R.id.inputSubject);
        body = (EditText) findViewById(R.id.inputBody);


        discard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(SendActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputEmail = email.getText().toString();
                String inputSubject = subject.getText().toString();
                String inputBody = body.getText().toString();

                if (inputBody.isEmpty() || inputEmail.isEmpty() || inputEmail.isEmpty()){
                    Toast.makeText(SendActivity.this, "Please enter the following data", Toast.LENGTH_SHORT).show();
                }
                else {
                    emailsArrayList = new ArrayList<>();
                    Emails newEmails = new Emails ();
                    newEmails.setEmails(inputEmail);
                    newEmails.setSubject(inputSubject);
                    newEmails.setBody(inputBody);
                    emailsArrayList.add(newEmails);
                    Intent intent = new Intent();
                    getIntent().putExtra("inputEmail", inputEmail);
                    getIntent().putExtra("inputSubject", inputSubject);
                    getIntent().putExtra("inputBody", inputBody);

                    setResult(RESULT_OK, intent);

                    finish();
                }


            }
        });
    }




}
4

1 回答 1

0

我还将更改以下内容:

private void updateList()
{
    Bundle bundle = getIntent().getExtras();
    Intent intent = getIntent();
    if(bundle != null)
    {
        Emails a = new Emails(intent.getStringExtra("inputEmail"), intent.getStringExtra("inputBody"), intent.getStringExtra("inputSubject"));
        emailAdapter.add(a);
        emailAdapter.notifyDataSetChanged();
    }
}

    private void updateList()
    {
Intent intent = getIntent();        
Bundle bundle = intent.getExtras();

        if(bundle != null)
        {
        Emails a = new Emails(String)bundle.get("inputEmail"), (String)bundle.get("inputBody"), (String)bundle.get("inputSubject"));
            emailAdapter.add(a);
            emailAdapter.notifyDataSetChanged();
        }
    }

我会先试试这个绝对的: 你可以使用你所拥有的,只需在你的updateList()中更改 Bundle 包和 Intent 意图行。再次没有专家,但先尝试,然后再尝试。我希望这个对你有用。

于 2021-05-31T21:34:01.163 回答