50

onClick()メソッドのコードには次のようなものがあります

 List<Question> mQuestionsList = QuestionBank.getQuestions();

次のように、この行の後に意図があります。

  Intent resultIntent = new Intent(this, ResultActivity.class);
  resultIntent.putParcelableArrayListExtra("QuestionsExtra", (ArrayList<? extends Parcelable>) mQuestionsList);
  startActivity(resultIntent);

インテントでこの質問リストをあるアクティビティから別のアクティビティに渡す方法がわかりません My Question クラス

public class Question {
    private int[] operands;
    private int[] choices;
    private int userAnswerIndex;

    public Question(int[] operands, int[] choices) {
        this.operands = operands;
        this.choices = choices;
        this.userAnswerIndex = -1;
    }

    public int[] getChoices() {
        return choices;
    }

    public void setChoices(int[] choices) {
        this.choices = choices;
    }

    public int[] getOperands() {
        return operands;
    }

    public void setOperands(int[] operands) {
        this.operands = operands;
    }

    public int getUserAnswerIndex() {
        return userAnswerIndex;
    }

    public void setUserAnswerIndex(int userAnswerIndex) {
        this.userAnswerIndex = userAnswerIndex;
    }

    public int getAnswer() {
        int answer = 0;
        for (int operand : operands) {
            answer += operand;
        }
        return answer;
    }

    public boolean isCorrect() {
        return getAnswer() == choices[this.userAnswerIndex];
    }

    public boolean hasAnswered() {
        return userAnswerIndex != -1;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();

        // Question
        builder.append("Question: ");
        for(int operand : operands) {
            builder.append(String.format("%d ", operand));
        }
        builder.append(System.getProperty("line.separator"));

        // Choices
        int answer = getAnswer();
        for (int choice : choices) {
            if (choice == answer) {
                builder.append(String.format("%d (A) ", choice));
            } else {
                builder.append(String.format("%d ", choice));
            }
        }
        return builder.toString();
       }

      }
4

20 に答える 20

84

活動の間:私のために働いた

ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);

Transfer.class 内

Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");

この助けが誰かであることを願っています。

Parcelable を使用してアクティビティ間でデータを渡す

これは通常、DataModel を作成したときに機能します。

例 タイプの json があるとします。

{
    "bird": [{
        "id": 1,
        "name": "Chicken"
    }, {
        "id": 2,
        "name": "Eagle"
    }]
}

ここで、bird は List であり、2 つの要素が含まれているため、

jsonschema2pojoを使用してモデルを作成します

これでモデル クラス Name BirdModel と Bird BirdModel は Bird のリストで構成され、Bird には名前と ID が含まれます

bird クラスに移動し、インターフェース「implements Parcelable」を追加します。

Alt+Enter で android studio に implemets メソッドを追加

注: ダイアログ ボックスが表示され、[メソッドの実装を追加] を入力して Enter を押します。

Alt + Enter を押して、Parcelable 実装を追加します。

注: ダイアログ ボックスが表示され、「Parcelable 実装を追加してもう一度入力してください」と表示されます。

これをインテントに渡します。

