How to assign OnActivityResult values to the Listv

2019-07-28 00:14发布

问题:

I have a Listview and Click Events in the respective listItems and need to get result back from the click events and populate on the List.

My BaseAdapter class is

 public class RemnantListAdapter : BaseAdapter<InventorySlabs>
 {
    private PartialInventory context;
    private List<InventorySlabs> RemnantList;

    public RemnantListAdapter(PartialInventory partialInventory, List<InventorySlabs> remnantList)
    {
        this.context = partialInventory;
        this.RemnantList = remnantList;
    }

    public override InventorySlabs this[int position]
    {
        get
        {
            return RemnantList[position];
        }
    }

    public override int Count
    {
        get
        {
            return RemnantList.Count;
        }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override int ViewTypeCount
    {
        get
        {
            return Count;
        }
    }

    public override int GetItemViewType(int position)
    {
        return position;
    }

    private class remnantHolder : Object
    {
        public Button EditRemnant1;

        public TextView Remnant1 = null;
        public TextView Rem_StockNMaterial1 = null;
        public TextView Rem_Dimensions1 = null;
        public TextView Rem_Status1 = null;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        remnantHolder holder = null;

        if (convertView == null)
        {
            convertView = (convertView ?? context.LayoutInflater.Inflate(Resource.Layout.remnant_list, parent, false));

            holder = new remnantHolder();                
            holder.EditRemnant1 = convertView.FindViewById<Button>(Resource.Id.editRemnant1);              

            holder.Remnant1 = convertView.FindViewById<TextView>(Resource.Id.remnant1);
            holder.Rem_StockNMaterial1 = convertView.FindViewById<TextView>(Resource.Id.remnant_mtrl1);
            holder.Rem_Dimensions1 = convertView.FindViewById<TextView>(Resource.Id.remnant_dimens1);
            holder.Rem_Status1 = convertView.FindViewById<TextView>(Resource.Id.remnant_status1);

            try
            {
                InventorySlabs remnantModel = this.RemnantList[position];
                if (remnantModel != null)
                {
                    holder.Remnant1.Text = "#" + remnantModel.ExtSlabNo;
                    holder.Rem_StockNMaterial1.Text = remnantModel.SellName + "-" + remnantModel.Depth + "-" + remnantModel.Finish;
                    double sqft = (remnantModel.Width * remnantModel.Height) / 144;
                    holder.Rem_Dimensions1.Text = "(" + remnantModel.Width.ToString() + " * " + remnantModel.Height.ToString() + ")" + sqft.ToString("N2");
                    holder.Rem_Status1.Text = remnantModel.Status;

                    holder.EditRemnant1.Click += delegate (object sender, System.EventArgs args)
                    {
                        if (remnantModel != null)
                        {
                            Intent intent = new Intent(context, typeof(EditRemnant));
                            intent.PutExtra("OpenPopType", 2);
                            intent.PutExtra("SlabNo", remnantModel.ExtSlabNo);
                            context.StartActivityForResult(intent, 1);
                        }
                    };                      
                }
            }
            catch (Exception ex)
            {
                var method = System.Reflection.MethodBase.GetCurrentMethod();
                var methodName = method.Name;
                var className = method.ReflectedType.Name;

                MainActivity.SaveLogReport(className, methodName, ex);
            }

            convertView.Tag = holder;
        }
        else
        {
            holder = (remnantHolder)convertView.Tag;
        }
        return convertView;
    }

    public void ActivityResult(int requestCode, Result resultCode, Intent data)
    {
        switch (requestCode)
        {              
            case 1:
                if (data != null)
                {
                    try
                    {
                        string remSlab = data.GetStringExtra("extSlabNo");
                        if (remSlab != null)
                        {
                            remnantData.Width = double.Parse(data.GetStringExtra("remWidth"));
                            remnantData.Height = double.Parse(data.GetStringExtra("remHeight"));
                            double sqft = (remnantData.Width * remnantData.Height) / 144;

                            Rem_Dimensions.Text = "(" + remnantData.Width.ToString() + " * " + remnantData.Height.ToString() + ")" + sqft.ToString("N2");
                        }
                    }
                    catch (Exception ex)
                    {
                        var method = System.Reflection.MethodBase.GetCurrentMethod();
                        var methodName = method.Name;
                        var className = method.ReflectedType.Name;

                        MainActivity.SaveLogReport(className, methodName, ex);
                    }
                }
                break;
        }
    }
}

In my Main Activity

I am calling the Result as

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    remnantListAdapter.ActivityResult(requestCode, resultCode, data);            
}

After editing the details in Edit view of the ListItem and close the Popup, I am not knowing how to Pass the Values to the Holder to Update in Listview.

回答1:

You could pass the positon to the new activity in the click event and pass it back with the result intent.

For example:

    holder.EditRemnant1.Click += delegate (object sender, System.EventArgs args)
    {
        if (remnantModel != null)
        {
            Intent intent = new Intent(context, typeof(EditRemnant));
            intent.PutExtra("OpenPopType", 2);
            intent.PutExtra("SlabNo", remnantModel.ExtSlabNo);
            intent.PutExtra("Position", position );
            context.StartActivityForResult(intent, 1);
        }
    };

And in the new activity:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        Intent intent = Intent;
        var position =intent.GetIntExtra("Position", -1);
        base.OnCreate(savedInstanceState);
        Intent data = new Intent();
        String text = "extSlabNo";
        data.PutExtra("extSlabNo", text);
        data.PutExtra("remWidth", 10);
        data.PutExtra("remHeight", 20);
        data.PutExtra("Position", position);
        SetResult(Result.Ok, data);
    }

Then you could modify the data in the adapter according to the positon value:

    public void ActivityResult(int requestCode, Result resultCode, Intent data)
    {
        switch (requestCode)
        {
            case 1:
                if (data != null)
                {
                    try
                    {
                        string remSlab = data.GetStringExtra("extSlabNo");
                        int position = data.GetIntExtra("Position", -1);
                        if (remSlab != null && position >= 0)
                        {
                            RemnantList[position].Width = data.GetIntExtra("remWidth", -1);
                            RemnantList[position].Height = data.GetIntExtra("remHeight", -1);
                            NotifyDataSetChanged();
                        }
                    }
                    catch (Exception ex)
                    {
                        var method = System.Reflection.MethodBase.GetCurrentMethod();
                        var methodName = method.Name;
                        var className = method.ReflectedType.Name;
                        MainActivity.SaveLogReport(className, methodName, ex);
                    }
                }
                break;
        }

And reset the adapter in MainActivity OnActivityResult() method :

public class MainActivity : AppCompatActivity
{
    RemnantListAdapter remnantListAdapter;
    ListView listView;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        List<InventorySlabs> remnantList = new List<InventorySlabs>();
        for(var i = 0; i < 10; i++)
        {
            InventorySlabs inventorySlabs = new InventorySlabs();
            inventorySlabs.Depth = i.ToString();
            inventorySlabs.ExtSlabNo = i.ToString();
            inventorySlabs.Finish = "Finish" + i.ToString();
            inventorySlabs.Height = 200;
            inventorySlabs.SellName = "SellName" + i.ToString();
            inventorySlabs.Status = "Status" + i.ToString();
            inventorySlabs.Width = 400;
            remnantList.Add(inventorySlabs);
        }
        listView = FindViewById<ListView>(Resource.Id.listView1);
        remnantListAdapter = new RemnantListAdapter(this, remnantList);
        listView.Adapter = remnantListAdapter;
    }

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        remnantListAdapter.ActivityResult(requestCode, resultCode, data);
        listView.Adapter = remnantListAdapter;
    }      
}