在重复按钮单击时向android中的表添加行

时间:2017-10-01 18:42:34

标签: android android-tablelayout android-gps

目前,我可以在Android中使用GPS获取当前位置。每次单击按钮获取当前位置时,我都需要将点(纬度和经度)作为单独的行添加到表中。

这是xml文件

<RelativeLayout
            android:id="@+id/RelativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical">

            <TableLayout
                android:id="@+id/table_main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true">

            </TableLayout>
        </RelativeLayout>

这是java

 public void showCurrentLocation() {

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(MainActivity.this, "First enable LOCATION ACCESS in settings.", Toast.LENGTH_LONG).show();
        return;
    }

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        String message = String.format(

                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        clat = location.getLatitude();
        clon = location.getLongitude();

        String point = String.valueOf(clat) + "," + String.valueOf(clon);
        Double Longitude = (clon);
        Double Latitude = (clat);

        Cod1.setText(String.valueOf((double) Math.round(clat * 1000000d) / 1000000d));
        Cod11.setText(String.valueOf((double) Math.round(clon * 1000000d) / 1000000d));

        cordsList.add(point);

        latitude.add(0.549035);
        Log.e("Latitudes", latitude.toString());

        longitude.add(33.561665);
        Log.e("Longitudes", longitude.toString());


        Log.e("Array", cordsList.toString());
        Toast.makeText(MainActivity.this, message,
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(MainActivity.this, "null location",
                Toast.LENGTH_LONG).show();
    }

    TableLayout stk = (TableLayout) findViewById(R.id.table_main);

    TableRow tbrow0 = new TableRow(this);
    TextView tv0 = new TextView(this);
    tv0.setText(" LATITUDE");
    tv0.setTextColor(Color.WHITE);
    tbrow0.addView(tv0);


    TextView tv1 = new TextView(this);
    tv1.setText(" LONGITUDE ");
    tv1.setTextColor(Color.WHITE);
    tbrow0.addView(tv1);


    stk.addView(tbrow0);


        TableRow tbrow = new TableRow(this);

        TextView t1v = new TextView(this);
        t1v.setText(String.valueOf((double) Math.round(clat * 1000000d) / 1000000d));
        t1v.setTextColor(Color.WHITE);
        t1v.setGravity(Gravity.CENTER);
        tbrow.addView(t1v);


        TextView t2v = new TextView(this);
        t2v.setText(String.valueOf((double) Math.round(clon * 1000000d) / 1000000d));
        t2v.setTextColor(Color.WHITE);
        t2v.setGravity(Gravity.CENTER);
        tbrow.addView(t2v);



        stk.addView(tbrow);



}

上面的代码可以添加行,但不断重复相同的字段。我想一个接一个地插入行

0 个答案:

没有答案