活动在使用Intent发送时未接收到List<Object>。

huangapple go评论80阅读模式
英文:

Activity does not receive List<Object> when sending it with Intent

问题

我目前在发送包含一个包含List&lt;Object&gt;的对象的意图方面遇到了困难
基本上我可以将正确的数据发送到另一个活动但是我未能获取数据中的List&lt;Object&gt;抱歉我真的不知道如何描述它

问题是在RecipeActivity中我可以通过调用例如`recipes.get(0).getIngredients()`来获取配料但在RecipeDetailsActivity中由于列表为空我会遇到错误
而且我也不知道如何在DetailsActivity中获取对象的正确索引

提前感谢

**Recipe.java**

```java
public class Recipe implements Parcelable {
    
    @SerializedName("id")
    @Expose
    private Integer mId;
    @SerializedName("name")
    @Expose
    private String mName;
    @SerializedName("ingredients")
    @Expose
    private List<Ingredient> mIngredients = null;
    @SerializedName("steps")
    @Expose
    private List<Step> mSteps = null;
    @SerializedName("servings")
    @Expose
    private Integer mServings;
    @SerializedName("image")
    @Expose
    private String mImage;

    // ... (其他方法)

}

**Ingredient.java**

```java
public class Ingredient {

    @SerializedName("quantity")
    @Expose
    private Double mQuantity;
    @SerializedName("measure")
    @Expose
    private String mMeasure;
    @SerializedName("ingredient")
    @Expose
    private String mIngredient;

    // ... (其他方法)

}

**RecipeActivity**

```java
public class RecipeActivity extends AppCompatActivity implements RecipeAdapter.RecipeAdapterOnClickHandler {
    
    // ... (其他代码)

    @Override
    public void onClick(int adapterPosition) {
        Context context = this;
        Class detailClass = RecipeDetailsActivity.class;

        Intent detailsIntent = new Intent(context, detailClass);
        detailsIntent.putExtra(MY_RECIPE, recipes.get(adapterPosition));
        startActivity(detailsIntent);
    }
    
    // ... (其他代码)

}

**RecipeDetailsActivity**

```java
public class RecipeDetailsActivity extends AppCompatActivity implements IngredientAdapter.IngredientAdapterOnClickHandler {
    
    // ... (其他代码)

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

        Intent intentToCatch = getIntent();
        recipes = intentToCatch.getParcelableExtra(RecipeActivity.MY_RECIPE);

        // ... (其他代码)

        mAdapter = new IngredientAdapter(this, ingredientList, RecipeDetailsActivity.this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(RecipeDetailsActivity.this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);

    }
    
    // ... (其他代码)

}

**RecipeAdapter**

```java
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.RecipeAdapterViewHolder> {
    
    // ... (其他代码)

}

**IngredientAdapter**

```java
public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.IngredientAdapterViewHolder> {
    
    // ... (其他代码)

}

这里是你提供的代码的翻译。如果你还有其他问题或需要进一步的帮助,请随时问我。

英文:

I am currently struggling with sending an intent that has an Object which contains a List<Object>.
Basically I can send the right data to another activity, but I am failing to get the List<Object> in the data. Sorry, I really don't know how to describe it.

