【发布时间】:2018-08-31 16:42:42
【问题描述】:
我有一个带有一些按钮和一个列表视图的布局。其中一个按钮将记录添加到 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 条记录,try catch 区域可以防止崩溃 - 这可能很关键!
非常感谢任何对重复行问题的修复。
【问题讨论】:
-
我会尝试
RecyclerView,仅此一项就可以解决您的问题。视图 = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null); ^ 为什么将ViewGroup root设置为空?传入parent参数 -
列表视图似乎是旧技术。作为一个努力跟上的人,我将改用 RecyclerView。谢谢。
-
recyclerview 有什么不同吗?
-
是的 - 我从 nuget 添加了下载,然后它说“检测到 xamarin.android.support.compat 的 xamarin recyclerview 版本冲突。直接从项目中引用包来解决这个问题。所以我更深入地研究了这一点,更新了各种项目,直到没有任何效果。我刚刚重新安装完 VS 和 Xamarin。
-
你的建议不错,等我的电脑觉得可以再试试。谢谢。
标签: android listview xamarin duplicates