简体   繁体   English

在这个程序中将ArrayList从一个活动传递到另一个活动不起作用

[英]in this program passing ArrayList from one activity to another not working

first activity : 第一项活动

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

  private Button bt1;
  private Button bt2;
  private EditText ed1;
  private EditText ed2;
  private TextView tv3;
  static ArrayList<String> s = new ArrayList<>();
  static ArrayList<Integer> i = new ArrayList<>();

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

    bt1 = (Button)findViewById(R.id.button);
    ed1 = (EditText) findViewById(R.id.editText);
    ed2 = (EditText)findViewById(R.id.editText2);
    bt2 = (Button)findViewById(R.id.button2);
    tv3 = (TextView)findViewById(R.id.textView3);

    bt2.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         s.add(ed1.getText().toString());
         i.add(Integer.parseInt(ed2.getText().toString()));
       }
    });

    bt1.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         try {
           Intent intent = new Intent(MainActivity.this, second.class);
           intent.putExtra("key", s);
           startActivity(intent);
         }
         catch(Exception e) {
           tv3.setText(e.getMessage());
         }
       }
    });
  }
}

Second activity : 第二项活动

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.TextView;
import java.util.ArrayList;

 public class second extends AppCompatActivity {

   private TextView tv1;
   private TextView tv2;

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

    tv1 = (TextView)findViewById(R.id.textView);
    tv2 = (TextView)findViewById(R.id.textView2);

    ArrayList<String> s = (ArrayList<String>)getIntent().getSerializableExtra("key");
    for(int j=0;j<=s.size();j++) {
      tv1.setText(s.get(j));
    }
  }
}

I don't understand the problem in this code. 我不明白这段代码中的问题。 when I click on bt2 to pass the ArrayList from on activity to another then the app just shutdown. 当我点击bt2将ArrayList从on活动传递到另一个时,应用程序就会关闭。 I am not able to understand the problem in this code. 我无法理解此代码中的问题。

Please help me, 请帮我,

I have also updated the manifest.xml for second class. 我还更新了第二类的manifest.xml。

Replace your for loop with below, use j< s.size and not j<=s.size 用下面替换你的for循环,使用j <s.size而不是j <= s.size

for(int j=0;j<s.size();j++)
{
  tv1.setText(s.get(j));
}

Are you sure 你确定吗

i.add(Integer.parseInt(ed2.getText().toString()));

in the onClick() call doesnt throw an Exception? 在onClick()调用不会抛出异常? Use a try-catch block around your code in Listener.onClick(). 在Listener.onClick()中使用代码周围的try-catch块。

Btw., you can easily use Lambda Exrpessions for ActionListeners. 顺便说一句,您可以轻松地将Lambda Exrpessions用于ActionListeners。 https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

Please use following construct:- 请使用以下构造: -

While Sending: 发送时:

intent.putStringArrayListExtra("key", s)

While Receiving: 接收时:

ArrayList<String> s = getIntent().getStringArrayListExtra("key");

You can do like this : 你可以这样做:

Bundle info= new Bundle();
ArrayList<Product> mas = new ArrayList<Product>();
info.putSerializable("product", mas);
intent.putExtras(info);

And get the values with this: 并获得以下值:

ArrayList<Product> prod = getIntent().getSerializableExtra(key);

Product class like a serializable object 产品类就像一个可序列化的对象

private class Product implements Serializable {

 }

Here it is just how you can pass arraylist between activities.Hope you wil get an idea to solve your issues. 这就是你如何在活动之间传递arraylist。希望你能有一个想法来解决你的问题。

intent.putExtra("key", s); -- this is wrong, you are sending a string but not your arrayList - 这是错误的,你发送的是字符串而不是你的arrayList

Try sending with-- intent.putStringArrayListExtra("key", s); 尝试使用intent.putStringArrayListExtra("key", s); and receiving -- ArrayList<String> stringArray = getIntent().getStringArrayListExtra("key"); 和接收 - ArrayList<String> stringArray = getIntent().getStringArrayListExtra("key");

hope this will solve your problem... 希望这能解决你的问题......

In your second activity onCreate , add the following code: 第二个 onCreate 活动中 ,添加以下代码:

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    // Add view

    // Must add check extras if null.
    Bundle extras = getIntent().getExtras();

    ArrayList<String> s = extras.getStringArrayListExtra("key");

    // do something

  }

In your first activity , send intent with: 在您的第一个活动中 ,发送意图:

Intent intent = new Intent(MainActivity.this, second.class);
intent.putStringArrayListExtra("key", s);
startActivity(intent);

I've tried an sample app with your code. 我已经尝试了一个带代码的示例应用程序。 Hope this will help you. 希望这会帮助你。 Kindly check my codes. 请检查我的代码。

First Activity 第一项活动

package com.arindam.testapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class FirstActivity extends AppCompatActivity {

    Button bt1;
    EditText ed1;
    EditText ed2;
    TextView tv3;
    ArrayList<String> s = new ArrayList<>();
    ArrayList<String> i = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        bt1 = (Button)findViewById(R.id.button);
        ed1 = (EditText) findViewById(R.id.editText);
        ed2 = (EditText)findViewById(R.id.editText2);
        tv3 = (TextView)findViewById(R.id.textView);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    s.add(ed1.getText().toString());
                    i.add(ed2.getText().toString());
                    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                    intent.putExtra("key", s);
                    intent.putExtra("value", i);
                    startActivity(intent);
                }
                catch(Exception e)
                {
                    tv3.setText(e.getMessage());
                }
            }
        });
    }
}

First Activity Layout 第一个活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#679667"
    android:orientation="vertical"
    tools:context="com.arindam.testapp.FirstActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />
</LinearLayout>

Second Activity 第二项活动

package com.arindam.testapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.widget.TextView;
import java.util.ArrayList;
public class SecondActivity extends AppCompatActivity {

    private TextView tv1;
    private TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        tv1 = (TextView)findViewById(R.id.textView);
        tv2 = (TextView)findViewById(R.id.textView2);

        ArrayList<String> s = getIntent().getStringArrayListExtra("key");
        for(int j=0;j<s.size();j++)
        {
            tv1.setText(s.get(j));
        }

        ArrayList<String> i = getIntent().getStringArrayListExtra("value");
        for(int k=0;k<i.size();k++)
        {
            tv2.setText(i.get(k));
        }

    }
}

Second Activity Layout 第二个活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#229597"
    android:orientation="vertical"
    tools:context="com.arindam.testapp.SecondActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>
</LinearLayout>

If you want to work with key value pairs, go for hashMap. 如果要使用键值对,请转到hashMap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM