public void AddCustomEvent() {
ListView lv = (ListView) findViewById(R.id.listView1);
final Set<String> tasks = sp.getStringSet("tasks", new HashSet<String>());
ArrayList<String> taskarr = new ArrayList<String>();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Hello!", String.valueOf(position) + " " + String.valueOf(id));
final int pos = position;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
// set title
alertDialogBuilder.setTitle("Do you want to delete this task");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to delete !")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
Set<String> tasks = sp.getStringSet("tasks", new HashSet<String>());
final ListView listview = (ListView) findViewById(R.id.listView1);
final ArrayList<String> list = new ArrayList<String>();
for (String str : tasks) {
list.add(str);
}
list.remove(pos);
Set<String> newTasks = new HashSet<String>();
for (String str : list) {
newTasks.add(str);
}
Editor edit = sp.edit();
edit.putStringSet("tasks", newTasks);
edit.commit();
final StableArrayAdapter adapter = new StableArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
我制作了一个任务应用程序。它在删除任务之前显示警报对话框。没什么特别的。我的代码因为崩溃(我想在其中显示警报对话框)而被破坏。
有人检查代码,让我知道我如何以及在哪里泄漏服务。