我有任务。我必须在一个活动的ListView
上显示来自特定移动电话号码的消息。这些消息包含两个位置的纬度和经度。当我单击任何List项时,它应该在这两个位置绘制标记,并且必须在另一个活动中绘制它们之间的路径。
我的留言格式是!+ 11.3326,+ 077.7193,08:41!+ 11.3326,+ 077.7194,08:41#
我的MainActivity.java是
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView);
List<String> msgList=getSMS();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,msgList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String str=(String)parent.getItemAtPosition(position);
String lat1=str.substring(2,8);
String lng1=str.substring(11,18);
String lat2=str.substring(27,33);
String lng2=str.substring(36,43);
Toast.makeText(getApplicationContext(),lat1+""+lng1+""+lat2+""+lng2,Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,MapsActivity.class);
startActivity(intent);
}
});
}
public List<String> getSMS(){
List<String>sms=new ArrayList<String>();
final String SMS_URI_INBOX="content://sms/inbox";
try {
Uri uri=Uri.parse(SMS_URI_INBOX);
String[] projection=new String[]{"_id","address","person","body","date","type"};
Cursor cursor=getContentResolver().query(uri,projection,"address='+918870346164'",null,null);
if (cursor.moveToFirst()){
int index_Address = cursor.getColumnIndex("address");
int index_Person = cursor.getColumnIndex("person");
int index_Body = cursor.getColumnIndex("body");
int index_Date = cursor.getColumnIndex("date");
int index_Type = cursor.getColumnIndex("type");
do {
String strAddress = cursor.getString(index_Address);
int intPerson = cursor.getInt(index_Person);
String strBody = cursor.getString(index_Body);
String longDate = cursor.getString(index_Date);
int intType = cursor.getInt(index_Type);
sms.add("\n"+strBody+"\n");
}while (cursor.moveToNext());
if (!cursor.isClosed()){
cursor.close();
cursor=null;
}
}
}catch (SQLiteException e){
Log.d("SQLiteException",e.getMessage());
}
return sms;
}
我的MapsActivity.java是 公共类MapsActivity扩展了FragmentActivity {
GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
addMarker();
}
private void addMarker() {
final LatLng latLng=new LatLng(lat1,lng1);
mMap.addMarker(new MarkerOptions().position(latLng).title("My Marker"));
}
我已显示消息,并且可以从ListView
项获取输入。我不知道如何将这些lat1
,lng1
,lat2
,lng2
值传递给MapsActivity
并添加标记。请解决任何问题。
答案 0 :(得分:0)
启动地图活动时,会在意图中传递latlng值。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String str=(String)parent.getItemAtPosition(position);
Intent intent=new Intent(MainActivity.this,MapsActivity.class);
intent.putExtra("locationValues",str);
startActivity(intent);
}
});
现在,在您创建的地图活动中,请重新获取这些值。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
String locationString = this.getIntent().getStringExtra("locationValues","");
if(!TextUtils.isEmpty(locationString))
{
String lat1=str.substring(2,8);
String lng1=str.substring(11,18);
String lat2=str.substring(27,33);
String lng2=str.substring(36,43);
addMarker(lat1,lng1,lat2,lng2);
}
}
private void addMarker(String lat1,String lng1,String lat2,String lng2) {
//you can use those value here then.
final LatLng latLng=new LatLng(lat1,lng1);
mMap.addMarker(new MarkerOptions().position(latLng).title("My Marker"));
}