我创建了一个 gpsservice,一个带有两个按钮的主要活动和一个列表活动。我想在获取gps数据时获取gpsservie数据,所以我在主activity中创建了一个gps接收器来获取数据。当我单击主活动中的按钮时,它会转到列表活动。我想在单击按钮时使用 intent() 将获取的数据传输到列表活动。问题是当我单击按钮时,列表视图中没有显示任何内容。
GPS服务的关键代码:
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); }
主要活动代码:
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);
}
});
}
列表活动:
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);
}
}