领域正在更新Seekbar

时间:2017-03-03 22:58:30

标签: c# android xamarin xamarin.android realm

我正在使用Realm来保存我的搜索栏更新。

我有一个RecyclerView,将X Items手册添加到RecyclerView, 在项目内部有用户的设置,我希望每个项目的设置分开。

    public static BottomSheet_Faecher newInstance(String mString)
    {
        BottomSheet_Faecher f = new BottomSheet_Faecher();
        Bundle args = new Bundle();
        args.PutString("string", mString);
        f.Arguments = args;
        return f;
    }

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        mString = Arguments.GetString("string");
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.Inflate(Resource.Layout.Settings_Faecher_Wertung, container, false);
        Fach1 = v.FindViewById<SeekBar>(Resource.Id.seek1);
        Fach2 = v.FindViewById<SeekBar>(Resource.Id.seek2);



        var prefs = PreferenceManager.GetDefaultSharedPreferences(this.Activity);
        bool previouslyStarted = prefs.GetBoolean("first", false);



        string name = name = Arguments.GetString("Name");
        Ad = realm.All<SAd>().Where(m => m.mName == name).First();


        if (!previouslyStarted)
          {
            wertungen = new HNWertung { HWertung = Fach1.Progress, NhWertung = Fach2.Progress };
            realm.Write(() => 
            {
                Ad.mWertunglist.Add(wertungen);
            });
            var edit = prefs.Edit();
              edit.PutBoolean("first", true);
              edit.Commit();

          }

        var test = realm.All<HNWertung>().First();
        Fach1.Progress = test.HauptfachWertung;
        Fach2.Progress = test.NebenfachWertung;

        //Fach1.Progress = L;
        //Fach2.Progress = ListWertung.NebenfachWertung;

        Fach1.ProgressChanged += Fach1_ProgressChanged;
        Fach2.ProgressChanged += Fach2_ProgressChanged;


        return v;
    }

    private void Fach2_ProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
    {
        Fach1.Progress = 100 - e.Progress;

    }

    private void Fach1_ProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
    {
        Fach2.Progress = 100 - e.Progress;
    }

    public override void OnDestroy()
    {

        base.OnDestroy();
    }

在片段被破坏后,如何更新/保存进度 以及如何达到更新的进度

//修改

public class StufeAdapter : RecyclerView.Adapter
{
    public event EventHandler<int> ItemClick;
    private RecyclerView mRecyclerView;
    private List<StufeAd> mStufenListe;
    Realm realm = Realm.GetInstance();

    public StufeAdapter(List<StufeAd> stufenListe, RecyclerView recyclerView)
    {
        mStufenListe = stufenListe;
        mRecyclerView = recyclerView;
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        var viewholder = holder as StufeViewHolder;

        viewholder.StufenPhoto.SetImageResource(mStufenListe[position].mPhoto);
        viewholder.Stufe.Text = mStufenListe[position].mName;
        viewholder.Durchschnitt.Text = mStufenListe[position].mDurchschnitt.ToString();
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.CardView_Stufe_Row, parent, false);


        StufeViewHolder vh = new StufeViewHolder(view, OnClick);

        return vh;
    }

    public override int ItemCount
    {
        get
        {
            return mStufenListe.Count;
        }
    }