List<Bird> birds = birdModel.getBird();
Intent intent = new Intent(Current.this, Transfer.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Birds", birds);
intent.putExtras(bundle);
startActivity(intent);

転送アクティビティ onCreate

List<Bird> challenge = this.getIntent().getExtras().getParcelableArrayList("Birds");

ありがとう

問題があればお知らせください。

于 2014-07-08T11:28:04.463 に答える
36

手順:

  1. オブジェクト クラスをシリアライズ可能に実装します

    public class Question implements Serializable`
    
  2. これをソースアクティビティに入れます

    ArrayList<Question> mQuestionList = new ArrayList<Question>;
    mQuestionsList = QuestionBank.getQuestions();  
    mQuestionList.add(new Question(ops1, choices1));
    
    Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
    intent.putExtra("QuestionListExtra", mQuestionList);
    
  3. これをターゲットアクティビティに入れます

     ArrayList<Question> questions = new ArrayList<Question>();
     questions = (ArrayList<Questions>) getIntent().getSerializableExtra("QuestionListExtra");
    
于 2015-05-15T17:59:21.853 に答える
24

それはうまく機能し、

public class Question implements Serializable {
    private int[] operands;
    private int[] choices;
    private int userAnswerIndex;

   public Question(int[] operands, int[] choices) {
       this.operands = operands;
       this.choices = choices;
       this.userAnswerIndex = -1;
   }

   public int[] getChoices() {
       return choices;
   }

   public void setChoices(int[] choices) {
       this.choices = choices;
   }

   public int[] getOperands() {
       return operands;
   }

   public void setOperands(int[] operands) {
       this.operands = operands;
   }

   public int getUserAnswerIndex() {
       return userAnswerIndex;
   }

   public void setUserAnswerIndex(int userAnswerIndex) {
       this.userAnswerIndex = userAnswerIndex;
   }

   public int getAnswer() {
       int answer = 0;
       for (int operand : operands) {
           answer += operand;
       }
       return answer;
   }

   public boolean isCorrect() {
       return getAnswer() == choices[this.userAnswerIndex];
   }

   public boolean hasAnswered() {
       return userAnswerIndex != -1;
   }

   @Override
   public String toString() {
       StringBuilder builder = new StringBuilder();

       // Question
       builder.append("Question: ");
       for(int operand : operands) {
           builder.append(String.format("%d ", operand));
       }
       builder.append(System.getProperty("line.separator"));

       // Choices
       int answer = getAnswer();
       for (int choice : choices) {
           if (choice == answer) {
               builder.append(String.format("%d (A) ", choice));
           } else {
               builder.append(String.format("%d ", choice));
           }
       }
       return builder.toString();
     }
  }

ソース アクティビティで、これを使用します。

  List<Question> mQuestionList = new ArrayList<Question>;
  mQuestionsList = QuestionBank.getQuestions();
  mQuestionList.add(new Question(ops1, choices1));

  Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
  intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);

ターゲット アクティビティで、これを使用します。

  List<Question> questions = new ArrayList<Question>();
  questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");
于 2012-11-29T00:13:10.210 に答える
10

Bean または pojo クラスはimplements parcelable interface.

例えば:

public class BeanClass implements Parcelable{
    String name;
    int age;
    String sex;

    public BeanClass(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    } 
     public static final Creator<BeanClass> CREATOR = new Creator<BeanClass>() {
        @Override
        public BeanClass createFromParcel(Parcel in) {
            return new BeanClass(in);
        }

        @Override
        public BeanClass[] newArray(int size) {
            return new BeanClass[size];
        }
    };
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeString(sex);
    }
}

タイプの を からarraylistに送信するシナリオを考えてみましょう。 次のコードを使用しますbeanclassActivity1Activity2

アクティビティ 1:

ArrayList<BeanClass> list=new ArrayList<BeanClass>();

private ArrayList<BeanClass> getList() {
    for(int i=0;i<5;i++) {

        list.add(new BeanClass("xyz", 25, "M"));
    }
    return list;
}
private void gotoNextActivity() {
    Intent intent=new Intent(this,Activity2.class);
    /* Bundle args = new Bundle();
    args.putSerializable("ARRAYLIST",(Serializable)list);
    intent.putExtra("BUNDLE",args);*/

    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("StudentDetails", list);
    intent.putExtras(bundle);
    startActivity(intent);
}

アクティビティ 2:

ArrayList<BeanClass> listFromActivity1=new ArrayList<>();

listFromActivity1=this.getIntent().getExtras().getParcelableArrayList("StudentDetails");

if (listFromActivity1 != null) {

    Log.d("listis",""+listFromActivity1.toString());
}

これはコンセプトを理解するための基本だと思います。

于 2018-02-06T13:05:48.550 に答える
7

オブジェクトを Parcelable 経由で渡します。そして、ここにあなたが始めるための良いチュートリアルがあります.
最初の質問は、このように Parcelable を実装し、それらの行を追加する必要があります。

public class Question implements Parcelable{
    public Question(Parcel in) {
        // put your data using = in.readString();
  this.operands = in.readString();;
    this.choices = in.readString();;
    this.userAnswerIndex = in.readString();;

    }

    public Question() {
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(operands);
        dest.writeString(choices);
        dest.writeString(userAnswerIndex);
    }

    public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {

        @Override
        public Question[] newArray(int size) {
            return new Question[size];
        }

        @Override
        public Question createFromParcel(Parcel source) {
            return new Question(source);
        }
    };

}

次に、次のようにデータを渡します。

Question question = new Question();
// put your data
  Intent resultIntent = new Intent(this, ResultActivity.class);
  resultIntent.putExtra("QuestionsExtra", question);
  startActivity(resultIntent);

そして、次のようにデータを取得します。

Question question = new Question();
Bundle extras = getIntent().getExtras();
if(extras != null){
    question = extras.getParcelable("QuestionsExtra");
}

これで十分です!

于 2012-11-28T09:35:27.090 に答える
3

このシナリオでは、次の 2 つのことのいずれかを行います

  1. オブジェクトのシリアル化/逆シリアル化システムを実装し、それらを文字列として渡します (通常は JSON 形式ですが、好きな方法でシリアル化できます)

  2. アクティビティの外部にあるコンテナーを実装して、すべてのアクティビティがこのコンテナーに対して読み書きできるようにします。このコンテナーを静的にするか、ある種の依存性注入を使用して、各アクティビティで同じインスタンスを取得できます。

Parcelable は問題なく動作しますが、見栄えの悪いパターンであり、モデルの外部で独自のシリアル化コードを記述した場合に存在しない価値を実際に追加することはありません。

于 2012-11-28T09:42:03.273 に答える
3

クラスQuestionにプリミティブSerializeble、またはStringフィールドしか含まれていない場合は、 Serializableを実装できます。ArrayList はSerializableを実装しているため、 Bundle.putSerializable(key, value)のように配置して別のActivityに送信できます。私見、Parcelable - それは非常に長い道のりです。

于 2012-11-28T09:47:27.510 に答える
2

Parcelable インターフェースも実装する必要があり、Serializable に加えて、Constructor で Parcel 引数を使用して、Ques クラスに writeToParcel メソッドを追加する必要があります。そうしないと、アプリがクラッシュします。

于 2015-01-17T01:51:24.603 に答える
2

あなたの配列リスト:

ArrayList<String> yourArray = new ArrayList<>();

インテントが必要な場所から次のコードを記述します。

Intent newIntent = new Intent(this, NextActivity.class);
newIntent.putExtra("name",yourArray);
startActivity(newIntent);

次のアクティビティ:

ArrayList<String> myArray = new ArrayList<>();

onCreate に次のコードを記述します。

myArray =(ArrayList<String>)getIntent().getSerializableExtra("name");
于 2018-01-31T08:11:09.503 に答える
1

意図を持ったバンドルを使用して、あるアクティビティから別のアクティビティに配列リストを渡すことができます。以下のコードを使用してくださいこれは、arraylistを渡すための最短かつ最も適切な方法です

bundle.putStringArrayList( "keyword"、arraylist);

于 2012-11-28T10:13:17.560 に答える
1

Question実装する場合、意図の作成は正しいようですParcelable

次のアクティビティでは、次のように質問のリストを取得できます。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(getIntent() != null && getIntent().hasExtra("QuestionsExtra")) {
        List<Question> mQuestionsList = getIntent().getParcelableArrayListExtra("QuestionsExtra");
    }
}
于 2012-11-28T09:39:13.393 に答える