阵列适配器显示错误的项目,无法绘制Google地图路线

时间:2019-03-11 13:46:43

标签: android google-maps

莱姆在这里拍我的代码...

public class Index extends AppCompatActivity implements GoogleMap.OnMyLocationButtonClickListener, GoogleMap.OnMyLocationClickListener, OnMapReadyCallback, ActivityCompat.OnRequestPermissionsResultCallback {
    public static String[] account;

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
    private boolean mPermissionDenied = false;
    private GoogleMap mMap;
    String[][] mobileArray = {
            new String[] {"150 Goyeau St, Windsor, ON N9A 6J5","Product 1","N/A","3:30pm"},
            new String[] {"2000 Talbot Road West, Windsor, ON N9A 6S4","Product 2","N/A","4:00pm"},
            new String[] {"350 City Hall Square W, Windsor, ON N9A 6S1","Product 3","N/A","4:30pm"}
    };

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }
    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tVDriver = (TextView)findViewById(R.id.tVDriver);
        tVDriver.setText(account[0]);
        final TextView tVSpecial = (TextView)findViewById(R.id.tVSpecial);
        final TextView tVProduct = (TextView)findViewById(R.id.tVProduct);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listview, mobileArray[0]);
        ListView listView = (ListView) findViewById(R.id.lVDelivery);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?>adapter, View v, int position, long id){
                tVSpecial.setText(mobileArray[position][2]);
                tVProduct.setText(mobileArray[position][1]);
            }
        });
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;
        mMap.setOnMyLocationButtonClickListener(this);
        mMap.setOnMyLocationClickListener(this);
        enableMyLocation();
    }
    private void enableMyLocation() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                    Manifest.permission.ACCESS_FINE_LOCATION, true);
        } else if (mMap != null) {
            mMap.setMyLocationEnabled(true);
        }
    }
    @Override
    public boolean onMyLocationButtonClick() {
        Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
        return false;
    }
    @Override
    public void onMyLocationClick(@NonNull Location location) {
        Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            return;
        }
        if (PermissionUtils.isPermissionGranted(permissions, grantResults,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            enableMyLocation();
        } else {
            mPermissionDenied = true;
        }
    }
    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        if (mPermissionDenied) {
            showMissingPermissionError();
            mPermissionDenied = false;
        }
    }
    private void showMissingPermissionError() {
        PermissionUtils.PermissionDeniedDialog
                .newInstance(true).show(getSupportFragmentManager(), "dialog");
    }
}

我试图通过点击数组中的项目来在Google地图上绘制路径(并在加载活动时自动放大我的位置。)

问题是arrayadapter列出了第一个数组项。以编程方式,这是有道理的,但不是我想要的。我想为所有第一个数组显示第二个数组中的第一项。这意味着我只希望将地址显示在arrayadapter中。

我知道问题就在这里。

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listview, mobileArray[0]);

但是我需要调用数组的哪一部分才能正确显示这些内容?

第二个问题是,加载此活动后如何放大我的位置?如何绘制路径?我尝试了两种不同的方法,其中一种出现了错误,使其无法正常工作,而另一种根本没有显示它。

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/in_the_weeds"
    android:layout_height="match_parent">

    <include
        layout="@layout/map"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="0dp" />

    <TextView
        android:id="@+id/tVNull"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="25dp"
        android:background="#80FFFFFF"
        android:text="Driver"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tVDriver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="20dp"
        android:background="#80FFFFFF"
        android:text="No Driver"
        android:textSize="24sp"/>

    <ListView
        android:id="@+id/lVDelivery"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#80FFFFFF"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="90dp" />

    <TextView
        android:id="@+id/tVProductNull"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp"
        android:layout_marginTop="90dp"
        android:layout_marginEnd="20dp"
        android:layout_toRightOf="@+id/lVDelivery"
        android:background="#80FFFFFF"
        android:text="Product"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tVProduct"
        android:layout_width="160dp"
        android:layout_height="165dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="125dp"
        android:layout_marginEnd="20dp"
        android:layout_toRightOf="@+id/lVDelivery"
        android:background="#80FFFFFF"
        android:textSize="24sp"
        android:text="1g - Bubba Hash" />

    <TextView
        android:id="@+id/tVSpecial"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="300dp"
        android:layout_toRightOf="@+id/tVProductNull"
        android:background="#80FFFFFF"
        android:textSize="24sp"
        android:text="TextView" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以创建一个仅包含地址的新数组。但是更好的方法是创建一个自定义类(将其称为findNewRoomID),该类仅包含getter和setter。然后,将Product更改为ArrayAdapter<String>。自定义ArrayAdapter非常简单:

https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

编辑:这是我提到的错误解决方案。另一个要我花很长时间才能输入(但是您应该真正学习它,因为这是Android中的超级常见任务):

ArrayAdapter<Product>

}