简体   繁体   English

从演示者(MVP)更新组合框

[英]Update a combobox from a presenter (MVP)

I am using MVP in my project, but i am new in MVP. 我在项目中使用MVP,但是我是MVP的新手。 I have two comboboxes. 我有两个组合框。 When I select an option in a combobox, the another combobox should be filled with new data. 当我在组合框中选择一个选项时,另一个组合框中应填充新数据。

This action will be in Presenter. 此操作将在Presenter中进行。 I get my view 'view1' in Presenter, and introduced Combobox1 and Combobox2 as properties in 'view1', because I need 'DataSource', 'DisplayMember', 'ValueMember' and 'Refresh()' in the method below. 我在Presenter中获得视图“ view1”,并在“ view1”中引入了Combobox1和Combobox2作为属性,因为在下面的方法中需要“ DataSource”,“ DisplayMember”,“ ValueMember”和“ Refresh()”。

But, when using a pattern, it is enough to send a property like 但是,当使用模式时,发送诸如

public string Combobox2
{
    get { return comboBox1.SelectedValue.ToSstring(); }
}

into Presenter not the whole Combobox. 进入Presenter而不是整个组合框。 How can I solve this problem? 我怎么解决这个问题?

public void OnSelectedIndexChangedCombobox1()
{
    if (view1.Combobox1.SelectedIndex == -1)
    {
        return;
    }

    DataTable dt = Tools.GetDataTable("A Path");

    var query =
        (from o in dt.AsEnumerable()
         where o.Field<string>("afield") == 
             farmerView.Combobox1.SelectedValue.ToString()
         orderby o.Field<string>("anotherfield")
         select new KeyValuePair<string, string>(o.Field<string>("field1"), 
             o.Field<string>("field2"))).ToList();

    farmerView.Combobox2SelectedIndexChanged -= OnSelectedIndexChangedCombobox2;

    farmerView.Combobox2.DataSource = new BindingSource(query, null);
    farmerView.Combobox2.DisplayMember = "Value";  
    farmerView.Combobox2.ValueMember = "Key";   
    farmerView.Combobox2.Refresh();
    farmerView.Combobox2SelectedIndexChanged += 
       OnSelectedIndexChangedCombobox2;

    farmerView.Combobox2.SelectedIndex = -1;
}

Thank you 谢谢

You should not pass any Android objects to presenter, just get the event in view (for instance your Activity) then call a method from presenter that provides data for second ComboBox (we call it Spinner in Android!) by passing selected item from first one, and then presenter will call a method of View which fill the second one and View knows how to do it. 您不应将任何Android对象传递给演示者,只需在事件视图中获取事件(例如您的Activity),然后从演示者中调用一个方法即可为第二个ComboBox(在Android中称为Spinner!)提供数据,方法是传递第一个对象中的选定项,然后演示者将调用View的方法,该方法将填充第二个方法,并且View知道如何执行此方法。

You can take a look at this sample project http://github.com/mmirhoseini/marvel and this article https://hackernoon.com/yet-another-mvp-article-part-1-lets-get-to-know-the-project-d3fd553b3e21 to get more familiar with MVP. 您可以看一下这个示例项目http://github.com/mmirhoseini/marvel和本文https://hackernoon.com/yet-another-mvp-article-part-1-lets-get-to-know -the-project-d3fd553b3e21以更熟悉MVP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM