Listview在后续行上重复数据

时间:2018-08-31 16:42:43

标签: android listview xamarin duplicates

我有一个带有一些按钮和一个列表视图的布局。按钮之一将记录添加到SQLite数据库。另一个按钮在列表视图中显示记录-可能是1、2或更多,例如3,这取决于我按第一个按钮的次数。 但是,当我按下按钮以在列表视图中显示记录并向下滚动列表视图时,相同的记录会在较低的行中弹出,通常用空白行隔开,以便第6行再次启动该列表。当我滚动到底部并再次向上滚动时,更多不需要的记录会显示在下方。每一个填充的行中都有一个“有效”记录,但记录太多。 当我按下在列表视图中重新显示记录的按钮时,该视图将恢复为正常状态,直到我再次向下滚动。 如果我减小列表视图的高度,则重复将从第5行而不是第6行开始。 附带代码。首先,在MainActivity中填充列表视图的按钮:

        //ShowRecordsInListView
    Button button5 = FindViewById<Button>(Resource.Id.button5_ID);
    button5.Click += delegate
    {

        try
        {
            //Set up the db connection:
            var db = new SQLiteConnection(dbPath);

            //Set up a table:
            db.CreateTable<FieldNamesInTable2>();

            //Get these items from the database and populate the array:
            FieldNamesInTable2[] myRecords = new FieldNamesInTable2[30];
            var table = db.Table<FieldNamesInTable2>();

            int count = 0;
            foreach (var item in table)
            {
                myRecords[count] = item;
                count++;
            }

            //Get ListView:
            var lv = FindViewById<ListView>(Resource.Id.recordsListView_ID);
            lv.Adapter = new HomeScreenAdaptor(this, myRecords);

        }
        catch (Exception e)
        {
            //System.Diagnostics.Debug.WriteLine(e);
            Toast.MakeText(this, e.Message, ToastLength.Long).Show();
        }

    };

请注意以下行: lv.Adapter = new HomeScreenAdaptor(this,myRecords);

MainActivity的代码顶部:

    namespace Database2.Droid
{
    [Activity(Label = "Database2", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {       

        //Path string for database file:
        string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "dbDatabase.db3");

        protected override void OnCreate(Bundle savedInstanceState)
        {

            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Database2Layout);

...等等。

实际上是从HomeScreenAdaptor.cs填充列表视图,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Database2.Droid
{

    class HomeScreenAdaptor : BaseAdapter<FieldNamesInTable2>
    //ListView: - this one uses HomeScreenAdapter but has a bug in it which is either it crashes if there are fewer than 6 items, or it repeats
    //randomly with spaces between repeats of the same data lower in the listview.
    {
        FieldNamesInTable2[] myRecords;
        Activity context;

        public HomeScreenAdaptor(Activity context, FieldNamesInTable2[] records) : base()
        {
            this.myRecords = records;
            this.context = context;
        }

        //You need to override 4 attributes:
        public override long GetItemId(int position)
        {
            return position;

        }

        public override FieldNamesInTable2 this[int position]
        {
            get
            {
                return myRecords[position];
            }
        }

        public override int Count
        {
            get
            {
                return myRecords.Length;
            }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {

            //Reuse a row if one becomes available.
            View view = convertView;
            try
            {
                if (view == null)
                {
                    //See Xamarin documentation 'Built-in Row Views' will show you the different types of views - simple, selectable, checkboxes, highlightable, with pictures etc.
                    view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
                }
                //Weird alert!! - Text1 exists
                view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = myRecords[position].ToString();
                //Text1 is used by the listview to populate each row.

            }
            catch (Exception)
            {
                //System.Diagnostics.Debug.WriteLine(e);
                return view;
            }
            return view;

        }
    }
}

如果没有记录或少于6条记录,则尝试捕获区域可防止崩溃-这可能至关重要!

非常感谢对重复行问题的任何修复。

1 个答案:

答案 0 :(得分:0)

尝试这个...我已经修改了您的GetView()方法

    public override View GetView(int position, View convertView, ViewGroup parent)
    {

        //Reuse a row if one becomes available.
        View view = convertView;
        ViewHolder viewholder;
            if (view == null)
            {
                //See Xamarin documentation 'Built-in Row Views' will show you the different types of views - simple, selectable, checkboxes, highlightable, with pictures etc.
                view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);

            viewholder=new ViewHolder();
            //Weird alert!! - Text1 exists

            viewholder.text1=(TextView)view.findviewbyid(Android.Resource.Id.Text1);

            //Text1 is used by the listview to populate each row.

            view.setTag(viewholder)
           }else{
                viewholder=(ViewHolder)view.getTag();
           }
           viewholder.text1.setText(myRecords[position]);
        return view;

    }
    private class ViewHolder{
         TextView text1;
    }