我正在 xamarin android
中创建自定义列表这是我的代码:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace FinalList
{
[Activity (Label = "FinalList", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity :ListActivity
{
string[] items;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
items = new string[] { "aBulbs","bFliuits","cFruits","dpriraji","eqsbciob","frTubers","guhVegetables","hvwert","iwxamarin","jyaaaa","kVegetables","lFruits","mVegetables","nwert","pxamarin","ryaaaa"};
//ListAdapter = new ArrayAdapter<String> (this, Android.Resource.Layout.SimpleListItem1, items);
ListView lv =FindViewById<ListView> (Android.Resource.Id.List);
ListAdapter = new HomeScreenAdapter(this, items);
ListView.FastScrollEnabled = true;
//listview.ChoiceMode = Android.Widget.ChoiceMode.Single;
// listview.SetItemChecked (1, true);
lv.SetItemChecked (3, true);
}
protected override void OnListItemClick(ListView l, View v, int position, long id)
{
var t = items[position];
Android.Widget.Toast.MakeText(this, t, Android.Widget.ToastLength.Short).Show();
}
}
}
这是我的axml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/List"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fastScrollEnabled="true"
/>
</LinearLayout>
这是我的HomeScreenAdapter
文件:
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 FinalList
{
[Activity (Label = "HomeScreenAdapter")]
public class HomeScreenAdapter : BaseAdapter<string>,ISectionIndexer {
String[] items;
Activity context;
Dictionary<string, int> alphaIndex;
String[] sections;
Java.Lang.Object[] sectionsObjects;
public HomeScreenAdapter(Activity context, string[] items) : base() {
this.context = context;
this.items = items;
alphaIndex = new Dictionary<string, int>();
for (int i = 0; i < items.Length; i++) { // loop through items
String s = items [i];
String key = s.Substring (0, 1);
key= key.ToUpper ();
if (!alphaIndex.ContainsKey(key))
alphaIndex.Add(key, i); // add each 'new' letter to the index
}
sections = new string[alphaIndex.Keys.Count];
alphaIndex.Keys.CopyTo(sections, 0); // convert letters list to string[]
// Interface requires a Java.Lang.Object[], so we create one here
sectionsObjects = new Java.Lang.Object[sections.Length];
for (int i = 0; i < sections.Length; i++) {
sectionsObjects[i] = new Java.Lang.String(sections[i]);
}
}
public override long GetItemId(int position)
{
return position;
}
public override string this[int position] {
get { return items[position]; }
}
public override int Count {
get { return items.Length; }
}
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(Android.Resource.Layout.SimpleListItemChecked, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position];
return view;
}
public Java.Lang.Object[] GetSections()
{
return sectionsObjects;
}
public int GetPositionForSection(int section)
{
return alphaIndex[sections[section]];
}
public int GetSectionForPosition(int position)
{ // this method isn't called in this example, but code is provided for completeness
int prevSection = 0;
for (int i = 0; i < sections.Length; i++)
{
if (GetPositionForSection(i) > position)
{
break;
}
prevSection = i;
}
return prevSection;
}
}
}
当我构建解决方案时,我得到 null对象引用异常 它说:
object reference is not set to an instance of an object at line
lv.setitemchecked(1,true);
如何删除此例外?
答案 0 :(得分:1)
您必须在base.OnCreate (bundle);
:
SetContentView(Resource.Layout."YourLayoutName");
答案 1 :(得分:0)
你应该使用
ListView lv =FindViewById<ListView> (Android.Resource.Id.List);
ListAdapter = new HomeScreenAdapter(this, items);
lv.FastScrollEnabled = true;
//lv.ChoiceMode = Android.Widget.ChoiceMode.Single;
// lv.SetItemChecked (1, true);
lv.SetItemChecked (3, true);
而不是
ListView lv =FindViewById<ListView> (Android.Resource.Id.List);
ListAdapter = new HomeScreenAdapter(this, items);
ListView.FastScrollEnabled = true;
//listview.ChoiceMode = Android.Widget.ChoiceMode.Single;
// listview.SetItemChecked (1, true);
lv.SetItemChecked (3, true);
记得setContentView(resource.layout.your_layout);太