0

I have have 2 activities "MainActivity", "Main2Activity" and 2 classes "User", "UserAdapter". What I want actually want is explained below.

  1. First of all I want to insert several data from "MainActivity" (If I insert "name" and "email" 5 times then I've inserted 5 data) by clicking "submit" button.

  2. Then I want to show all data as listview in "Main2Activity" which is another activity.

  3. But I can only see the last data I've inserted while I inserted several data.

  4. I mean if I insert 1st data(John, john123@gmail.com), 2nd data(cherry, cherry45@gmail.com) by clicking submit button I can see all data in same activity which is "MainActivity" and can only see the last data (cherry, cherry45@gmail.com) in second activity which is "Main2Activity".

  5. Can anyone help me to pass entire arraylist from MainActivity to display in listview to Main2Activity? My all codes are given below.

===============MainActivity.java=================

package com.andromet.customlistview;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.LayoutInflater;`

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.Toast;

import java.io.Serializable;

import java.lang.reflect.Array;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

EditText editName, editEmail;
String name, email;
Intent intent;
UserAdapter adapter;

ListView listView;
ArrayList<User> users = new ArrayList<>();

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

    editName = findViewById(R.id.name);
    editEmail = findViewById(R.id.email);
    listView = findViewById(R.id.listView);
    adapter = new UserAdapter(this, users);
}

public void submit(View view) {
    name = editName.getText().toString();
    email = editEmail.getText().toString();

    listView.setAdapter(adapter);
    users.add(new User(name,email));
    intent = new Intent(MainActivity.this, Main2Activity.class);

    intent.putExtra("name", name);
    intent.putExtra("email", email);
    adapter.notifyDataSetChanged();
    Toast.makeText(this,"Total Items: " +users.size(), Toast.LENGTH_SHORT).show();
}

public void onListClicked(View view) {
    //intent = new Intent(MainActivity.this, Main2Activity.class);
    startActivity(intent);
}
}

===============Main2Activity.java================

package com.andromet.customlistview;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.ListView;

import android.widget.Toast;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

ListView listView;
UserAdapter adapter;
String name, email;
ArrayList<User> users = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    listView = findViewById(R.id.listView2);
    adapter = new UserAdapter(this, users);
    Bundle bundle = getIntent().getExtras();
    name = bundle.getString("name");
    email = bundle.getString("email");

    listView.setAdapter(adapter);
    users.add(new User(name, email));
    adapter.notifyDataSetChanged();
    Toast.makeText(this,"Total Items: " +users.size(), Toast.LENGTH_SHORT).show();
}

}

==================User.java=================

package com.andromet.customlistview;

public class User {

private String name;
private String email;

public User(String name, String email) {
    this.name = name;
    this.email = email;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String emial) {
    this.email = emial;
}

}

===============UserAdapter.java=================

package com.andromet.customlistview;

import android.content.Context;

import android.support.annotation.NonNull;

import android.support.annotation.Nullable;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.TextView;

import java.util.ArrayList;

public class UserAdapter extends ArrayAdapter<User> {

private Context context;
private ArrayList<User> users;

public UserAdapter(Context context, ArrayList<User> users){
    super(context, 0, users);
    this.users = users;
    this.context = context;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View row = convertView;

    if (row == null){
        row = LayoutInflater.from(context).inflate(R.layout.mylist,parent,false);
    }
    User user = users.get(position);

    if (row != null){
        TextView textViewName = row.findViewById(R.id.view_name);
        TextView textViewEmail = row.findViewById(R.id.view_email);
        textViewName.setText(user.getName());
        textViewEmail.setText(user.getEmail());
    }

    return row;
}
}

This is what I can see in Main2Activity

This is what I can see in MainActivity and I want to pass this entire array to Main2Activity

Amin
  • 25
  • 1
  • 7
  • Possible duplicate of https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities. Review the singleton and other options in the accepted answer there as ways of storing and sharing data between activities. – Tyler V Jul 28 '18 at 23:23

1 Answers1

1

There are a lot of problems in Your implementation.

  1. You are not saving User data anywhere like in some class other than your activities.
  2. You are not passing all the data to Main2Activity, that why You can see only one item and that's also the latest added item.

Solution:

  1. Save User data in some UserManager class. You can make this class Singleton for ease of access. Provide the method to add User into some ArrayList and a get method to retrieve the whole list.

  2. Now, whenever Your Main2Activity start, fetch data from UserManager and then display it in the list.

theJango
  • 1,100
  • 10
  • 22
  • I have no idea how to implement UserManager to save and fetch data from it. I have given all my source code. Can you please cooperate to come to this solution providing "UserManager" class implemented and necessary source codes to add to the Main2Activity class? – Amin Jul 28 '18 at 19:59
  • something like this http://www.java2novice.com/java_constructor_examples/singleton/ – theJango Jul 28 '18 at 20:04
  • Although I solved this problem in alternative way, thanks for your extreme help. I have solved this using string array. It does save data temporarily till running the app. But this time I am happy with that. :) – Amin Jul 29 '18 at 19:28