add data to listview from another activity

203 views Asked by At

I'm really confused on how to add from another activity I keep getting an error in this part of the code:

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

So the error says "actual and formal argument lists differ in length". if anyone could help me out it would be such a huge help thank you :)

this my code:

main activity:

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("[email protected]");
        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();
    }
}

this is the add item activity:

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


            }
        });
    }




}
1

There are 1 answers

0
Shawn Mulligan On

I would also change the following as well:

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

to

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

I would try this absolutley first: You may beable to use what you have and just change the Bundle bundle and Intent intent lines around in your updateList(). Again no expert but try that first and then above that second. I hope it works for you.