2012-01-11 2 views
4

다른 활동의 ArrayAdapter를 어떻게 사용할 수 있습니까? 나는() MyListActivity.onCreate에서 다음을 수행 시도 :다른 클래스/액티비티의 어댑터 사용

myAdapter 정의 및 SomeOtherActivity에 초기화
setListAdapter(SomeOtherActivity.myAdapter); 

. 그러나, 나는 내가 SomeOtherActivity.myAdapter가 완전히에 대한 호출을 통해 채워져 있는지 확인에도 불구하고, 빈 목록을 얻을 :

SomeOtherActivity.myAdapter.getCount(); 

을 내가 정의하고 setListAdapter (myLocalAdapter)와 MyListActivity에 내 자신의 어댑터를 초기화 할 경우 작동합니다. setListAdapter (SomeOtherActivity.myAdapter)로 전환하면 빈 목록이 표시됩니다. 내가 그것을 디버깅하고 어댑터의 getView()도 호출되지 않는 것으로 나타났습니다.

도와주세요. 감사.

내 SubActivity.onCreate에서 MainActivity.onCreate()

listIsDone = false; 
myList = new ArrayList<ItemInfo>(); 
init = new Runnable() { 
    public void run() { 
    myList = generate(); // generate the list, takes a while 
    Collections.sort(myList, new CustomComparator()); // sorts the list 
    myAdapter = new MyInfoAdapter(MainActivity.this, R.layout.row, myList); 
    synchronized(this) { 
     listIsDone = true; 
     notifyAll(); 
    } 
    } 
}; 
Thread thread = new Thread(null, init, "Background"); 
thread.start; 

()

setListAdapter(MainActivity.myAdapter); 
doStuff = new Runnable() { 
    public void run() { 
    synchronized(MainActivity.init) { 
     if (!MainActivity.getListDone()) { 
     try { 
      MainActivity.init.wait(); // wait for list/adapter to be initialized 
     } catch (InterruptedException e) { 
     } 
     } 
    } 
    } 
}; 
Thread thread = new Thread(null, doStuff, "Background"); 
thread.start(); 

내가의 Runnable 스레드에서() myAdapter.notifyDataSetChanged 실행할 ​​수 없습니다 알에서

(I 얻을 런타임 오류), runOnUiThread()로 실행하면 runnable에서 수행 할 수 있습니다. 어댑터에 대한 모든 메서드 호출을 동일한 UI 스레드에서 수행해야합니다.

답변

0

currentActivity에서 myAdapter의 액세스 가능성을 확인하십시오. 어댑터를 초기화하기 전에 사용하고있는 것으로 보입니다.

@share the code .. 적절한 해결책

+0

감사합니다. 원래 게시물에 코드를 추가했습니다. – user1118764

+0

안녕하세요, 문제는 줄에 있습니다. myList = generate(); generate()에서 새 목록을 만든 다음 채워서 반환했습니다. 매개 변수로 myList를 generate()로 전달하도록 변경했으며 작동하는 것 같습니다. Runnable 스레드에서 어댑터에 바인드 된 목록을 수정해도 문제가 있습니까? – user1118764

+0

"어댑터의 내용이 변경되었지만 ListView가 알림을받지 못했습니다. 어댑터의 내용이 백그라운드 스레드에서 수정되지 않고 UI 스레드에서만 수정되었는지 확인하십시오." 때로는 오류. 내가 배경 스레드에서 목록을 생성하기로되어 있지 않다고 생각하니? 그렇다면 기본 UI 스레드를 차단하지 않고 어떻게해야합니까? – user1118764