调用startActivityForResult并没有正确地传回意图

时间:2017-12-08 18:25:46

标签: java android

在我的代码中,我有一个用于配方的类,然后是每个步骤的另一个类。配方类包含该配方中步骤的arraylist。

我有3个进程被一个接一个地调用:

  • MainActivity使用startActivityResult
  • 调用AddEditRecipe
  • 在AddEditRecipe中我有另一个调用AddStep的startActivityResult
  • 我在AddStep中添加一个新的步骤实例到配方列表中。

它在调用finish()之前成功地将对象添加到列表中,但是当我进入第二个活动时检查列表的大小它已经恢复为0,即使我已经设法创建我从AddStep函数中收集的数据按钮。

基本上我不知道该做什么,所以一切都正确地存在。 也是一个抬头,我仍然是相当新的java,所以可能有很多其他的错误,任何随机的建议都非常感激。

MainActivity

passenger-child process

AddEditRecipe

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

Button newRecipe;
Button editRecipe;
Button deleteRecipe;
LinearLayout recipeLayout;

List<NewRecipe> listOfRecipes;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listOfRecipes = new ArrayList<>();
    recipeLayout = (LinearLayout) findViewById(R.id.RecipeListLayout);

    newRecipe = (Button) findViewById(R.id.NewRecipeButton);
    newRecipe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                    Intent i = new Intent(MainActivity.this, AddRecipePopUp.class);
                    startActivityForResult(i,998);

        }
    });

}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode==999 && resultCode == RESULT_OK){
        NewRecipe rec = (NewRecipe)data.getParcelableExtra("curr_recipe");
        Log.d("Number of Steps",Integer.toString(rec.listOfSteps.size()));
        UpdateScreen(rec);
    }

    if(requestCode== 998 && resultCode == RESULT_OK){
        NewRecipe recipe =(NewRecipe)data.getParcelableExtra("curr_recipe");
        listOfRecipes.add(recipe);

        Intent i = new Intent(MainActivity.this,AddEditRecipe.class);
        i.putExtra("curr_recipe", recipe);
        startActivityForResult(i,999);
    }
}

void UpdateScreen(final NewRecipe recipe){



       Button button = new Button(this);
        button.setText(recipe.name);
        button.setHeight(120);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                NewRecipe recipe1 = recipe;
                Button thisBut = (Button)view;
                String butName = thisBut.getText().toString().toLowerCase();
                for(int j = 0;j<listOfRecipes.size();j++){
                    String curr = listOfRecipes.get(j).name.toString().toLowerCase();
                    Log.d("Button ",thisBut.getText().toString());
                    Log.d("Current List ",listOfRecipes.get(j).name.toString().toLowerCase());
                    Log.d("Check bool statement", Boolean.toString(butName ==curr));


                }
                //recipe1 = listOfRecipes.get(0);

                Intent i = new Intent(MainActivity.this, AddEditRecipe.class);
                i.putExtra("curr_recipe",recipe1);
                startActivityForResult(i,997);

            }
        });
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        recipeLayout.addView(button,lp);

}
}

AddStep

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class AddEditRecipe extends Activity {
Button home;
Button add;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_edit_recipe);



    Intent i = getIntent();
    final NewRecipe this_recipe =  i.getParcelableExtra("curr_recipe");
    Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
    LinearLayout recipeLayout = (LinearLayout) findViewById(R.id.RecipeListLayout);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);


    for(int j = 0; j<this_recipe.listOfSteps.size();j++){
        Button button = new Button(this);
        button.setText(this_recipe.listOfSteps.get(j).processName);
        button.setHeight(120);


        recipeLayout.addView(button,lp);
    }

    home = (Button) findViewById(R.id.HomeButton);
    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent i = new Intent(AddEditRecipe.this, MainActivity.class);
            i.putExtra("curr_recipe", this_recipe);
            setResult(RESULT_OK,i);
            Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
            finish();


        }
    });

    add = (Button) findViewById(R.id.AddStepButton);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(AddEditRecipe.this, AddStep.class);
            i.putExtra("curr_recipe", this_recipe);
            startActivityForResult(i,999);
        }
    });

}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode==999 && resultCode == RESULT_OK){
        NewRecipe curr_rec = (NewRecipe)data.getParcelableExtra("curr_recipe");

        Button button = new Button(this);
        button.setText(curr_rec.listOfSteps.get(curr_rec.listOfSteps.size()-1).processName + " " + curr_rec.listOfSteps.get(curr_rec.listOfSteps.size()-1).seconds );
        button.setHeight(120);
        LinearLayout lay = (LinearLayout) findViewById(R.id.RecipeListLayout) ;
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lay.addView(button,lp);
    }
}

}

0 个答案:

没有答案
相关问题