//Все лишнее из кода выбросил, оставляю суть public class MainActivityLink extends AppCompatActivity { ListView listView; MyCommandProcessing myCommandProcessing; MyAdapterLink adapter; ArrayList<MyArrayLink> items = new ArrayList<MyArrayLink>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myCommandProcessing = new MyCommandProcessing(); items = myCommandProcessing.getAllLinkOnIDGroup(idGroup, items); adapter = new MyAdapterLink(this, items); listView = (ListView) findViewById(R.id.listViewLink); listView.setAdapter(adapter); //Вывод данных проходит как надо EventBus.getDefault().register(this); } public void onEvent(MessageEvent event){ myCommandProcessing.getAllLinkOnIDGroup(idGroup, items); adapter.notifyDataSetChanged(); //проблема с обновлением ListView } } public class MyCommandProcessing { ArrayList<MyArrayLink> getAllLinkOnIDGroup(long idGroup, ArrayList<MyArrayLink> items){ items.clear(); List<DBLink> allDBLink = new DBLink().getLinkOnIDGroup(idGroup); for(int i=0; i<allDBLink.size();i++) { items.add(items.size(), new MyArrayLink(allDBLink.get(i).getId(), allDBLink.get(i).name, allDBLink.get(i).id_group, allDBLink.get(i).link, allDBLink.get(i).countNew, allDBLink.get(i).countAll, allDBLink.get(i).year, allDBLink.get(i).month, allDBLink.get(i).day, allDBLink.get(i).hh, allDBLink.get(i).mm, allDBLink.get(i).ss)); } return items; } } 

the problem is that after the execution of the onEvent(MessageEvent event) method, the data in the ListView not updated, the update only happens if (!) if you touch the ListView element (!). those. Intuitively, I understand that onEvent(MessageEvent event) is invoked by EventBus tools and this happens in another thread that is not connected to the interface. How to solve this problem?

I would be grateful for any answers.

  • And if you wrap the update adapter in runOnUiThread ()? - Yuriy SPb
  • one
    Thank you very much, YurySPb! You're right. - ivansoft

1 answer 1

If the method is not called in the main thread and something must be done from it basically, then there is a special feature in the activation. Method for this:

 runOnUiThread() 

inside it, a Runnable object is created and in its run method the code will be executed in the main thread, i.e. in where you can interact with the UI