gpt4 book ai didi

java - Listview 长按要删除的项目时出错

转载 作者:行者123 更新时间:2023-12-02 11:35:47 27 4
gpt4 key购买 nike

Visual Reference

EDITED WITH COMMENT FIX

我的 listView 应该在长按时删除您点击的任何内容。 sharePreferences 中有数据,所以这不应该是问题。所以,我正在做的是从 noteSet 获取数据,noteSet 从 myPref 获取数据。然后,在单击 listView 的位置,notesSet 会删除该注释。然后,它将修改后的notesSet重新上传到sharedPreferences,然后将notesSet添加到notes中,供listview使用。

我认为这是我收到的错误代码:

02-23 12:20:12.384 5211-5229/com.example.jackson.collegeplanner E/OpenGLRenderer:GL错误:GL_INVALID_OPERATION

02-23 12:20:28.461 550-710/system_process W/InputDispatcher: channel '17b2c24b com.example.jackson.collegeplanner/com.example.jackson.collegeplanner.Schedule (server)' ~ 消费者关闭输入 channel 或发生错误。事件=0x9

02-23 12:22:12.723 54-54/? E/EGL_emulation:tid 54:eglCreateSyncKHR(1299):错误0x3004(EGL_BAD_ATTRIBUTE)

    myPref.edit().putStringSet("NN", notesSet).apply();

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

new AlertDialog.Builder(getApplicationContext()).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Pop Up!")
.setMessage("Ready to delete this task?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

SharedPreferences myPref = getApplicationContext().getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);
Set<String> notesSet = new HashSet<String>(myPref.getStringSet("NN", null));
ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, notes);
ListView listView = (ListView) findViewById(R.id.listView);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
notesSet.remove(i);
notes.clear();

notes.addAll(notesSet);

myPref.edit().putStringSet("NN", notesSet).apply();

listView.setAdapter(arrayAdapter);
Log.i("TEST", "notesSet didn't return null!");

}
})
.setNegativeButton("No", null).show();



return false;
}
});

为了方便起见,这是我的程序中的一段代码。程序的其余部分可以正常工作,并且仅当我引入此新代码时才会发生应用程序崩溃。感谢您抽出时间。

最佳答案

使用 Acivity 的上下文而不是 getApplicationContext() 。不要在任何地方使用 getApplicationContext() ,除非它是要使用的。请按如下所示进行操作。

  new AlertDialog.Builder(YourActtivity.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Pop Up!")
.setMessage("Ready to delete this task?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
SharedPreferences myPref = getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);
Set<String> notesSet = new HashSet<String>(myPref.getStringSet("NN", null));
ListView listView = (ListView) findViewById(R.id.listView);
notesSet.remove(i);
notes.clear();
notes.addAll(notesSet);
myPref.edit().putStringSet("NN", notesSet).apply();
ArrayAdapter arrayAdapter = new ArrayAdapter(YourActtivity.this, android.R.layout.simple_list_item_1, notes);
listView.setAdapter(arrayAdapter);
Log.i("TEST", "notesSet didn't return null!");
}
})
.show();

还调试代码检查和返回值。

关于java - Listview 长按要删除的项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48958825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com