我使用GSON将JSON存储到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 :(得分:0)
我的建议/解决方案:
ArrayList<News> newsList
,其中包含将在ListView
中显示的每条新闻。newsList
存储在SharedPreferences
中,并使用它为ListView
创建适配器。newsList
中的元素,请使用新名称(例如SharedPreferences
)获取存储在newsListNew
中的列表。newsListNew
在iterator
中找到该项目并将其删除。newsList
newsListNew
添加到newsList
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()并不总是有效。我希望这有效,随意问我任何额外的东西,我可以对答案进行编辑