Why doesn't it work?



  • I have such a 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 once it works onReceive in the Listview, only Ivan, "Anton," that is, ListView is not being updated.



  • Try not to create a new set, but to change the type of one that goes to the ArrayList Adapter and the Reciver without touching the adapter, read the list to the activists and add new elements. Then notify the adapter of this.

    You don't need to use the stocks in the ListView.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2