更改ArrayList(已复制)中项的值也会更改main ArrayList中item的值

时间:2018-05-18 06:47:41

标签: java android

我有一个ArrayList,我在其中添加了一些项目。

    private List<Product> mMainList = new ArrayList<>();
    private List<Product> mCopyList;

    mMainList.add(new Product(true,"First"));
    mMainList.add(new Product(true,"Second"));
    mMainList.add(new Product(true,"Third"));

现在我有了一个新列表mCopyList,我正在使用mMainList

进行初始化
mCopyList = new ArrayList<>(mMainList);

现在我正在更改mCopyList

中第0个索引的产品名称
mCopyList.get(0).setName("First After Change");

问题

我面临的问题是,mMainList会自动更改。我不想改变mMainList。请建议。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试像这样克隆主要的arraylist

mCopyList= (ArrayList<>)mMainList.clone();

现在克隆了arraylist

mCopyList.get(0).setName("First After Change");

现在更改是克隆列表

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;


public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.ConnectionCallbacks,     GoogleApiClient.OnConnectionFailedListener,
    com.google.android.gms.location.LocationListener {

protected static final String TAG = "Location Services";
protected static final String TAGfail = "FAILED TO CONNECT";
protected static final String TAGNOCONNECTION = "NO CONNECTION";
protected GoogleApiClient mGoogleApiClient;
protected TextView mLatitudeText;
protected TextView mLongitudeText;
protected LocationRequest mLocationRequest;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLatitudeText = (TextView) findViewById(R.id.latitude);
    mLongitudeText = (TextView) findViewById(R.id.longitude);
    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            1);
    buildGoogleApiClient();

}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
            (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onLocationChanged(Location location) {
    Log.i(TAG, location.toString());

    mLatitudeText.setText(String.valueOf(location.getLatitude()));
    mLongitudeText.setText(String.valueOf(location.getLongitude()));

}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAGNOCONNECTION, "no connection");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Toast.makeText(this, "No location permission granted", Toast.LENGTH_SHORT).show();
    Log.i(TAGfail, "failed to connect");
}

}

这不会影响您的主列表。 希望它能帮到你!!