The thing is, I am able to get the ingredients in the RecipeActivity by calling e.g. recipes.get(0).getIngredients() but in the RecipeDetailsActivity I get an error due to the fact that the List is empty. (And also I don't know how to get the correct index of the object in the DetailsActivity)

Thanks in advance!

Recipe.java

public class Recipe implements Parcelable {
@SerializedName(&quot;id&quot;)
@Expose
private Integer mId;
@SerializedName(&quot;name&quot;)
@Expose
private String mName;
@SerializedName(&quot;ingredients&quot;)
@Expose
private List &lt; Ingredient &gt; mIngredients = null;
@SerializedName(&quot;steps&quot;)
@Expose
private List &lt; Step &gt; mSteps = null;
@SerializedName(&quot;servings&quot;)
@Expose
private Integer mServings;
@SerializedName(&quot;image&quot;)
@Expose
private String mImage;
protected Recipe(Parcel in ) {
if ( in .readByte() == 0) {
mId = null;
} else {
mId = in .readInt();
}
mName = in .readString();
if ( in .readByte() == 0) {
mServings = null;
} else {
mServings = in .readInt();
}
mImage = in .readString();
}
public static final Creator &lt; Recipe &gt; CREATOR = new Creator &lt; Recipe &gt; () {
@Override
public Recipe createFromParcel(Parcel in ) {
return new Recipe( in );
}
@Override
public Recipe[] newArray(int size) {
return new Recipe[size];
}
};
public Integer getId() {
return mId;
}
public void setId(Integer id) {
mId = id;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public List &lt; Ingredient &gt; getIngredients() {
return mIngredients;
}
public void setIngredients(List &lt; Ingredient &gt; ingredients) {
mIngredients = ingredients;
}
public List &lt; Step &gt; getSteps() {
return mSteps;
}
public void setSteps(List &lt; Step &gt; steps) {
mSteps = steps;
}
public Integer getServings() {
return mServings;
}
public void setServings(Integer servings) {
mServings = servings;
}
public String getImage() {
return mImage;
}
public void setImage(String image) {
mImage = image;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (mId == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeInt(mId);
}
dest.writeString(mName);
if (mServings == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeInt(mServings);
}
dest.writeString(mImage);
}
}

Ingredient.java

public class Ingredient {
@SerializedName(&quot;quantity&quot;)
@Expose
private Double mQuantity;
@SerializedName(&quot;measure&quot;)
@Expose
private String mMeasure;
@SerializedName(&quot;ingredient&quot;)
@Expose
private String mIngredient;
public Double getQuantity() {
return mQuantity;
}
public void setQuantity(Double quantity) {
mQuantity = quantity;
}
public String getMeasure() {
return mMeasure;
}
public void setMeasure(String measure) {
mMeasure = measure;
}
public String getIngredient() {
return mIngredient;
}
public void setIngredient(String ingredient) {
mIngredient = ingredient;
}
}
RecipeActivity
public class RecipeActivity extends AppCompatActivity implements RecipeAdapter.RecipeAdapterOnClickHandler {
private static final String TAG = RecipeActivity.class.getSimpleName();
@BindView(R.id.recipe_recycler_view)
RecyclerView mRecyclerView;
private RecipeAdapter mRecipeAdapter;
private List &lt; Recipe &gt; recipes;
public static final String MY_RECIPE = &quot;myRecipe&quot;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe);
RecipeService service = RecipeClient.getRetrofit().create(RecipeService.class);
Call &lt; List &lt; Recipe &gt;&gt; call = service.getAllRecipes();
call.enqueue(new Callback &lt; List &lt; Recipe &gt;&gt; () {
@Override
public void onResponse(Call &lt; List &lt; Recipe &gt;&gt; call, Response &lt; List &lt; Recipe &gt;&gt; response) {
if (response.isSuccessful()) {
recipes = response.body();
generateDataList(recipes);
}
}
@Override
public void onFailure(Call &lt; List &lt; Recipe &gt;&gt; call, Throwable t) {
Toast.makeText(RecipeActivity.this, &quot;Something went wrong...Please try later!&quot;, Toast.LENGTH_SHORT).show();
Log.v(TAG, t.toString());
}
});
}
private void generateDataList(List &lt; Recipe &gt; recipeList) {
ButterKnife.bind(this);
mRecipeAdapter = new RecipeAdapter(this, recipeList, RecipeActivity.this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(RecipeActivity.this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mRecipeAdapter);
}
@Override
public void onClick(int adapterPosition) {
Context context = this;
Class detailClass = RecipeDetailsActivity.class;
Intent detailsIntent = new Intent(context, detailClass);
detailsIntent.putExtra(MY_RECIPE, recipes.get(adapterPosition));
startActivity(detailsIntent);
}
}

RecipeDetailsActivity

public class RecipeDetailsActivity extends AppCompatActivity implements IngredientAdapter.IngredientAdapterOnClickHandler {
private static final String TAG = RecipeDetailsActivity.class.getSimpleName();
private Recipe recipes;
private List &lt; Recipe &gt; recipeList;
private String recipeName;
private String ingredient;
private Double quantity;
private String measure;
private List &lt; Ingredient &gt; ingredientList = new ArrayList &lt; &gt; ();
IngredientAdapter mAdapter;
@Nullable
@BindView(R.id.ingredients)
TextView ingredientsTV;
@Nullable
@BindView(R.id.quantity)
TextView quantityTV;
@BindView(R.id.recipe_details_rv)
RecyclerView mRecyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_details);
Intent intentToCatch = getIntent();
recipes = intentToCatch.getParcelableExtra(RecipeActivity.MY_RECIPE);
recipeName = recipes.getName();
ingredientList = recipes.getIngredients();
if (ingredientList != null) {
ingredient = ingredientList.get(0).getIngredient();
quantity = ingredientList.get(0).getQuantity();
measure = ingredientList.get(0).getMeasure();
} else {
Log.v(TAG, &quot;FAILING LOADING INGREDIENTSLIST&quot;);
}
setTitle(recipeName);
ButterKnife.bind(this);
mAdapter = new IngredientAdapter(this, ingredientList, RecipeDetailsActivity.this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(RecipeDetailsActivity.this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onClick(int adapterPosition) {
}
}
RecipeAdapter
public class RecipeAdapter extends RecyclerView.Adapter &lt; RecipeAdapter.RecipeAdapterViewHolder &gt; {
private List &lt; Recipe &gt; mRecipeList;
private Context mContext;
private RecipeAdapterOnClickHandler mOnClickHandler;
public RecipeAdapter(Context context, List &lt; Recipe &gt; recipeList, RecipeAdapterOnClickHandler onClickHandler) {
mContext = context;
mRecipeList = recipeList;
mOnClickHandler = onClickHandler;
}
public interface RecipeAdapterOnClickHandler {
void onClick(int adapterPosition);
}
public class RecipeAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.recipe_name)
TextView mRecipeName;
public RecipeAdapterViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
ButterKnife.bind(this, itemView);
}
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
mOnClickHandler.onClick(adapterPosition);
}
}
@NonNull
@Override
public RecipeAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.recipe_list_item, parent, false);
return new RecipeAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecipeAdapterViewHolder holder, int position) {
holder.mRecipeName.setText(mRecipeList.get(position).getName());
}
@Override
public int getItemCount() {
if (mRecipeList == null) {
return 0;
}
return mRecipeList.size();
}
}

