我正在开发一个应用程序,我正在与谷歌地图2,我以ArrayList的形式将五个地址位置保存到共享首选项中。我使用ListView显示5个地址。现在,每当我长按某个项目时,我都要从ListView中删除该项目。为此,我在ListView中使用“setOnTimeLongClickListener()”。
在这里,我需要从sharedpreference中获取数组并删除我要删除的特定项,我尝试了如下操作。但效果不好。请提供任何帮助
protected void removeItemFromList(int position) {
final int deletePosition = position;
if(addressList.size()>0){
addressList.remove(deletePosition);
}
if(addAList.size()>0){
addAList.remove(deletePosition);
}
adapter.remove(deletePosition);
adapter1.remove(deletePosition);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
adapter1.notifyDataSetChanged();
adapter1.notifyDataSetInvalidated();
}
});
我的密码
GoogleMap _googlemap;
private LatLng hydLocation;
Button clearButton;
private SharedPreferences sharedPreferences;
private Editor editor;
private double lat;
private double lng;
public static ArrayList<String> addressList = new ArrayList<String>();;
public static ArrayList<String> distanceList = new ArrayList<String>();;
public static ArrayList<LatLng> latLngList = new ArrayList<LatLng>();;
public ListView addressListView;
public static ArrayList<Marker> markerList = new ArrayList<Marker>();;
public static ArrayList<Float> latArray = new ArrayList<Float>(); ;
public static ArrayList<Float> lngArray = new ArrayList<Float>();;
private String[] addressArry;
private static int count=0;
private Builder alertDialogBuilder;
private EditText input;
protected String diskm;
private String distance;
private Marker marker1;
private Marker marker2;
private Marker marker3;
private Marker marker4;
private Marker marker5;
private Float lng1;
private Float lat1;
private ArrayAdapter adapter;
private ArrayAdapter adapter1;
private LatLng latLng;
public static ArrayList<String> addAList=new ArrayList<String>();
public static ArrayList<Float> lngList= new ArrayList<Float>();
public static ArrayList<Float> latList= new ArrayList<Float>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_or_delete_favourate_loc);
addressListView =(ListView)findViewById(R.id.listView);
clearButton = (Button)findViewById(R.id.clearBtn);
_googlemap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.favarouteLocMap)).getMap();
_googlemap.getUiSettings().setZoomControlsEnabled(true);
_googlemap.getUiSettings().setCompassEnabled(true);
_googlemap.getUiSettings().setMyLocationButtonEnabled(true);
hydLocation = new LatLng(17.3752800, 78.4744400);
_googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(hydLocation, 10));
_googlemap.setOnMapClickListener(this);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();
editor.clear();
/*addAList=new ArrayList<String>();
latList = new ArrayList<Float>();
lngList = new ArrayList<Float>();*/
int size1=sharedPreferences.getInt("size1",0);
int size2=sharedPreferences.getInt("size2",0);
int size3=sharedPreferences.getInt("size3",0);
for(int j=0;j<size1;j++)
{
addAList.add(sharedPreferences.getString("addr"+j,""));
}
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,addAList);
addressListView.setAdapter(adapter);
for(int j=0;j<size2;j++){
latList.add(sharedPreferences.getFloat("latArr"+j,0.0f));
}
Log.e("LAT", ""+latList.size());
for(int j=0;j<size3;j++){
lngList.add(sharedPreferences.getFloat("lngArr"+j,0.0f));
}
for(int i=0;i<latList.size();i++){
lat1=latList.get(i);
lng1=lngList.get(i);
Log.e("LAT LNG", ""+lat1+lng1);
latLng = new LatLng((double)lat1,(double)lng1);
getMarks(_googlemap, latLng);
}
addressListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ""+pos, 100).show();
removeItemFromList(pos);
return false;
}
});
}
@Override
public void onMapClick(final LatLng point) {
// TODO Auto-generated method stub
lat = point.latitude;
lng = point.longitude;
count=sharedPreferences.getInt("count",0);
count++;
editor.clear();
Geocoder geoCoder = new Geocoder(AddOrDeleteFavourateLocActivity.this, Locale.getDefault());
try{
List<Address> addresses = geoCoder.getFromLocation(point.latitude, point.longitude, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
final String strAdd = strReturnedAddress.toString();
alertDialogBuilder = new AlertDialog.Builder(
AddOrDeleteFavourateLocActivity.this);
alertDialogBuilder.setTitle("Enter Distance From Your Location");
input = new EditText(AddOrDeleteFavourateLocActivity.this);
input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
alertDialogBuilder.setView(input);
alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
distance=input.getText().toString();
diskm ="\n"+distance+" km";
if(count==1){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker1);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
distanceList.add((String)distance);
Log.e("DIST", distance);
}
if(count==2){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker2);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
distanceList.add((String)distance);
}
if(count==3){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker3);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
distanceList.add((String)distance);
}
if(count==4){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker4);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
distanceList.add((String)distance);
}
if(count==5){
addressList.add(strAdd+diskm);
latLngList.add(point);
markerList.add(marker5);
latArray.add((float)lat);
lngArray.add((float)lng);
editor.putInt("count", count);
distanceList.add((String)distance);
}
adapter1=new ArrayAdapter(AddOrDeleteFavourateLocActivity.this,android.R.layout.simple_list_item_1,addressList);
addressListView.setAdapter(adapter1);
for(int i=0;i<distanceList.size();i++){
editor.putString("dist"+i, distanceList.get(i));
}
editor.putInt("distSize", distanceList.size());
for(int i=0;i<addressList.size();i++)
{
editor.putString("addr"+i,addressList.get(i));
}
for(int i=0;i<latArray.size();i++)
{
editor.putFloat("latArr"+i,latArray.get(i));
}
for(int i=0;i<lngArray.size();i++)
{
editor.putFloat("lngArr"+i,lngArray.get(i));
}
editor.putInt("size1",addressList.size());
editor.putInt("size2",latArray.size());
editor.putInt("size3",lngArray.size());
editor.commit();
}
});
alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
--count;
marker1.remove();
}
});
if(count==1){
marker1= getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==2){
marker1=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==3){
marker1=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==4){
marker1=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count==5){
marker1=getMarks(_googlemap, point);
alertDialogBuilder.show();
}
if(count>5){
Toast.makeText(getApplicationContext(),"Sorry You Can Add Only Five Locations", 100).show();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
private Marker getMarks(GoogleMap googleMap,LatLng arg0){
Marker marker;
MarkerOptions markerOption = new MarkerOptions();
markerOption.position(arg0).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)).flat(true);
markerOption.draggable(true);
marker=googleMap.addMarker(markerOption);
return marker;
}
public void clearData(View v){
_googlemap.clear();
//marker1.remove();
sharedPreferences.edit().remove("size2").commit();
sharedPreferences.edit().remove("size3").commit();
editor.clear();
editor.commit();
adapter.clear();
if(!addAList.isEmpty()){
addAList.clear();}
if(!latList.isEmpty()){
latList.clear();
}
if(!lngList.isEmpty()){
lngList.clear();
}
}
protected void removeItemFromList(int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(
AddOrDeleteFavourateLocActivity.this);
alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub
// main code on after clicking yes
if(addressList.size()>0){
addressList.remove(deletePosition);
}
if(addAList.size()>0){
addAList.remove(deletePosition);
}
adapter.remove(deletePosition);
adapter1.remove(deletePosition);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
adapter1.notifyDataSetChanged();
adapter1.notifyDataSetInvalidated();
/*Intent intent = new Intent(AddOrDeleteFavourateLocActivity.this,AddOrDeleteFavourateLocActivity.class);
startActivity(intent);*/
}
});
alert.setNegativeButton("CANCEL", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alert.show();
}
最佳答案
我建议将列表适配器中的项保存在arraylist中。同样在适配器创建方法中,如下所示:
public void removeItem(int pPosition) {
items.remove(pPosition);
notifyDatasetChanged();
}
在活动/片段中包含对适配器的引用。
实现ListView的方法setOnEmClickListener和setOnEmLongClickListener。
关于java - 长按 ListView 项时,删除 ListView 项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23108887/