我需要创建自定义ListView
。我创建它并且它有效,但我需要使用MVVMCross
原则。我把假数据放在错误的地方。我应该把它放进ViewModel
,但我有点困惑如何去做。
好吧,我有两个.axml
布局。列表:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/toolbar" />
<ListView
android:id="@+id/listView"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="#FFFFFF"
android:layout_marginBottom="1dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:elevation="2dp" />
</RelativeLayout>
对于列表项:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView
android:id="@+id/placeIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:padding="3dp"
android:scaleType="fitXY"
android:src="@mipmap/one" />
<TextView
android:id="@+id/placeName"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="1dp"
android:textColor="#1565C0"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text" />
<TextView
android:id="@+id/placeAddress"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginLeft="12dp"
android:textColor="#DE000000"
android:textSize="16sp"
android:text="Another Text" />
</RelativeLayout>
所以我创建了模型:
using System;
namespace My.project.Core.Models
{
public class SearchResults
{
public SearchResults(int Id, int mPlaceIcon, String mPlaceName, String mPlaceAddress)
{
this.Id = Id;
this.mPlaceIcon = mPlaceIcon;
this.mPlaceName = mPlaceName;
this.mPlaceAddress = mPlaceAddress;
}
public int mPlaceIcon { get; set; }
public string mPlaceName { get; set; }
public string mPlaceAddress { get; set; }
public string mTripCost { get; set; }
}
}
和ViewModel:
using System;
using System.Collections.Generic;
using System.Text;
namespace My.project.Core.ViewModels
{
class SearchResultsViewModel : BaseViewModel
{
}
}
之后我创建了适配器:
using Android.App;
using System.Collections.Generic;
using Android.Views;
using Android.Widget;
using My.project.Core.Models;
namespace My.project.Droid.Views
{
public class ViewHolder : Java.Lang.Object
{
public TextView txtPlaceName { get; set; }
public TextView txtPlaceAddress { get; set; }
}
public class SearchResultsAdapter : BaseAdapter
{
private Activity activity;
private List<SearchResults> searchresults;
public SearchResultsAdapter(Activity activity, List<SearchResults> searchresults)
{
this.activity = activity;
this.searchresults = searchresults;
}
public override int Count
{
get
{
return searchresults.Count;
}
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return searchresults[position].Id;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.SearchResultsView, parent, false);
var txtPlaceName = view.FindViewById<TextView>(Resource.Id.placeName);
var txtPlaceAddress = view.FindViewById<TextView>(Resource.Id.placeAddress);
txtPlaceName.Text = searchresults[position].mPlaceName;
txtPlaceAddress.Text = searchresults[position].mPlaceAddress;
return view;
}
}
}
这是我的观点。而且我做错了。
using Android.App;
using System.Collections.Generic;
using Android.OS;
using Android.Widget;
using My.project.Core.Models;
namespace My.project.Droid.Views
{
[Activity]
class SearchResultsView : BaseView
{
protected override int LayoutResource => Resource.Layout.SearchResultsView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var lstData = FindViewById<ListView>(Resource.Id.listView);
List<SearchResults> lstSource = new List<SearchResults>();
for (int i = 0; i < 10; i++)
{
SearchResults searchreasults = new SearchResults()
{
Id = i,
mPlaceName = "Place " + i,
mPlaceAddress = "Address " + i,
};
lstSource.Add(searchreasults);
}
var adapter = new SearchResultsAdapter(this, lstSource);
lstData.Adapter = adapter;
}
}
}
我需要将此循环置于ViewModel
内,但它不起作用。 Adapter
的链接无效,我无法使用它。
我该如何解决?谢谢你的帮助!