// Realm Object

    public class StufeAd : RealmObject
    {
        [PrimaryKey]
        public string mName { get; set; }
        public int mPhoto { get; set; }
        public int mDurchschnitt { get; set; }
        public IList<HNWertung> mWertunglist { get; }
    }
    public class HNWertung : RealmObject
    {
        public int HauptfachWertung { get; set; }
        public int NebenfachWertung { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

假设你有RecyclerView,看起来像是:

enter image description here

我们可以创建一个匹配的RealmObject来保存您的数据。

RealmObject

public class ARealmObject : RealmObject
{
    [PrimaryKey] // Use Gap-less Primary keys for super fast RecyclerView, otherwise you have to create a "positional" RealmObject mapping
    public int Key { get; set; } // zero-based for RecyclerView Adapter
    public string Caption { get; set; }
    public int SeekBar1 { get; set; }
    public int SeekBar2 { get; set; }
}

我们可以创建一个RecyclerView.Adapter来从您的​​Realm实例中提取数据,并将用户更改的更改保存到每个SeekBar

RecyclerView.Adapter

public class RealmAdapter : RecyclerView.Adapter
{
    Realm realm;
    IQueryable<ARealmObject> realmobjects;

    public RealmAdapter(RealmConfiguration config)
    {
        realm = Realm.GetInstance(config);
        realmobjects = realm.All<ARealmObject>();
    }

    public class RealmObjectViewHolder : RecyclerView.ViewHolder
    {
        readonly Timer timer;
        readonly Realm realm;
        ARealmObject aRealmObject;
        string lastCaption;

        public RealmObjectViewHolder(View view, Realm realm) : base(view)
        {
            this.realm = realm;
            Caption = view.FindViewById<TextView>(Resource.Id.realmTextView);
            SeekBar1 = view.FindViewById<SeekBar>(Resource.Id.seekBar1);
            SeekBar2 = view.FindViewById<SeekBar>(Resource.Id.seekBar2);
            SeekBar1.StopTrackingTouch += SeekBar_HandleEventHandler;
            SeekBar2.StopTrackingTouch += SeekBar_HandleEventHandler;
        }

        public TextView Caption { get; private set; }
        public SeekBar SeekBar1 { get; private set; }
        public SeekBar SeekBar2 { get; private set; }

        public int Key
        {
            get { return aRealmObject.Key; }
            set
            {
                aRealmObject = realm.Find<ARealmObject>(value);
                aRealmObject = aRealmObject ?? new ARealmObject { Key = value, Caption = $"missing key : {value}", SeekBar1 = 50, SeekBar2 = 50 };
                Caption.Text = aRealmObject.Caption;
                lastCaption = Caption.Text;
                SeekBar1.Progress = aRealmObject.SeekBar1;
                SeekBar2.Progress = aRealmObject.SeekBar2;
            }
        }

        void SeekBar_HandleEventHandler(object sender, SeekBar.StopTrackingTouchEventArgs e)
        {
            UpdateRealmObject();
        }

        void UpdateRealmObject()
        {
            if (aRealmObject.Caption != Caption.Text || aRealmObject.SeekBar1 != SeekBar1.Progress || aRealmObject.SeekBar2 != SeekBar2.Progress)
            {
                lastCaption = Caption.Text;
                realm.Write(() =>
                {
                    aRealmObject.SeekBar1 = SeekBar1.Progress;
                    aRealmObject.SeekBar2 = SeekBar2.Progress;
                    realm.Add(aRealmObject, true); // Using the optional update: parameter set to true
                });
            }
        }
    }

    public override int ItemCount
    {
        get { return realmobjects.Count(); }
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        var vh = holder as RealmObjectViewHolder;
        vh.Key = position;
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        var itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ARealmObjectCardView, parent, false);
        var vh = new RealmObjectViewHolder(itemView, realm);
        return vh;
    }
}

示例用法w / 10,000个测试项目:

[Activity(Label = "RecyclerViewWithRealm", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    Realm masterRealmInstance;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        masterRealmInstance = Realm.GetInstance("RecyclerViewExample.realm");
        if (masterRealmInstance.All<ARealmObject>().Count() == 0)
        {
            masterRealmInstance.Write(() => // Create some test data
            {
                for (int i = 0; i < 10000; i++)
                {
                    masterRealmInstance.Add(new ARealmObject { Key = i, Caption = $"StackOverFlow # + {i.ToString()}", SeekBar1 = 50, SeekBar2 = 50 });
                }
            });
        }
        SetContentView(Resource.Layout.ARecyclerLayOut);

        recyclerView = (RecyclerView)FindViewById(Resource.Id.recyclerView);
        recyclerView.HasFixedSize = true;
        layoutManager = new LinearLayoutManager(this);
        recyclerView.SetLayoutManager(layoutManager);
        adapter = new RealmAdapter((RealmConfiguration)masterRealmInstance.Config);
        recyclerView.SetAdapter(adapter);
    }
}

更改任何单个SeekBar退出的值并重新启动应用程序,您的更改将重新加载:

enter image description here