我想将新数据添加到我的listviewAdapter中,我尝试了清除所有数据并通知现在有数据,但似乎不起作用的尝试。我遵循了Solution (stackoverflow),但是它不起作用。
有人知道为什么吗?
public class ReportsListViewAdapter : BaseAdapter<IMobileReport>, IFilterable
{
internal List<IMobileReport> originalData;
internal List<IMobileReport> reports;
private Context context;
public override IMobileReport this[int position] => reports[position];
public ReportsListViewAdapter(Context context, IEnumerable<IMobileReport> reports)
{
this.reports = reports.OrderBy(report => report.StudyDate).ToList();
this.context = context;
Filter = new ReportsFilter(this);
}
public override int Count => this.reports.Count;
public Filter Filter { get; private set; }
public override long GetItemId(int position)
{
return position;
}
public void updateReportsList(List<MobileReport> newlist)
{
reports.AddRange(newlist);
this.NotifyDataSetChanged();
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
row = LayoutInflater.From(context).Inflate(Resource.Layout.listView_reports_row, null, false);
}
var txtName = row.FindViewById<TextView>(Resource.Id.txtName);
txtName.Text = reports[position].Student.Name;
var txtFirstName = row.FindViewById<TextView>(Resource.Id.txtFirstName);
txtFirstName.Text = reports[position].Student.FirstName;
var txtSource = row.FindViewById<TextView>(Resource.Id.txtSource);
txtSource.Text = reports[position].Source;
var txtCritical = row.FindViewById<TextView>(Resource.Id.txtCritical);
txtSource.Text = reports[position].Critical.ToString();
return row;
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
reports = new List<IMobileReport>();
//Init();
_reportsHubConnector = new ReportsHubConnector();
#pragma warning disable 4014 // We cannot await this task here because the signature of the inherited method is void
Task.Factory.StartNew(async () =>
{
await _reportsHubConnector.StartConnection();
await _reportsHubConnector.SendT();
}, TaskCreationOptions.PreferFairness);
#pragma warning restore 4014
Console.WriteLine("HomeActivity: OnCreate");
SetContentView(Resource.Layout.activity_reports);
SupportActionBar.SetDisplayShowTitleEnabled(false);
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
SupportActionBar.SetDisplayShowHomeEnabled(true);
WireUpElements();
listView = FindViewById<ListView>(Resource.Id.reports);
ReportsListViewAdapter adapter = new ReportsListViewAdapter(this, reports);
listView.Adapter = adapter;
searchView = FindViewById<SearchView>(Resource.Id.searchView1);
searchView.QueryTextChange += this.Filter;
listView.ItemClick += ItemClicked;
criticalButton = FindViewById<LinearLayout>(Resource.Id.AuthenticatorButton);
criticalButton.Click += criticalClicked;
_reportsHubConnector.ReportsRecieved += (reports) =>
{
adapter.updateReportsList(reports);
};
}
当我调试缓慢时,GetView确实被触发,也许这是为什么当我不调试或快速浏览代码时为什么不调用它的线索。
This.RunOnUiThread从不调用,但更新。
_reportsHubConnector.ReportsRecieved += (tmpReports) =>
{
adapter.updateReportsList(tmpReports);
this.RunOnUiThread(() =>
{
criticalButton.SetBackgroundColor(Android.Graphics.Color.Red);
});
};
我尝试过清除所有数据,并通知现在有数据,但似乎不起作用。
从共享代码,看不到清晰的方法,您可以添加reports.Clear()
以检查其是否有效。
public void updateReportsList(List<MobileReport> newlist)
{
reports.Clear();
reports.AddRange(newlist);
this.NotifyDataSetChanged();
}
如果不起作用,则需要检查添加的newlist
是否是正确的数据格式。
========================更新======================= =
在OnCreate
方法中,其中_reportsHubConnector.ReportsRecieved
调用更新方法如下:
_reportsHubConnector.ReportsRecieved += (tmpReports) =>
{
adapter.updateReportsList(tmpReports);
};
将reports
参数更改为tmpReports
,以避免与原始数据reports
混合。
因此,还有另一种更新适配器数据的常用方法,如下所示:
_reportsHubConnector.ReportsRecieved += (reports) =>
{
List<IMobileReport> tmp = new List<IMobileReport>();
tmp = reports ; // use a tmp list data for updating , not using original list data
adapter.updateReportsList(tmp);
};
===========================更新==================== ============
从我的示例项目中,我发现了一个有趣的现象,也许就是这个问题。
这里我将共享我的自定义适配器HomeScreenAdapter:
public class HomeScreenAdapter : BaseAdapter<TableItem> {
List<TableItem> items;
Activity context;
public HomeScreenAdapter(Activity context, List<TableItem> items)
: base()
{
this.context = context;
this.items = new List<TableItem>();
this.items.AddRange(items);
//this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.SubHeading+" items";
view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.ImageResourceId);
return view;
}
public void UpdateListView(List<TableItem> newTableItem)
{
items.Clear();
items.AddRange(newTableItem);
NotifyDataSetChanged();
}
}
您将在HomeScreenAdapter的Constructor
中看到,我注释了代码行this.items = items;
。它会起作用的reault:
但是,如果我使用this.items = items;
替换this.items.AddRange(items);
,即使在ListView
中无法显示任何内容,它也不会更新。
public HomeScreenAdapter(Activity context, List<TableItem> items) : base() { this.context = context; this.items = new List<TableItem>(); //this.items.AddRange(items); this.items = items; }
效果:
可能的原因
:如果在此处使用等号,则各项的指针地址将改变。数据更新时,它无法指向原始数据源,因此更新无法成功。
因此
,您的代码的Constructor
可以按如下修改以检查其是否有效::这是the sample project link。public ReportsListViewAdapter(Context context, IEnumerable<IMobileReport> reports) { //this.reports = reports.OrderBy(report => report.StudyDate).ToList(); this.reports = new List<IMobileReport>(); this.reports.AddRange(reports.OrderBy(report => report.StudyDate).ToList()); this.context = context; Filter = new ReportsFilter(this); }
相关