按钮中适配器内部的StartActivityForResult不会触发OnActivityResult

时间:2019-05-11 05:33:44

标签: c# android visual-studio

当我按下按钮没有成功时,我尝试使用以下代码在我的适配器内触发活动结果。 TraEdit活动已正确启动并更新了我的数据。我无法使用NotifyDataSetChanged()刷新列表,因此不会激活OnActivityResult来控制结果。 我做错了什么?有没有办法在我的适配器内NotifyDataSetChanged(),还是我不得不尝试其他方法? 任何帮助表示赞赏。

using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Views;
using Android.Widget;

namespace Cashier
{
    [Activity(Label = "TraBaseAdapter")]
    public partial class TraBaseAdapter : BaseAdapter<TraTable>
    {
        List<TraTable> TraListArrayList;
        private LayoutInflater mInflater;
        private Context activity;
        public TraBaseAdapter(Context context, List<TraTable> results)
        {
            activity = context;
            TraListArrayList = results;
            mInflater = (LayoutInflater)activity.GetSystemService(Context.LayoutInflaterService);
        }
        public override int Count { get { return TraListArrayList.Count; } }
        public override long GetItemId(int position) { return position; }
        public override TraTable this[int position] { get { return TraListArrayList[position]; } }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            TraViewHolder holder = null;
            if (convertView == null)
            {
                convertView = mInflater.Inflate(Resource.Layout.TraRecord_view, null);
                holder = new TraViewHolder
                {
                    TxtBtnEdit = convertView.FindViewById<Button>(Resource.Id.TraEditButtonHolder),
                    TxtTraName = convertView.FindViewById<TextView>(Resource.Id.TraNameHolder),
                    TxtTraCode = convertView.FindViewById<TextView>(Resource.Id.TraCodeHolder)
                };
                if (!holder.TxtBtnEdit.HasOnClickListeners)
                {
                    holder.TxtBtnEdit.Click += (sender, e) => 
                    {
                        TraEditClick(holder);
                    };
                }
                convertView.Tag = holder;
            }
            else { holder = convertView.Tag as TraViewHolder; }
            holder.LineId = position;
            holder.TraId = TraListArrayList[position].Cs_Traid;
            holder.TxtBtnEdit.Tag = holder.LineId + 1;
            holder.TxtTraName.Text = TraListArrayList[position].Cs_Name;
            holder.TxtTraCode.Text = TraListArrayList[position].Cs_Code;
            return convertView;
        }
        private void TraEditClick(TraViewHolder LineHolder)
        {
            Intent ActivityAddEditTra = new Intent(this.activity, typeof(TraEdit));
            ActivityAddEditTra.PutExtra("TraIdSel", TraListArrayList[LineHolder.LineId].Cs_Traid);
            ActivityAddEditTra.PutExtra("NameSel", TraListArrayList[LineHolder.LineId].Cs_Name);
            ActivityAddEditTra.PutExtra("TraCodeSel", TraListArrayList[LineHolder.LineId].Cs_Code);
            ((Activity)activity).StartActivityForResult(ActivityAddEditTra, 99);
        }
    public void OnActivityResult(int RequestCode, Result ResultCode, Intent Data)
    {
        if (ResultCode == Result.Ok & RequestCode == 99)
        {
            int RowUpdate = Data.GetIntExtra("RowUpdate", 0);
            NotifyDataSetChanged();
            Toast.MakeText(activity, "Data changes :" + RowUpdate.ToString(), ToastLength.Short).Show();
        }
    }
        public class TraViewHolder : Java.Lang.Object
        {
            public TextView TxtTraCode { get; set; }
            public TextView TxtTraName { get; set; }
            public Button TxtBtnEdit { get; set; }
            public int LineId { get; set; }
            public int TraId { get; set; }
        }
    }
}

在TraEdit中:

......
                Intent RetunData = new Intent();
                SetResult(Result.Ok, RetunData);
                RetunData.PutExtra("RowUpdate", RowsUpd);
......

1 个答案:

答案 0 :(得分:0)

也许

RetunData.PutExtra("RowUpdate", RowsUpd);

应该先于

SetResult(Result.Ok, RetunData);

我对VS不好,但是也许OnActivityResult方法必须在Activity中,而不是adapater