Android. How to add items to ListView from BroadcastReceiver?

doing so
public class AddNewRequestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// create a BroadcastReceiver
 br = new BroadcastReceiver() {
 // actions when receiving messages
 public void onReceive(Context Context, Intent intent) {
String[] names = { "John", "Mary", "Peter", "Anton", "Dora", "Boris",
 "Kostya", "Igor", "Anna", "Denis", "John" };

// find the list
 ListView lvMain = (ListView) findViewById(R. id.listViewRequests);
 // create the adapter
 ArrayAdapter<string> adapter = new ArrayAdapter<string>(AddNewRequestActivity.this, android.R.layout.simple_list_item_1, names);
 // assign adapter to list
lvMain.setAdapter(adapter);

}
};
// create a filter for BroadcastReceiver
 IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION);
 // pending (include) BroadcastReceiver
 registerReceiver(br, intFilt);
}
}</string></string>

not added to the ListView items(ListView itself is placed in one of the tabs TabHost). how to fix?
July 8th 19 at 15:55
1 answer
July 8th 19 at 15:57
It...
Create the adapter each time when receiving a message... Why? O_o
Constantly pull the ListView and set the adapter... The same, why?
And by the way, in the normal applications list, after the changes update.
I have omitted some code. With servers come commands, each command corresponds to an if block, in one of them and need to update the ListView. In activiti there is a TabHost, by clicking one of the tabs, the user requests data from the server, the server prisylat data BroadcastReceiver catches them, and from BroadcastReceiver you need to update the ListView after inserting data into it. - Marlin_Ritchie commented on July 8th 19 at 16:00
if you paste the code
String[] names = { "John", "Mary", "Peter", "Anton", "Dora", "Boris",
"Kostya", "Igor", "Anna", "Denis", "John" };

// find the list
ListView lvMain = (ListView) findViewById(R. id.listViewRequests);
// create the adapter
ArrayAdapter adapter = new ArrayAdapter(AddNewRequestActivity.this, android.R.layout.simple_list_item_1, names);
// assign adapter to list
lvMain.setAdapter(adapter);
in OnCreate of activity, then it works. and if you insert in your BroadcastReceiver? the list is empty. - Marlin_Ritchie commented on July 8th 19 at 16:03
: what is BROADCAST_ACTION? This code actually runs, though, stream is coming?

Throw TabHost. Throw ListView. B 2016 live, after all. - Art_Christians commented on July 8th 19 at 16:06
: Yes, the code inside onReceive is executed. I'm just starting to learn Android so maybe something not so doing. under Android 4 found only a TabHost and a ListView. is there something else? - Marlin_Ritchie commented on July 8th 19 at 16:09

Find more questions by tags Android