I create an gpsservice, a main activity with two buttons and a list activity. I’d like to get the gpsservie data when the gps data is obtained, so I create a gps receiver in the main activity to get the data. When I click the button in the main activity, it turns to the list activity. I’d like to transfer the obtained data to the list activity by using intent() when the button is clicked. the problem is that when I click the button, there is nothing shown in the listview.
the key codes of GPS service :
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
additem(location);
addgeo(location);
//String latLongString = "Lat:" + location.getLatitude() + "\nLong:" + location.getLongitude();
Intent intent= new Intent("com.example.geotask.gpsdata");
intent.putIntegerArrayListExtra("lat", (ArrayList<Integer>) lat);
intent.putIntegerArrayListExtra("lon", (ArrayList<Integer>) lon);
intent.putStringArrayListExtra("data", (ArrayList<String>) position);
//intent.putExtra("data", latLongString);
sendBroadcast(intent); }
the main activity code:
public class MainActivity extends Activity {
public ArrayList<String> position1 = new ArrayList<String>();
public ArrayList<Long> time=new ArrayList<Long>();
public ArrayList<Integer> lat1= new ArrayList<Integer>();
public ArrayList<Integer> lon1= new ArrayList<Integer>();
private BroadcastReceiver gpsreceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter=new IntentFilter("com.example.geotask.gpsdata");
gpsreceiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
ArrayList<String> position1=intent.getStringArrayListExtra("data");
ArrayList<Integer> lat1=intent.getIntegerArrayListExtra("lat");
ArrayList<Integer> lon1=intent.getIntegerArrayListExtra("lon");
}
};
this.registerReceiver(gpsreceiver, intentFilter);
final Button listbutton=(Button) findViewById(R.id.button2);
listbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent =new Intent();
intent.setClass(MainActivity.this, list.class);
intent.putStringArrayListExtra("data", (ArrayList<String>) position1);
startActivity(intent);
}
});
}
the list activity:
public class list extends Activity{
static ArrayList<listdetails> details = new ArrayList<listdetails>();
// private BroadcastReceiver gpsreceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView listView;
listView = (ListView)findViewById(R.id.listView1);
Intent intent=getIntent();
List<String> ex=intent.getStringArrayListExtra("data");
customlist Adapter = new customlist(details, this);
listView.setAdapter(Adapter);
listdetails Detail = new listdetails();
for(int i=0;i<ex.size();i++)
{
Detail.setIcon(R.drawable.ic_launcher);
Detail.setPlace(ex.get(i));
Detail.setTime("2012.12.2");
details.add(Detail);
}
}
You are duplicating your variables in
onReceive(). This means that theArrayListvariables get destroyed onceonReceive()completes. YouronReceive()should look like:This ensures that you are now modifying the global ArrayLists. Also, you may need to declare the global ArrayLists as final since Java might treat your
new BroadCastReceiveras an anonymous inner class.