C
C
CeBePHblY2016-06-09 12:23:26
Android
CeBePHblY, 2016-06-09 12:23:26

Why isn't ListView from BroadcastReceiver updated?

I have this code:

public class AddNewRequestActivity extends Activity {
    ListView lvMain;
    ArrayAdapter<String> adapter;
    String[] names = {"Денис", "Андрей"}; //допустим, какие-то начальные данные
    protected void onCreate(Bundle savedInstanceState) {
        lvMain = (ListView) findViewById(R.id.listViewRequests);
        adapter = new ArrayAdapter<String>(AddNewRequestActivity.this, android.R.layout.simple_list_item_1, names);
        lvMain.setAdapter(adapter);
        br = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
               String[] newNames = { "Иван", "Марья", "Петр", "Антон", "Даша", "Борис", "Костя", "Игорь", "Анна", "Денис", "Андрей" };
               adapter.clear();
               adapter.addAll(newNames);
               adapter.notifyDataSetChanged();
            }
        };
        IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION);
        registerReceiver(br, intFilt);
    }
}

but after the onReceive is triggered in the Listview, only "Denis", "Andrey" are displayed, i.e. ListView is not updating. what's wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question