2012-07-06 2 views
1

목록보기 항목에이 문제가있어서 문제를 해결할 수 있기를 바랍니다. 내 목표는 listview를 목록으로 채우는 것이며 사용자가이 항목 중 하나를 터치하면 다른보기가로드되기를 원합니다. 항목을 추가하면 제대로 작동하지만 선택한 항목에서 값을 가져 와서 올바른 개체로 타입 변환하면 " '잘못된 캐스트'가 캐스팅 될 수 없습니다 ... '라는 메시지가 나타나고 충돌이 발생합니다.Monodroid ListView 선택한 항목의 유형 변환 오류가 발생했습니다.

는 참고로, 나는 안드로이드 4.0 SIM을 사용하고,이 코드의 일부입니다 :

SetContentView(Resource.Layout.ArchiveList); 
ListView lstArchiveList = FindViewById<ListView>(Resource.Id.lstArchive); 

if (lstArchiveList != null) { 
    ArrayAdapter<MobileContracts.Archive> archivesAdapter = new 
     ArrayAdapter<MobileContracts.Archive>(this, Resource.Layout.ListItem, sessionData.Archives.Archive); 
    lstArchiveList.Adapter = archivesAdapter; 
    lstArchiveList.TextFilterEnabled = true; 
    lstArchiveList.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(lstArchiveList_ItemClick); 
    archivesAdapter.NotifyDataSetChanged(); 
} 

OnClick 이벤트 핸들러 : 어떤 도움을 주시면 감사

void lstArchiveList_ItemClick(object sender, AdapterView.ItemClickEventArgs e) { 
    SetContentView(Resource.Layout.SearchDocuments); 
    ListView lstEditableIndexList = FindViewById<ListView>(Resource.Id.lstEditableIndexList); 
    if (lstEditableIndexList != null) { 

     Console.WriteLine("sender type: {0}", sender.GetType().FullName); 

     Object currentItem = e.Parent.GetItemAtPosition(e.Position); 
     MobileContracts.Archive selectedArchive = (MobileContracts.Archive) currentItem; //invalid cast? 

     Toast.MakeText(Application, selectedArchive.Name + " => " + selectedArchive.Id, ToastLength.Short).Show(); 
} 

. 많은 감사드립니다. 건배, Inoel

답변

1

신경 쓰지

, 나는이 알아낼.

이 교체 :이와

MobileContracts.Archive selectedArchive = (MobileContracts.Archive) currentItem; //invalid cast? 

:

System.Reflection.PropertyInfo propertyInfo = currentItem.GetType().GetProperty("Instance"); 
MobileContracts.Archive selectedArchive = propertyInfo.GetValue(currentItem, null) as MobileContracts.Archive; 
관련 문제