我正在开发一个购物清单应用程序,我正在解决问题,并在数组中添加项目。
我有 output_txt.text 和 input_txt.text ,其中包含三个按钮 add_btn , remove_btn 和 clear_btn 即可。在我的清单(输出)里面我有“面包,狗粮,鸡蛋,汉堡包,牛奶”的商品。我希望能够将项目添加到列表中并按字母顺序对它们进行排序。
当我添加一个项目时,它会按字母顺序对其进行排序,但是当我尝试添加另一个项目时,它只会替换我输入的最后一个项目。
当我想要清除项目时,我希望能够从列表中复制项目并将其放在输入文本框中,然后单击删除btn将其删除,但是当我这样做时它只删除第二项和然后将它放在列表的底部。
( totalItems_txt.text 是我在列表中添加和删除的项目总数。)
这是我的代码:
clear_btn.addEventListener(MouseEvent.CLICK, ClearList);
function ClearList(e:MouseEvent):void {
output_txt.text = "";
totalItems_txt.text = "0";
}
addItem_btn.addEventListener(MouseEvent.CLICK, AddItem);
function AddItem(e:MouseEvent):void {
var newItems:Array = ["Bread", "Dog Food", "Eggs", "Hamburger", "Milk"];
newItems[0] = "Bread";
newItems[1] = "Dog Food";
newItems[2] = "Eggs";
newItems[3] = "Hamburger";
newItems[4] = "Milk";
newItems[5] = input_txt.text;
newItems.sort(Array.CASEINSENSITIVE);
input_txt.text = "";
output_txt.text = "";
for (var i:int = 0; i < newItems.length; i++){
output_txt.appendText(newItems[i] + "\n");
}
totalItems_txt.text = newItems.length.toString();
}
remove_btn.addEventListener(MouseEvent.CLICK, RemoveItems);
function RemoveItems(e:MouseEvent):void {
var items:Array = ["Bread", "Dog Food", "Eggs", "Hamburger", "Milk"];
items[0] = "Bread";
items[1] = "Dog Food";
items[2] = "Eggs";
items[3] = "Hamburger";
items[4] = "Milk";
items[5] = input_txt.text;
output_txt.text = "";
items.splice(1,1);
for (var i:int = 0; i < items.length; i++){
output_txt.appendText(items[i] + "\n");
}
totalItems_txt.text = items.length.toString();
}
答案 0 :(得分:1)
如果您提供complete and verifiable example,则更容易确定问题的原因,但问题的核心是对Array
方法的理解:
splice(startIndex:int, deleteCount:uint)
,用于删除项目。push(... args)
,用于添加项目。如果您明确引用newItems[0]
至[5]
,那么您只会影响条目0 - 5,但push
很有用,因为它只是将其添加到最后你的阵列。相反,您可以使用splice
(专门针对特定索引)或pop()
(从数组中删除最后一个元素)来删除数组中的项目。
另一个问题是您的阵列是本地的。由于您每次拨打RemoveItems
或AddItem
时都要重新创建,因此您永远不会保存更改。因此,将其移到这些功能之外,并在点击之间保存。
我已经通过更改重新编写了代码,并为您未提供的缺失的UI代码添加了补充代码。您可以在新的.fla
文件中运行它,它将按预期工作。
import flash.text.TextField;
import flash.events.KeyboardEvent;
import flash.display.Sprite;
var items:Array = ["Bread", "Dog Food", "Eggs", "Hamburger", "Milk"];
function addItem(e:MouseEvent):void {
if (input_txt.text != "") { // Assuming we having something in the input_txt,
items.push(input_txt.text); // add it to the end of our items array
input_txt.text = "";
updateOutput();
}
}
function removeItems(e:MouseEvent):void {
items.splice(0,1);
updateOutput();
}
function clearList(e:MouseEvent):void {
items = []; // empty our list by replacing it with a new one.
output_txt.text = "";
totalItems_txt.text = "0";
}
function updateOutput():void {
items.sort(Array.CASEINSENSITIVE);
output_txt.text = "";
for (var i:int = 0; i < items.length; i++){
output_txt.appendText(items[i] + "\n");
}
totalItems_txt.text = "Total: " + items.length;
}
/* Supplemental UI Creation */
var addItem_btn:Sprite = new Sprite();
addItem_btn.graphics.beginFill(0xa1FFa1,1);
addItem_btn.graphics.drawRect(0,0,100,25)
addChild(addItem_btn);
addItem_btn.addEventListener(MouseEvent.CLICK, addItem);
var clear_btn:Sprite = new Sprite();
clear_btn.graphics.beginFill(0xa1a1a1,1);
clear_btn.graphics.drawRect(0,0,100,25)
addChild(clear_btn);
clear_btn.x = 101;
clear_btn.addEventListener(MouseEvent.CLICK, clearList);
var remove_btn:Sprite = new Sprite();
remove_btn.graphics.beginFill(0xFFa1a1,1);
remove_btn.graphics.drawRect(0,0,100,25)
addChild(remove_btn);
remove_btn.x = 202;
remove_btn.addEventListener(MouseEvent.CLICK, removeItems);
var input_txt:TextField = new TextField();
addChild(input_txt);
input_txt.type = "input";
input_txt.text = "input_txt";
input_txt.y = 50;
var output_txt:TextField = new TextField();
addChild(output_txt);
output_txt.text = "output_txt";
output_txt.y = 50;
output_txt.x = 101;
var totalItems_txt:TextField = new TextField();
addChild(totalItems_txt);
totalItems_txt.text = "totalItems_txt";
totalItems_txt.y = 50;
totalItems_txt.x = 202;
答案 1 :(得分:0)
您所要做的就是将您的数组声明为public,然后使用push()
将值添加到数组中。当你排序数组的实际索引仍然存在,但显示的项目索引将是不同的,