我在这里有一些代码,简短的灰色解释(编码注释):
namespace FridgeApp
{
[Activity( MainLauncher = true, Icon = "@drawable/icon")]
public class MenuActivity : Activity
{
public string content;
ListView productsListView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Menu);
//GETTING ACCES TO XML FILES
AssetManager assets = this.Assets;
//PLACING CONTENT OF XML FILE "products.xml" TO 'content' VARIABLE
using (StreamReader sr = new StreamReader(Assets.Open("products.xml")))
{
content = sr.ReadToEnd();
}
List<Product> _productsList = ProductsFromXml();
//CONVERTING FROM LIST TO AN ARRAY
Array prdcts = _productsList.ToArray();
Button button = FindViewById<Button>(Resource.Id.button1);
//BY CLICKING A BUTTON APP SETS ANOTHER LAYOUT AND DISPLAYS A LIST OF PRODUCTS
button.Click += delegate
{
SetContentView(Resource.Layout.ProductsList);
productsListView = FindViewById<ListView>(Resource.Id.listView1);
productsListView.Adapter = new ArrayAdapter (this, Android.Resource.Layout.SimpleListItemMultipleChoice, prdcts );
productsListView.ChoiceMode = ChoiceMode.Multiple;
};
}
//THIS MEHOD READS ACTUAL DATA FROM XML FILE "products.xml" AND PLACES IT IN LIST 'productsList'
public List<Product> ProductsFromXml()
{
List<Product> productsList = new List<Product>();
XmlDocument productsXml = new XmlDocument();
productsXml.LoadXml(content);
XmlNode root = productsXml.SelectSingleNode("CurrentProducts");
XmlNodeList nodeList = root.SelectNodes("Product");
foreach (XmlNode n in nodeList)
{
Product p = new Product();
p.Name = n.SelectSingleNode("Name").InnerText;
p.Quantity = n.SelectSingleNode("Quantity").InnerText;
productsList.Add(p);
}
return productsList;
}
}
}
对于每个产品&#39;名称&#39;和&#39;数量&#39;有一个类,这些参数创建名为&#39;产品&#39;。 有一个XML文件:
<CurrentProducts>
<Product>
<Name>Apple</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Banana</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Bread</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Butter</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Cucumber</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Milk</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Cheese</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Smoked Sausage</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Tomato</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Egg</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Chicken</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Chocolate</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Potato</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Onion</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Lettuce</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Sour Cream</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Tomato Sauce</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Carrot</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Orange Juice</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Salmon</Name>
<Quantity>1</Quantity>
</Product>
<Product>
<Name>Flavour</Name>
<Quantity>1 kg</Quantity>
</Product>
<Product>
<Name>Salt</Name>
<Quantity>1 kg</Quantity>
</Product>
<Product>
<Name>Sugar</Name>
<Quantity>1 kg</Quantity>
</Product>
以下是点击按钮并显示数组内容时仿真器屏幕的照片:
问题是,为什么它显示Products.Product
而不是products.xml
中写的实际产品?
以下是该类的代码:
public class Product
{
public string Name;
public string Quantity;
public MeasurementType UnitMeasure;
public Product()
{
}
public Product(string _name)
{
this.Name = _name;
}
答案 0 :(得分:0)
您应该共享Product
类的代码;我相信ListView
正在对产品对象调用ToString()
,他们只是返回类型名称,因为类型不知道更好。
尝试添加:
class Product
{
public override string ToString()
{
// Format your product name here as you want it to be displayed.
return $"{Name} - {Quantity}";
}
}