IngredientAdapter

public class IngredientAdapter extends RecyclerView.Adapter &lt; IngredientAdapter.IngredientAdapterViewHolder &gt; {
private List &lt; Ingredient &gt; mIngredient;
private Context mContext;
private IngredientAdapterOnClickHandler mOnClickHandler;
public IngredientAdapter(Context context, List &lt; Ingredient &gt; ingredientList, IngredientAdapterOnClickHandler onClickHandler) {
mContext = context;
mIngredient = ingredientList;
mOnClickHandler = onClickHandler;
}
public interface IngredientAdapterOnClickHandler {
void onClick(int adapterPosition);
}
public class IngredientAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.ingredients)
TextView ingredientTextView;
@BindView(R.id.step_description)
TextView stepsDescriptionTextView;
@BindView(R.id.quantity)
TextView quantityDescriptionTextView;
public IngredientAdapterViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
mOnClickHandler.onClick(adapterPosition);
}
}
@NonNull
@Override
public IngredientAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.recipe_details_list_item, parent, false);
return new IngredientAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull IngredientAdapterViewHolder holder, int position) {
holder.ingredientTextView.setText(mIngredient.get(position).getIngredient());
holder.stepsDescriptionTextView.setText(mIngredient.get(position).getQuantity().toString());
holder.quantityDescriptionTextView.setText(mIngredient.get(position).getMeasure());
}
@Override
public int getItemCount() {
if (mIngredient == null) {
return 0;
}
return mIngredient.size();
}
}

答案1

得分: 1

我建议您在RecipeDetailsActivity内部创建一个名为public static Recipe recipes的变量。然后,在打开详细活动时,只需这样操作:RecipeDetailsActivity.recipes = recipes.get(adapterPosition); 这将是我在这种情况下会采取的更简单的解决方案。

英文:

I would suggest you to make Recipe recipes variable inside a RecipeDetailsActivity public static variable. Like public static Recipe recipes. And when you're opening the Detailed activity just it like this: RecipeDetailsActivity.recipes = recipes.get(adapterPosition); This would be an easier solution I would do in this case.

huangapple
  • 本文由 发表于 2020年4月7日 22:46:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61082766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定