我有2个活动“ MainActivity”,“ Main2Activity”和2个类“ User”,“ UserAdapter”。我想要的实际上在下面解释。
首先,我想通过单击“提交”按钮从“ MainActivity”中插入多个数据(如果我输入“ name”和“ email” 5次,则我插入了5个数据)。
然后我想在另一个活动“ Main2Activity”中将所有数据显示为列表视图。
但是当我插入多个数据时,我只能看到我最后插入的数据。
我的意思是,如果我通过单击提交按钮插入第一数据(John,john123 @ gmail.com),第二数据(樱桃,cherry45 @ gmail.com),我可以看到同一活动中的所有数据是“ MainActivity ”,并且只能看到第二个活动“ Main2Activity”中的最后一个数据(樱桃,cherry45 @ gmail.com)。
有人可以帮助我从MainActivity传递整个数组列表,以在ListView中显示给Main2Activity吗?我的所有代码都在下面给出。
=============== 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
答案 0 :(得分:1)
您的实施中存在很多问题。
User
数据,例如在某些班级中。Main2Activity
,这就是为什么您只能看到一项,而这也是最新添加的项。解决方案:
将用户数据保存在某些UserManager
类中。您可以使此类Singleton
为易于访问。提供将User添加到某些ArrayList
中的方法,以及提供get方法以检索整个列表。
现在,只要您的Main2Activity
开始,就从UserManager
获取数据,然后将其显示在列表中。