2017-04-15 68 views
-1

我正在使用GSON將JSON存儲到SharedPreferences中,我有一個方法可以從ListView中刪除滑動的行,我怎樣才能從該滑動的行中刪除sharedPreferences?如何從選定的ListView中刪除SharedPreferences

ListViewNews.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) {  
       switch (event.getAction()) { 
        ...  
        case MotionEvent.ACTION_UP: 
         if (event.getX() - historicX < -DELTA) { 
          ...  
          alertDialogBuilder.setPositiveButton("Yes",new DialogInterface. OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            newsAdapter.FunctionDeleteRowWhenSlidingLeft(position);  

            **ATTEMPT OF DELETION** 
            //Delete from Database 
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); 
            SharedPreferences.Editor editor = sharedPrefs.edit(); 
            Gson gson = new Gson(); 
            String json = sharedPrefs.getString(TAG, null); 
            News news = gson.fromJson(json, News.class); 

            News selItem = (News) newsAdapter.getItem(position); 

            //read 
            News news2 = gson.fromJson(json, News.class); 
            System.out.println("---"+news2.getStory_id()+"---"); 

            /* 
            This deletes everything 
            editor.remove("HackerNews"); 
            */ 

            editor.commit(); 
            editor.apply(); 

            //read 
            sharedPrefs.getString("json", ""); 
            System.out.println("SP"+sharedPrefs.getAll()); 
           } 
          ... 
+0

你的ListView是由哪些數據組成的?這些數據是否存儲在單個JSON中?是否有可能覆蓋SharedPreferences鍵值,還是隻需要修改它?我想我可以幫忙,但我需要知道這個問題的答案:) –

+0

@ S.Czop我的列表視圖是由存儲在SharedPreferences中的GSON字符串組成的,它們是如何存儲的: 'news = new News (story_id,story_title,author,created_at,story_url); //從JSON添加 SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = sharedPrefs.edit(); Gson gson = new Gson(); 字符串json = gson.toJson(news); editor.putString(TAG,json); editor.commit(); //讀取 新聞news2 = gson.fromJson(json,News.class); //從SharedPreferences中添加 newsAdapter.add(news2);' – Schavcovsky

回答

0

我的建議/解決方案:

  • 創建一個ArrayList<News> newsList持有每一個消息,將在您的ListView所示進行。
  • 存儲newsListSharedPreferences,也用它來創建您的適配器爲ListView
  • 要刪除newsList中的元素,請使用新名稱(例如newsListNew)獲取存儲在SharedPreferences中的列表。
  • 找到newsListNew中的項目與iterator並將其刪除。
  • 清除newsList
  • 將所有newsListNewnewsList
  • 更改通知適配器,所以它更新ListView
  • 覆蓋SharedPreferences

這是我目前用於當前項目的內容。 ListView應該使用一組數據,ArrayList應該是您的選擇,而不是爲您的每個News創建SharedPreferences存儲。通過這種方式,您不必在SharedPreferences上進行迭代,而是使用ArrayList,這對每個人都更爲熟悉。如果我不得不做出這樣一個草案,它應該是這樣的:

public class MainActivity extends AppCompatActivity { 

ArrayList<News> newsList, newsListNew; 
MyCustomAdapter adapter; //Can be an ArrayAdapter or Custom Adapter 

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

//Initialize the main ArrayList 
Gson gson = new Gson(); 
     SharedPreferences prefs = getSharedPreferences(
       "com.example.app", Context.MODE_PRIVATE); 
     String json = prefs.getString("newsList", null); 
     if (json == null) { 
      newsList = new ArrayList<>(); 
     } else { 
      Type type = new TypeToken<ArrayList<News>>(){}.getType(); 
      newsList = gson.fromJson(json, type); 
     } 

//Attach the adapter 
adapter = new MyCustomAdapter(this, newsList) 
listView = (ListView)findViewById(R.id.listView); 
listView.setAdapter(adapter); 

} 


//Method for removing a News 
    public void deleteNews(String id) { 

Gson gson = new Gson(); 

SharedPreferences prefs = getSharedPreferences(
     "com.example.app", Context.MODE_PRIVATE); 
String json = prefs.getString("newsList", null); 
if (json == null) { 
    newsListNew = new ArrayList<>(); 
} else { 
    Type type = new TypeToken<ArrayList<News>>(){}.getType(); 
    newsListNew = gson.fromJson(json, type); 
} 

for(Iterator<News> iterator = newsListNew.iterator(); iterator.hasNext();) { 
News news = iterator.next(); 
if (news.id == id) { //Look for the News element 
iterator.remove(); //Remove the News from the ArrayList 
break; //Include break if the Id is unique, remove it if the Id might repeat 
} 
} 

newsList.clear(); //Empty the main ArrayList 
newsList.addAll(newsListNew); //Replace the ArrayList with new info 
adapter.notifyDataSetChanged(); //Update the ListView UI 
//Save the newsList again into SharedPreferences by turning it into a JSON, as you normally do 

} 

這可能會造成混亂做.clear(),然後.addAll()的想法,但奇怪的是,如果你沒有包括它們,notifyDataSetChanged()不會總是工作。我希望這可以工作,隨時問我任何額外的東西,我可以編輯答案

相關問題