我有一个由静态ArrayList对象组成的自定义类。出于某种原因,我似乎无法弄清楚,当我调用的另一个 ArrayList对象上的clear()方法时,对象中的元素正在被覆盖。这就是我说的是:在itemsArray.clear()行自定义类变量被某些未知进程覆盖
通知,我将解释进一步向下跌破
public class AddItemsActivity extends AppCompatActivity{
// Global variables
// For the Description
private EditText descEditText;
// For the Price
private EditText priceEditText;
// Temporary array to store the list of items which will be passed into the Diner
public static ArrayList<Item> itemsArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// THIS HAS PROBLEM!!!!
itemsArray.clear();
// Set the content to use the activity_add_items xml file
setContentView(R.layout.activity_add_items);
final ItemsListAdapter adapter = new ItemsListAdapter(this, itemsArray);
// Find the ListView to display the adapter on
ListView listView = (ListView) findViewById(R.id.items_list);
// Set the ListView with the adapter
listView.setAdapter(adapter);
Button addDetailsFragment = (Button) findViewById(R.id.add_item_button);
addDetailsFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Setting up a new dialog
final Dialog dialog = new Dialog(AddItemsActivity.this);
dialog.setContentView(R.layout.item_add_dialog);
dialog.setCancelable(true);
dialog.setTitle(R.string.add_item_title);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(false);
Button num1 = (Button) dialog.findViewById(R.id.key_1);
Button num2 = (Button) dialog.findViewById(R.id.key_2);
Button num3 = (Button) dialog.findViewById(R.id.key_3);
Button num4 = (Button) dialog.findViewById(R.id.key_4);
Button num5 = (Button) dialog.findViewById(R.id.key_5);
Button num6 = (Button) dialog.findViewById(R.id.key_6);
Button num7 = (Button) dialog.findViewById(R.id.key_7);
Button num8 = (Button) dialog.findViewById(R.id.key_8);
Button num9 = (Button) dialog.findViewById(R.id.key_9);
Button num0 = (Button) dialog.findViewById(R.id.key_0);
Button numPeriod = (Button) dialog.findViewById(R.id.key_period);
Button numDel = (Button) dialog.findViewById(R.id.key_del);
Button numAdd = (Button) dialog.findViewById(R.id.key_add_item);
// Properties for description field
descEditText = (EditText) dialog.findViewById(R.id.details_desc_input);
// Properties for price field
priceEditText = (EditText) dialog.findViewById(R.id.details_price_input);
// Keypad OnClickListener to append or delete digits in the price input field
View.OnClickListener keyOnClickListener = new View.OnClickListener() {
@Override
// Program logic when one of the buttons is pressed
public void onClick(View v) {
priceEditText.requestFocus();
CharSequence originalText = priceEditText.getText();
Button button = (Button)v;
// Prevents app from crashing when trying to delete an empty field
if (button.getText() == getString(R.string.details_button_delete_text)
&& originalText != null && originalText.length()>0) {
// Deletes one character/digit, deletes '$' if required
if (originalText.length() == 2 && originalText.charAt(0) == '$') {
priceEditText.setText("");
} else {
priceEditText.setText("");
priceEditText.append(originalText.subSequence(0, originalText.length() - 1));
}
// Delete key does nothing instead of displaying 'Del'
} else if (button.getText() == getString(R.string.details_button_delete_text)) {
priceEditText.append("");
// Prevents a second period from appearing in the field
} else if (button.getText().toString()
.equals(getString(R.string.details_button_period_text))
&& originalText.toString().contains(".")) {
priceEditText.append("");
// Adds a $ sign
} else if (originalText == null || originalText.length() == 0) {
priceEditText.append("$");
priceEditText.append(button.getText());
// Else, input the digit pressed
} else {
priceEditText.append(button.getText());
}
}
};
// Add key OnClickListener to add items into Diner's editItemsArray
View.OnClickListener addOnClickListener = new View.OnClickListener() {
@Override
// Program logic when one of the buttons is pressed
public void onClick(View v) {
priceEditText.requestFocus();
CharSequence originalText = priceEditText.getText();
Button button = (Button)v;
// If the input field is empty, do nothing. Else, add the items
if (originalText == null && button.getText() == getString(R.string.details_button_add_text)) {
priceEditText.append("");
// Adds the items into the editItemsArray ArrayList
} else {
Item itemToAdd = new Item(descEditText.getText().toString(),
Double.parseDouble(priceEditText.getText().toString().replace("$",
"")));
itemsArray.add(itemToAdd);
adapter.notifyDataSetChanged();
dialog.dismiss();
Log.e("add item", "works");
}
Log.e("line1", "works");
}
};
num1.setOnClickListener(keyOnClickListener);
num2.setOnClickListener(keyOnClickListener);
num3.setOnClickListener(keyOnClickListener);
num4.setOnClickListener(keyOnClickListener);
num5.setOnClickListener(keyOnClickListener);
num6.setOnClickListener(keyOnClickListener);
num7.setOnClickListener(keyOnClickListener);
num8.setOnClickListener(keyOnClickListener);
num9.setOnClickListener(keyOnClickListener);
num0.setOnClickListener(keyOnClickListener);
numPeriod.setOnClickListener(keyOnClickListener);
numDel.setOnClickListener(keyOnClickListener);
numAdd.setOnClickListener(addOnClickListener);
/*
Request focus for the price input field such that focus is on that field when
the dialog opens
*/
priceEditText.requestFocus();
/*
Hide the appearance of any keyboard when the user presses on the price input field
so the user only uses the provided in app customised keypad
*/
// Hide default keyboard when focus is on this field
priceEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
getWindow().setSoftInputMode(WindowManager
.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Log.e("testonclickclose", "works");
}
});
priceEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
});
priceEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
dialog.show();
// Setting the size of the dialog
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
}
});
Button addNextDinerActivity = (Button) findViewById(R.id.item_done_button);
addNextDinerActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Assign the list of items to specified Diner (selected previously or
// currently adding)
// Get name of current diner
String nameOfCurrentDiner = Diner.getCurrentName();
// Iteration process to find the index of current Diner
int indexOfCurrentDiner = -1;
for (Diner list : AddDinerActivity.dinerArray) {
if (list.getmDinerName().equals(nameOfCurrentDiner)) {
// Stores in index in a variable
indexOfCurrentDiner = AddDinerActivity.dinerArray.indexOf(list);
}
}
// Instantiate a Diner object to be added later
ArrayList<Item> array = getItemsArray();
Diner dinerToAdd = new Diner(nameOfCurrentDiner, getCurrentBill(getItemsArray()), array);
// Sets a new Diner object to the index position found
AddDinerActivity.dinerArray.remove(indexOfCurrentDiner);
AddDinerActivity.dinerArray.add(indexOfCurrentDiner, dinerToAdd);
// Clears the editItemsArray when leaving activity
// Brings the user back to Diner list page
Intent addDiner = new Intent(AddItemsActivity.this, AddDinerActivity.class);
startActivity(addDiner);
}
});
// Method to calculate the current individual's bill
public double getCurrentBill(ArrayList<Item> list) {
double currentBill = 0;
for (Item item : list) {
currentBill += item.getmItemPrice();
}
return currentBill;
}
// Method to get current item array
public ArrayList<Item> getItemsArray() {
return itemsArray;
}
}
我的晚餐类定义为低于由一个名为mDinerItemsList
public class Diner {
public static String currentName;
// To store the currentName
private String mDinerName;
// To store individual's total bill
private double mDinerBill;
// To store the individual's list of items (its description and price)
private ArrayList<Item> mDinerItemsList = new ArrayList<>();
// Class constructor
public Diner(String mDinerName, double mDinerBill, ArrayList<Item> DinerItemsList) {
this.mDinerName = mDinerName;
this.mDinerBill = mDinerBill;
mDinerItemsList = DinerItemsList;
}
// Class constructor with Name ONLY
public Diner(String mDinerName) {
this.mDinerName = mDinerName;
}
// Class constructor with Bill ONLY
public Diner(double mDinerBill) {
this.mDinerBill = mDinerBill;
}
// Class constructor with ItemsList ONLY
public Diner(ArrayList<Item> mDinerItemsList) {
this.mDinerItemsList = mDinerItemsList;
}
// Method to set the current name to identify current diner
public static void setCurrentName(String name) {
currentName = name;
}
// Method to return the current name to identify current diner
public static String getCurrentName() {
return currentName;
}
// Method to return Diner's Name
public String getmDinerName() {
return mDinerName;
}
// Method to return Diner's bill
public double getmDinerBill() {
return mDinerBill;
}
public void setmDinerName(String mDinerName) {
this.mDinerName = mDinerName;
}
// Method to put an item (description and price) into Diner's list of items
public void putmDinerItem(ArrayList<Item> array) {
mDinerItemsList = array;
}
// Method to return Diner's list of items in the form of ArrayList
public ArrayList<Item> getmDinerItemsList() {
return mDinerItemsList;
}
}
截至记者的ArrayList对象活动中的按钮,包含descEditText和priceEditText字符串的单个对象将被添加到itemsArray中。然后这个数组在我实例化一个新的Diner对象的时期被传递给mDinerItemsList。这些恰好在AddItemsActivity结束之前发生。通过调试过程,我发现第二次调用AddItemsActivity时,itemsArray.clear()以某种方式擦除了先前实例化的Diner对象的mDinerItemsList。每当调用活动时都会发生这种情况。因此,我无法在mDinerItemsList中为我想创建的每个Diner对象存储任何ArrayList。我哪里做错了??我一直试图弄清楚这几个小时!请帮忙!!!
问题是你只是传递你的列表而不是创建副本。所以实际上只有一个List对象,但有许多变量指向该列表。 –
你介意进一步阐述吗?当我使用itemsArray的元素分配mDinerItemsList时,不应将该变量“绑定”到Diner对象。因此,itemsArray的任何更改都不应该影响我的Diner类变量吗? –
不知道你的意思是“绑定”。您没有使用新的关键字创建新的ArrayList。因此,不会创建新列表,并且只需将您现有的列表分配给Diner中的mDinerItemsList字段即可。 –