ListView不会更新xamarin

时间:2018-04-01 12:11:11

标签: android listview xamarin

所以我有这段代码:

这是整个SlidingTabFragment.cs

public class SlidingTabsFragment : Fragment
{
    private SlidingTabScrollView mSlidingTabScrollView;
    private ViewPager mViewPager;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.Inflate(Resource.Layout.fragment_sample, container, false);
    }

    public override void OnViewCreated(View view, Bundle savedInstanceState)
    {
        mSlidingTabScrollView = view.FindViewById<SlidingTabScrollView>(Resource.Id.sliding_tabs);
        mViewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
        mViewPager.Adapter = new SamplePagerAdapter();
        mSlidingTabScrollView.ViewPager = mViewPager;
    }

    public class SamplePagerAdapter : PagerAdapter
    {
        List<string> items = new List<string>();
        List<string> mItems;
        private ArrayAdapter<string> adapter;

        public SamplePagerAdapter() : base()
        {
            items.Add("Élelmiszerek");
            items.Add("Receptek");
            items.Add("Recept hozzáadása");
            items.Add("Bevásárlólista");
            items.Add("Előzmények");

            return ;
        }

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

        public override bool IsViewFromObject(View view, Java.Lang.Object obj)
        {
            return view == obj;
        }

        public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {

            View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.pager_item, container, false);
            container.AddView(view);

            int pos = position + 1;
            //I'm using a scrollable menu, and if the user is on the first page than this runs. Moving the `if` somehow fixed my problem.
            if (pos == 1)
            {
            var autoCompleteOptions = new string[] { "Sajt", "Tej", "Kecske", "Barátnő", "piros", "alma" };
            ArrayAdapter autoCompleteAdapter = new ArrayAdapter(container.Context, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);
            AutoCompleteTextView mautoCompleteTextView = view.FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1);
            mautoCompleteTextView.Adapter = autoCompleteAdapter;

            ListView mListView = view.FindViewById<ListView>(Resource.Id.myListView);
            LoadData();
            mListView.ItemLongClick += MListView_ItemLongClick;
            adapter = new ArrayAdapter<string>(container.Context, Android.Resource.Layout.SimpleListItem1, mItems);
            mListView.Adapter = adapter;

            adapter.NotifyDataSetChanged();

                   mautoCompleteTextView.Visibility = ViewStates.Visible;
                   mListView.Visibility = ViewStates.Visible;
            }

            return view;
        }

        private void LoadData()
        {

            mItems = new List<string>();
            for (int i = 1; i < 8; i++)
            {
                mItems.Add("Élelmiszer"+ i);
            }
        }

        private void MListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
        {
            Console.WriteLine(mItems[e.Position]);
            mItems.RemoveAt(e.Position);
            adapter.Remove(adapter.GetItem(e.Position));
            adapter.NotifyDataSetChanged();
        }

        public string GetHeaderTitle(int position)
        {
            return items[position];
        }

        public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object obj)
        {
            container.RemoveView((View)obj);
        }

    }
}

这是fragment_sample.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layoutTextColor"
    android:orientation="vertical" >
  <App6.SlidingTabScrollView
      android:id="@+id/sliding_tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
  <android.support.v4.view.ViewPager
      android:id="@+id/viewpager"
      android:layout_width="match_parent"
      android:layout_height="0px"
      android:layout_weight="1"
      android:background="@color/grey" />
</LinearLayout>

//我试图在较小的项目中重现问题。

固定

我的问题是ListView没有更新。我知道Listview正在改变,因为如果我长按其中的一个项目,我会在visual studio的输出中看到该项目的名称(因为MListView_ItemLongClick中的Console.Writeline)。如果我再次点击相同的项目,它会写下他的下一个项目,因为我点击的第一个被删除了,但ListView没有刷新,所以它看起来仍然存在。

1 个答案:

答案 0 :(得分:0)

  

我的问题是ListView没有更新。我知道Listview正在改变,因为如果我在其中单击一个项目,我会在visual studio的输出中看到该项目的名称(因为MListView_ItemLongClick中的Console.Writeline)。

您需要删除适配器的项目,而不是直接在原始列表中删除项目,请修改下面的MListView_ItemLongClick代码:

private void MListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
{
    Console.WriteLine(mItems[e.Position]);
    mItems.RemoveAt(e.Position);
    //remove the items directly from adapter
    adapter.Remove(adapter.GetItem(e.Position));
    adapter.NotifyDataSetChanged();
}