私のアプリは、電話の受信トレイからメッセージを読み取り、アプリのリストビューに表示します。これらのメッセージには、Googleマップに表示される座標が含まれています。コンテンツを1つずつ抽出できます。選択したリストビューの行をクリックすると、マーカーがGoogleマップに表示されます。今私がやりたいことは、リストビューには多くの座標があるので、すべての座標を1つに抽出してGoogleマップに表示できるようにしたいので、Googleマップ上に多くのマーカーが表示され、それを並べる。私は配列を使用する必要がありますか?これについてどうすればいいですか?ここでリストビュー全体を選択してコンテンツ全体を抽出する方法は?
は私のメインのアクティビティコードです:
public class MainActivity extends AppCompatActivity implements OnClickListener {
// GUI Widget
Button getSmsButton, viewGoogleMapButton;
TextView lblMsg, lblNumber;
ListView messageListView;
Context ctx = this;
String msg;
String lat, lng, latTag = "latTag", lngTag = "lngTag";
double latd, lngd;
// Cursor Adapter
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init GUI Widget
getSmsButton = (Button) findViewById(R.id.getSmsButton);
getSmsButton.setOnClickListener(this);
messageListView = (ListView) findViewById(R.id.messageListView);
viewGoogleMapButton = (Button) findViewById(R.id.viewGoogleMapButton);
viewGoogleMapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent startGoogleMap = new Intent(ctx, MapsActivity.class);
startActivity(startGoogleMap);
}
});
messageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//for getting the position
String data=messageListView.getItemAtPosition(position).toString();
//for getting the text from selected item
TextView textView = (TextView) view.findViewById(R.id.lblMsg); //textView in your listView
String text = textView.getText().toString();
//Log.i("msg", text);
String[] coordinate = text.split(", ");
lat = coordinate[0];
lng = coordinate[1];
latd = Double.parseDouble(lat);
lngd = Double.parseDouble(lng);
Intent passVariable = new Intent(ctx, MapsActivity.class);
passVariable.putExtra(latTag,latd);
passVariable.putExtra(lngTag,lngd);
startActivity(passVariable);
}
});
}
@Override
public void onClick(View v) {
if (v == getSmsButton) {
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
messageListView.setAdapter(adapter);
}
}
}
は、ここに私のGoogleマップのアクティビティコードです:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String latTag = "latTag", lngTag = "lngTag";
double latd, lngd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Bundle extras = getIntent().getExtras();
if (extras != null) {
latd = extras.getDouble(latTag);
lngd = extras.getDouble(lngTag);
}
// Add a marker in Singapore and move the camera
LatLng singapore = new LatLng(latd, lngd);
/*LatLng singapore0 = new LatLng(1.3347954, 103.7760273);
LatLng singapore1 = new LatLng(1.3350862, 103.7765259);
LatLng singapore2 = new LatLng(1.3347714, 103.776533);
LatLng singapore3 = new LatLng(1.3345733, 103.776867);
LatLng singapore4 = new LatLng(1.334255, 103.775617);
LatLng singapore5 = new LatLng(1.333447, 103.7752314);
LatLng singapore6 = new LatLng(1.332072, 103.774257);
LatLng singapore7 = new LatLng(1.330976, 103.775757);
LatLng singapore8 = new LatLng(1.332358, 103.777676);*/
mMap.addMarker(new MarkerOptions().position(singapore).title("Marker in Singapore"));
/*mMap.addMarker(new MarkerOptions().position(singapore0).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore1).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore2).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore3).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore4).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore5).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore6).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore7).title("Marker in Singapore"));
mMap.addMarker(new MarkerOptions().position(singapore8).title("Marker in Singapore"));*/
mMap.addPolyline((new PolylineOptions())
.add(singapore));
//singapore0, singapore1, singapore2, singapore3, singapore4, singapore5, singapore6, singapore7, singapore8));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(singapore,16));
}
}
は、ここに私の行のXMLファイルのコードです:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblMsg"></TextView>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f"
android:id="@+id/lblNumber"></TextView>
</LinearLayout>
メインアクティビティの場合、リストビュー内のすべての座標が抽出されますか?どこに私のコードを置くのですか? – Niwnaj