1
私は、その場所に基づいて人々の鳴き声を無音にするアプリケーションを構築しています。私はPlacePicker APIを使用して、その場所の名前をSharedPreferencesに保存し、それをListViewに追加します。私は項目を削除することに問題があります。ユーザーがリストビュー項目を長くクリックすると、SharedPreferencesから削除されるはずです。問題は、最初の削除の後、どのSharedPrefsアイテムを削除するかを参照する私の方法が悪く、うまくいきません。どうすれば修正できますか?SharedPreferencesからこれらのアイテムの削除を処理するにはどうすればよいですか?
public class MainActivity extends AppCompatActivity implements LocationListener {
PlacePicker.IntentBuilder builder;
int PLACE_PICKER_REQUEST;
ListView placeListView;
ArrayList<String> placeArrayList;
ArrayAdapter<String> arrayAdapter;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
ArrayList<Double> latitudeList;
Integer counter;
LocationManager locationManager;
String provider;
double lat;
double lng;
Place place;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
placeListView = (ListView) findViewById(R.id.placeListView);
latitudeList = new ArrayList<>();
placeArrayList = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, placeArrayList);
placeListView.setAdapter(arrayAdapter);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
placeListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure?")
.setMessage("Do you want to delete this place?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
placeArrayList.remove(position);
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
sharedPreferences.edit().remove("name" + position).apply();
arrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
return false;
}
});
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null){
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(getApplicationContext(), String.valueOf(lat) + String.valueOf(lng), Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Unable to get location", Toast.LENGTH_LONG).show();
}
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
sharedPreferences.edit().putInt("counter", 0);
counter = sharedPreferences.getInt("counter", -1);
arrayAdapter.notifyDataSetChanged();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PLACE_PICKER_REQUEST = 1;
builder = new PlacePicker.IntentBuilder();
pickPlace();
}
});
}
public void pickPlace(){
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
place = PlacePicker.getPlace(data, this);
// String toastMsg = String.format("Place: %s", place.getName());
//Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
counter = sharedPreferences.getInt("counter", -1);
counter = counter + 1;
sharedPreferences.edit().putInt("counter", counter).apply();
LatLng latLng = place.getLatLng();
sharedPreferences.edit().putFloat("latitude" + String.valueOf(counter), (float) latLng.latitude).apply();
sharedPreferences.edit().putFloat("longitude" + String.valueOf(counter), (float) latLng.longitude).apply();
placeArrayList.add(place.getName().toString());
sharedPreferences.edit().putString("name" + placeArrayList.size(), place.getName().toString()).apply();
arrayAdapter.notifyDataSetChanged();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onLocationChanged(Location location) {
for (int i = counter; i >= 0; i--){
if (lat >= sharedPreferences.getFloat("latitude" + String.valueOf(i), -1) - .005 &&
lat <= sharedPreferences.getFloat("latitude" + String.valueOf(i), -1) + .005 &&
lng >= sharedPreferences.getFloat("longitude" + String.valueOf(i), -1) - .005 &&
lng <= sharedPreferences.getFloat("longitude" + String.valueOf(i), -1) + .005){
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, AudioManager.RINGER_MODE_SILENT, AudioManager.FLAG_SHOW_UI);
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
私はそれが私が必要とするSharedPreferences項目を参照するためにどのように使用するのですか?私の問題は、アイテムが削除されると、リストビューシフトの項目がさらに下がってしまい、SharedPreferencesでそれらを参照するために "position"を使用できなくなるということです。たとえば、3番目のアイテムが削除されると、4番目のアイテムが削除されます。ユーザーがそのタグを削除しようとすると、タグが4であるためにできません。位置が3でない場合は、 – felix
、今は理解しています。ありがとう! – felix
私はうれしいです:) –