我的Listview與Adapter和Visual Studio中的項目集合xamarine android sdk(本機UI)在項目更改時不會更新。 我使用下面的代碼來獲得visual-studio-xamarine中的項目列表。它似乎工作,除了當一個項目被更改,添加或刪除到集合時不更新列表。在適配器數據更改時更新列表視圖visual-studio android sdk
我應該採用不同的方法嗎?需要做些什麼才能讓列表視圖監聽項目更改?
我的自定義適配器
public class TableItemAdapter : BaseAdapter<Table>
{
List<Table> items;
Activity context;
public TableItemAdapter(Activity context, IEnumerable<Table> items) : base() {
this.context = context;
this.items = items.ToList();
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView; // re-use an existing view, if one is available
if (view == null) // otherwise create a new one
view = context.LayoutInflater.Inflate(Resource.Layout.TableListItem, null);
var tableName = view.FindViewById<TextView>(Resource.Id.tvTableName);
tableName.Text = items[position].TABLE_NAME;
tableName.SetTextColor(items[position].tableColor);
view.FindViewById<TextView>(Resource.Id.tvTableDetail).Text = items[position].Rest;
return view;
}
public override long GetItemId(int position) { return position; }
public override Table this[int position] { get { return items[position]; } }
public override int Count { get { return items.Count; } }
}
TableListItem.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<TextView
android:id="@+id/tvTableName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="20sp" />
<TextView
android:id="@+id/tvTableDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tvTableName"
android:gravity="right"/>
</RelativeLayout>
C#
private ObservableCollection<Table> tables = new ObservableCollection<Table>();
fillTablesWithObjects(tables); // adding items to collection
listView = FindViewById<ListView>(Resource.Id.lvTables);
var lvAdapter = new TableItemAdapter(this, tables);
listView.Adapter = lvAdapter;
// when adding or altering tables, nothing happens to listview
tables.Add(new Table() { Name = "Table A1" });
NotifyDataSetChanged
,listView.listSource
等似乎並不是xamarine android sdk,這正是我所需要的。