0
votes

I am using MVP in my project, but i am new in 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. 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.

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. 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

1

1 Answers

1
votes

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.

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.