2017-01-16 106 views
1

在我的代码中,借助上下文菜单,我可以从Listview中删除特定项目,但由于我使用sharedpreferences来保存名为“places”的数组列表,因此它将恢复应用程序重新启动时的共享首选项。现在,我应该如何实现我的共享首选项,以便在从listview中删除特定项目时,同样的项目也会从共享首选项的数组列表中“删除”。从列表视图中移除项目

下面是我的代码片段

static ArrayList<String> places = new ArrayList<String>(); 
    static ArrayList<LatLng> locations = new ArrayList<>(); //to save lat and long 
    static ArrayAdapter arrayAdapter; 
    public ListView listView; 
    SharedPreferences sharedPreferences; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     listView = (ListView) findViewById(R.id.listView); 

     sharedPreferences = this.getSharedPreferences("com.starprojects.memorableplaces", Context.MODE_PRIVATE); 
     registerForContextMenu(listView); 

     //tricker locations 
     ArrayList<String> latitudes = new ArrayList<>(); 
     ArrayList<String> longitudes = new ArrayList<>(); 

     //initially set 
     places.clear(); 
     latitudes.clear(); 
     longitudes.clear(); 
     locations.clear(); 


     //to restore 
     try { 

      places = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("places", ObjectSerializer.serialize(new ArrayList<>()))); 

      latitudes = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("latitudes", ObjectSerializer.serialize(new ArrayList<>()))); 

      longitudes = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("longitudes", ObjectSerializer.serialize(new ArrayList<>()))); 

      Log.i("palces",places.toString()); 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } 

@Override 
    public boolean onContextItemSelected(MenuItem item) { 

      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
     // sharedPreferences = getSharedPreferences("places",0); 
      SharedPreferences.Editor editor = sharedPreferences.edit(); 

      if((item.getTitle()).equals("Delete")) 
      { 
       places.remove(info.position); 

       editor.remove("places"); //problem is here, how to get particular index to be removed from arraylist places and save it. 
       editor.commit(); 

       arrayAdapter.notifyDataSetChanged(); 

       return true; 
      } 
      return super.onContextItemSelected(item); 


     } 
} 

回答

0

您可以通过

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
int index = info.position; 
+0

nope它期望字符串 –

0

获得指标就意味着你必须在你的列表共享的喜好或你有你的共享偏好不同的密钥。

案例1:如果您的共享首选项中有一个列表,而不是通过从listview中删除数据的数据更新共享首选项。

情况2:如果您为每个列表项分配了不同的key_name,那么只需从共享首选项中删除数据时删除或清除该key_name即可。

+0

可以请你在我的代码中实现并解释 –

+0

不,请你先尝试我们可以指导这一点。 –

0

如果我得到了您的问题的重点,您正试图保持共享首选项与您显示的共享首选项一致,反之亦然。 要做到这一点,我认为你只是需要把更新的地方数组列表到共享的喜好,就像这样:

@Override 
public boolean onContextItemSelected(MenuItem item) { 

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 

    if ((item.getTitle()).equals("Delete")) { 

     // Update local list 
     places.remove(info.position); 

     // Set list into shared preferences 
     // Or if you use a JSON string you could serialize and use putString() 
     editor.putStringSet("places", places); 

     // Use apply it's async 
     editor.apply(); 

     arrayAdapter.notifyDataSetChanged(); 
     return true; 
    } 

    return super.onContextItemSelected(item); 
} 

请使用到位提交的申请()()。 It's faster and asynchronous

+0

我在这里要做的是有一个名为Places的数组列表,所以当我通过删除places.remove(info.position)从中删除一个元素时,它也应该从sharedpreferences中删除。 –

+0

如果地方有{Add new location,xyz,abc};所以如果我删除abc然后sharedpreference也应该删除它。但是,我的代码发生了什么事情,当我删除它时被删除为本地更改,但当它在杀死它后重新启动应用程序时,它返回,因为它被保存在sharedpreference –

+0

好吧,那就是我得到的,我认为我的例子将解决您的问题。顺便说一下,您应该考虑转移到像数据库这样的永久性存储器来存储位置。这将是更容易管理 – LucioB