I have this code:

public class AddNewRequestActivity extends Activity { ListView lvMain; ArrayAdapter<String> adapter; final ArrayList names = new ArrayList(); names.add("Ivan"); names.add("Anton"); 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) { names.clear(); names.add("Ivan"); names.add("Anton"); names.add("Sasha"); names.add("Viktor"); adapter.notifyDataSetChanged(); } }; IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION); registerReceiver(br, intFilt); } } 

but after onReceive is triggered, only "Ivan", "Anton" are displayed in the Listview, i.e. ListView is not updated. what's wrong?

    1 answer 1

    Try not to create a new array, but change the data type, transferred to the adapter on the ArrayList and in the receiver, without touching the adapter, clear the list in the activation and add new elements to it. Then notify the adapter about it.

    You should not use arrays in ListView at all - use ArrayList

    • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin