我正在尝试为AutoCompleteTextView创建自定义适配器以动态显示搜索结果。我使用Xamarin Android在C#中编写此代码。当我创建自定义适配器时,我试图给视图充气,但我的编译器没有将我的视图识别为资源文件的一部分。这是我的适配器:
using System;
using Android;
using Android.Content;
using Android.Views;
using Android.Widget;
using Java.Lang;
public class CustomTrafficSearchAdapter : BaseAdapter
{
Context context;
string[] data;
static LayoutInflater inflater = null;
public override int Count
{
get;
}
public CustomTrafficSearchAdapter(Context context, string[] data)
{
this.context = context;
this.data = data;
inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
}
public override Java.Lang.Object GetItem(int position)
{
return data[position];
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
view = View.Inflate(Resource.Layout.autocomplete_textview, null);
}
convertView = inflater.Inflate(context, Resource.Layout.autocomplete_textview, null);
TextView placeNameView = convertView.FindViewById<TextView>(Resource.Id.placeName);
TextView placeAddressView = convertView.FindViewById<TextView>(Resource.Id.placeAddress);
//TextView text = vi.FindViewById<TextView>(Resource.Id.text);
//text.setText(data[position]);
return convertView;
}
}
这是我的自动填充布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/CustomAuto"
android:singleLine="true"
android:orientation="vertical">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/placeName" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/placeAddress" />
</LinearLayout>
这是我的样式文件:
<style name="CustomAuto">
<item name="android:paddingTop">3dp</item>
<item name="android:paddingRight">3dp</item>
<item name="android:paddingBottom">3dp</item>
<item name="android:paddingLeft">3dp</item>
<item name="android:textColor">#333</item>
<item name="android:textSize">17sp</item>
</style>