如何选择整个列表视图并提取整个内容?

时间:2017-01-21 02:53:30

标签: android arrays google-maps listview sms

我的应用程序从手机的收件箱中读取邮件,并将其显示在应用程序的列表视图中。这些消息包含要在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);



    }

}
}

这是我的谷歌地图活动代码:

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>

1 个答案:

答案 0 :(得分:0)

当我想绘制多个标记时,这就是我解决的问题。首先创建一个模型类来保存lat和long坐标,然后创建一个List。这样:

    public class LatLong {

    private Double lat;
    private Double lng;
    private String name;
    private String address;

    /**
     *
     * @return
     * The lat
     */
    public Double getLat() {
        return lat;
    }

    /**
     *
     * @param lat
     * The lat
     */
    public void setLat(Double lat) {
        this.lat = lat;
    }

    /**
     *
     * @return
     * The lng
     */
    public Double getLng() {
        return lng;
    }

    /**
     *
     * @param lng
     * The lng
     */
    public void setLng(Double lng) {
        this.lng = lng;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    }

然后在MapsActivity

private List<LatLong> list_location;

然后将数据作为Parcelable或以您自己喜欢的方式传递到此列表。获得数据后,就可以绘制它了:

 @Override
public void onMapReady(GoogleMap map) {

    for(LatLong x:list_location)
    {
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(x.getLat(),x.getLng()), 15));
        map.addMarker(new MarkerOptions()
                .title(x.getName())
                .snippet(x.getAddress())
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.dine))
                .position(new LatLng(x.getLat(),x.getLng())));
    }


    MapStyleOptions style = MapStyleOptions.loadRawResourceStyle(
            this, R.raw.style_json); // ignore this if you are not using the map with a custom design
    map.setMapStyle(style);
}