[英]populate ListView from Another class Xamarin c#
I'm trying to populate a ListView from Another class which run on another thread but I failed to do that. 我试图从另一个在另一个线程上运行的类填充ListView,但是我没有做到这一点。 Here is the main Activity : 这是主要活动:
public ListView myList;
List < string>contacts = new List< string>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Control);
myList = FindViewById<ListView>(Resource.Id.contactsList);
myList.ItemLongClick += MyList_ItemLongClick;
}
public void insertContact(string ContactInfo)
{
myList=FindViewById<ListView>(Resource.Id.contactsList);
contacts.Add(ContactInfo);
ArrayAdapter<string> myArray = new ArrayAdapter<string>(Application.Context, Android.Resource.Layout.SimpleListItem1, contacts);
list.Adapter = null;
list.Adapter = myArray;
myList.DeferNotifyDataSetChanged();
}
public void startAsync(){
Contacts con = new Contacts();
Thread thread = new Thread(()=> con.Start());
thread.Start();
}
I'm firing a method from another class to grap contacts. 我正在从另一个类中激发一种方法来抓取联系人。 There it just get some info and should send it to main Activity The other class doing like 在那里,它只是获取一些信息,并应将其发送给main Activity。
MainAct main = new MainAct();
main.insertContact("new Contact");
but It did nothing. 但是它什么也没做。 I tried to make a method to get the listView from the main activity (like mentioned in some thread) and pass it as second paramter to add contact like : 我试图做一个从主要活动中获取listView的方法(就像在某个线程中提到的那样),并将其作为第二个参数传递,以添加联系人,例如:
public ListView getListView()
{
ListView myList = FindViewById<ListView>(Resource.Id.contactsList);
return myList;
}
and after than calling this line which has the ListView : 然后调用此具有ListView的行:
main.insertContact("new Contact", main.getListView());
but I got an exception : 但我有一个例外:
Java.Lang.NullPointerException: < Timeout exceeded getting exception details>
I tried from the other class to run the line like : 我从另一堂课尝试运行像这样的行:
main.RunOnUIThread(()=> main.insertContact("new Contact", main.getListView()));
but got the same exception ! 但是有同样的例外! What did I missed ? 我错过了什么?
NOTE: If I put both classes in one class ( I mean all the code in one class ) It works without a problem with RunOnUIThread(()=> ); 注意:如果我将两个类都放在一个类中(我的意思是所有代码都在一个类中),那么RunOnUIThread(()=>)不会出现问题; . 。 but When I move the method to another class, I got the exception. 但是当我将方法移到另一个类时,出现了异常。
EDIT 1: When I catch the exception I got nullpointer 编辑1:当我捕获异常时,我得到了nullpointer
Exception of type 'Java.Lang.NullPointerException' was thrown.
1) If Contacts
is your another class, I suggest you rename it as MyContacts
, because there is a Contacts class,it will confused us. 1)如果Contacts
是您的另一个类,建议您将其重命名为MyContacts
,因为有一个Contacts类,它将使我们感到困惑。
2) You don't need to new MainAct
, just pass the context to your another class. 2)您不需要新的MainAct
,只需将上下文传递给另一个类。
I have tried to reproduce your question, and I add a comment for these codes: 我试图重现您的问题,并为这些代码添加了注释:
Here is your MyContacts
class: 这是您的MyContacts
类:
public class MyContacts
{
MainActivity mContext;
public MyContacts(MainActivity context) {
this.mContext = context;
}
public void Start() {
mContext.insertContact("new Contact");
mContext.insertContact("new Contact1");
mContext.insertContact("new Contact2");
}
}
You MainActivity
: 您MainActivity
:
public class MainActivity : Activity
{
public ListView myList;
List<string> contacts = new List<string>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
myList = FindViewById<ListView>(Resource.Id.contactsList);
//we usually init the data in OnCreate method.
startAsync();
}
public void insertContact(string ContactInfo)
{
//this is redundant, because you have Instantiated the myList in the OnCreate method.
//myList = FindViewById<ListView>(Resource.Id.contactsList);
contacts.Add(ContactInfo);
//You can use this instead of Application.Context.
ArrayAdapter<string> myArray = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, contacts);
myList.Adapter = myArray;
//from your question, I can't find any information about the list property.
//list.Adapter = null;
//list.Adapter = myArray;
//myList.DeferNotifyDataSetChanged();
}
//From your question, I can't find where have you call this method, I will call it in the OnCreate method
public void startAsync()
{
MyContacts con = new MyContacts(this);
Thread thread = new Thread(() => con.Start());
thread.Start();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.