gpt4 book ai didi

java - 为什么我的 parceled 对象返回为空?安卓开发

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:19 24 4
gpt4 key购买 nike

我一直在尝试打包一个对象并将其值返回给 MainActivity 方法。当我在我的 AddItemActivity 中记录 mItem 对象时,所有对象的属性都已分配,然后再将其全部 checkout 。我的数据在某个地方丢失了......无论是从我创建新 Intent 的一开始,还是在 MainActivity 的接收端。我想做的是……获取用户输入,创建项目对象,将该对象传递给 MainActivity,然后将其添加到 Inventory。有什么想法吗?

这是主要 Activity :

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.gardnerwebideas.inventorytracker.R;
import com.gardnerwebideas.inventorytracker.adapters.ItemAdapter;
import com.gardnerwebideas.inventorytracker.models.Inventory;
import com.gardnerwebideas.inventorytracker.models.Item;

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

public class MainActivity extends ListActivity {

private static final String TAG = MainActivity.class.getSimpleName();
private Item mNewItem;
private Inventory mInventory;

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

Button addButton = (Button) findViewById(R.id.addToInventoryButton);

mInventory = new Inventory();
mNewItem = new Item();

addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addToInventory();
}
});

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch(requestCode) {
case (1): {
if (resultCode == Activity.RESULT_OK) {
Intent intent = getIntent();
mNewItem = intent.getParcelableExtra(AddItemActivity.ADD_ITEM);
new ItemAdapter(this, mInventory);
Toast.makeText(this,
"Inventory size: "
+ String.valueOf(mInventory.getSize())
+ mInventory.toString(),
Toast.LENGTH_LONG).show();
}
break;
}
default:
break;
}
}

public void addToInventory() {
Intent intent = new Intent(this, AddItemActivity.class);
startActivityForResult(intent, 1);
}

}

这是 AddItemActivity:

import android.app.Activity;
import android.content.Intent;
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.EditText;
import android.widget.Toast;

import com.gardnerwebideas.inventorytracker.models.Item;
import com.gardnerwebideas.inventorytracker.R;

import java.util.Date;

public class AddItemActivity extends AppCompatActivity {

public static final String TAG = AddItemActivity.class.getSimpleName();

public static final String ADD_ITEM = "ADD_ITEM";
private Item mItem;
private String mName;
private String mDate;
private String mDescription;
private String mPrice;

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

Button addButton = (Button) findViewById(R.id.addButton);

addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNewItem();
mItem = new Item(mName, mDescription, mPrice, mDate);
Log.d(TAG, "THE ITEM IS: " + mItem.toString());

Intent intent = new Intent();
intent.putExtra(ADD_ITEM, mItem);
setResult(Activity.RESULT_OK, intent);
finish();
}
});

}

private void createNewItem() {
EditText itemName = (EditText) findViewById(R.id.nameField);
EditText itemDescription = (EditText) findViewById(R.id.descriptionField);
EditText itemPrice = (EditText) findViewById(R.id.priceField);

mName = itemName.getText().toString();
mDescription = itemDescription.getText().toString();
mPrice = itemPrice.getText().toString();
mDate = String.valueOf(new Date());
}

}

项目类:

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Date;

public class Item implements Parcelable {

private String mName;
private String mInventoryDate;
private String mDescription;
private String mPrice;

public Item(String name, String description, String price, String stringDate) {
mName = name;
mInventoryDate = stringDate;
mDescription = description;
mPrice = price;
}

public String[] getItemInfo() {
String[] itemInfo = {mName, mDescription, mInventoryDate, mPrice};
return itemInfo;
}

public String getName() {
return mName;
}

public void setName(String name) {
mName = name;
}

public String getInventoryDate() {
return mInventoryDate;
}

public void setInventoryDate(String stringDate) {
mInventoryDate = stringDate;
}

public String getDescription() {
return mDescription;
}

public void setDescription(String description) {
mDescription = description;
}

public String getPrice() {
return mPrice;
}

public void setPrice(String price) {
mPrice = price;
}

@Override
public String toString() {
return mName + " " + mInventoryDate + " " + mDescription + " " + mPrice;
}

@Override
public int describeContents() {
return 0; //Not necessary...
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeString(mInventoryDate);
dest.writeString(mDescription);
dest.writeString(mPrice);
}

private Item(Parcel in) {
mName = in.readString();
mInventoryDate = in.readString();
mDescription = in.readString();
mPrice = in.readString();
}

public static final Creator<Item> CREATOR = new Creator<Item>() {
@Override
public Item createFromParcel(Parcel source) {
return new Item(source);
}

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

}

和 Inventory 类:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Inventory {

private List<Item> mInventoryItems;

public Inventory() {
mInventoryItems = new ArrayList<>();
}

public int getSize() {
return mInventoryItems.size();
}

public void addItem(Item item) {
mInventoryItems.add(item);
}

public Item getPosition(int position) {
return mInventoryItems.get(position);
}

public List<Item> getInventoryItems() {
return mInventoryItems;
}

public void setInventoryItems(List<Item> items) {
mInventoryItems = items;
}

@Override
public String toString() {
return "Inventory{" +
"mInventoryItems=" + mInventoryItems +
'}';
}

}

我已经花了大约 18 个小时试图弄清楚这个问题,但我只是遗漏了一些最后的部分,我就是知道。

最佳答案

问题出在您的 onActivityResult() 方法中,您在其中调用:

Intent intent = getIntent();

getIntent() 返回用于创建 Activity 的 Intent。您想使用传递给 onActivityResult() 方法的 Intent,所以:

mNewItem = data.getParcelableExtra(AddItemActivity.ADD_ITEM);

关于java - 为什么我的 parceled 对象返回为空?安卓开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com