我试图用列表中的对象填充列表视图,但只显示名称属性,同时允许用户在列表视图中多选项目

问题描述 投票:0回答:1

我最近开始第一次使用Android应用程序。我正在尝试将一个Ingredient对象列表添加到listview并获取listview以仅显示Ingredient对象上的name属性。

下面的代码显示了视图中的整个对象(WeeklyMealGenerator.Models.Ingredient)。我还需要确保解决方案仍允许我从所选对象中拉出对象

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    >
        <LinearLayout
        android:layout_width ="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop = "0.0dp"
        android:layout_marginLeft = "10dp"
        android:layout_marginBottom="0.0dp"
        >
        <TextView
            android:layout_width       ="wrap_content"
            android:layout_height      ="wrap_content"
            android:text               ="Meal name:"
            android:layout_marginRight ="10dp"
            android:layout_marginBottom="6.6dp"
            android:layout_marginTop   ="5dp" />
        <EditText
            android:id            ="@+id/MealName"
            android:layout_gravity="center"
            android:layout_width  ="190dp"
            android:layout_height ="37.5dp"
            android:inputType = "number"
            android:textSize = "12dp"
            android:layout_marginTop="0.0dp"
            android:layout_marginBottom="0.0dp" />
    </LinearLayout>
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Ingredients" />

</LinearLayout>



namespace WeeklyMealGenerator.Models
{
    public class Ingredient
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }
    }
}




namespace WeeklyMealGenerator.Activities
{
    [Activity(Label = "AddMealAcitivity")]
    public class AddMealActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.AddMeal);

            Ingredient[] ingredients =
            {
                new Ingredient{Name = "Carrot"},
                new Ingredient{Name = "Steak"},
                new Ingredient{Name = "Potatoes"},
                new Ingredient{Name = "Roast Chicken"},
                new Ingredient{Name = "Pasta"},
                new Ingredient{Name = "Pasta Sauce"}
            };

            ListView listView = FindViewById<ListView>(Resource.Id.Ingredients);
            ArrayAdapter<Ingredient> adapter = new ArrayAdapter<Ingredient>(this, Android.Resource.Layout.SimpleListItemMultipleChoice, ingredients);
            listView.SetAdapter(adapter);
            listView.ChoiceMode = ChoiceMode.Multiple;

        }
    }
}
c# android xamarin
1个回答
0
投票

经过几个小时的研究,我终于发现它在模型上调用了ToString方法。我只是必须覆盖它才能使它工作。

© www.soinside.com 2019 - 2024. All rights reserved.