0
這個錯誤並沒有出現,直到我說Xamarin System.InvalidCastException:無法轉換類型「Android.Widget.EditText」的情況下鍵入「Android.Widget.Button」
private void ExList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
ExList.RemoveViewAt(e.Position);
Openers.Remove(e.Position.ToString());
adapter.NotifyDataSetChanged();
}
完整的代碼只是從Itext textview獲取輸入文本並將其添加到打開ExList Listview的Openers。該適配器是一個簡單的。 Itext.text =「」;突出與調試器,所以我認爲它停在那條線。 繼承人的代碼。
public class MainActivity : Activity
{
public List<string> Openers = new List<string>();
Button AButton;
TextView Itext;
ListView ExList;
Button SettingsB;
ArrayAdapter adapter;
public OpenersView adap;//Custom adapter class
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
SettingsB = FindViewById<Button>(Resource.Id.SettingsB);
AButton = FindViewById<Button>(Resource.Id.AddB);
Itext = FindViewById<TextView>(Resource.Id.Itext);
ExList = FindViewById<ListView>(Resource.Id.ExList);
//adap = new OpenersView(this, Openers); //Custom adapter
adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1, Openers);//Simple adapter
ExList.Adapter = adapter;
AButton.Click += (object sender, EventArgs e) =>
{
if (Itext.Text.Length > 8) { Openers.Add(Itext.Text); }
adap.NotifyDataSetChanged();
Itext.Text = "";
//I declare the listview here so it will update every click of the button.
};
ExList.ItemClick += ExList_ItemClick;
}
private void ExList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
ExList.RemoveViewAt(e.Position);
Openers.Remove(e.Position.ToString());
adapter.NotifyDataSetChanged();
}
}
}
其EDITTEXT改變你可以發表你的'Main.axml'文件? – Ironman
是的。這個問題似乎出現在你的設計師身上。你可能有一個編輯文本,並在你的主代碼中試圖將它轉換爲一個按鈕。 – Ogbe