我目前正在使用Android的Parse API,我目前在我的应用程序中有一个字符串数组。我目前有一个包含所有字符串的ListView。在我的应用程序内部ListView本身,我实现了一个setOnItemLongClickListener
Dialog
。我想从Parse中删除所选的ListView项的字符串。
NotesFragment.java
public class NotesFragment extends Fragment {
ParseUser user;
ListView notesList;
private FloatingActionButton FAB;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes, container, false);
notesList = (ListView) rootView.findViewById(R.id.lv_contact);
user = ParseUser.getCurrentUser();
FAB = (FloatingActionButton) rootView.findViewById(R.id.fab);
FAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), AddNoteActivity.class);
getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
startActivity(intent);
}
});
final ArrayList<ParseObject> list1 = (ArrayList) user.getList("notes");
ArrayAdapter<ParseObject> arrayAdapter;
arrayAdapter = new ArrayAdapter<ParseObject>(getContext(), android.R.layout.simple_list_item_1, list1);
RelativeLayout emptyView = (RelativeLayout) rootView.findViewById(R.id.empty);
if (list1 == null) {
emptyView.setVisibility(View.VISIBLE);
} else {
notesList.setAdapter(arrayAdapter);
emptyView.setVisibility(View.INVISIBLE);
}
notesList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(getContext())
.setTitle("Are you sure you want to Delete?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ArrayList<String> toRemove = new ArrayList<>();
toRemove.add("Hey");
ParseUser.getCurrentUser().removeAll("checklistDat", toRemove);
ParseUser.getCurrentUser().saveInBackground();
ParseUser.getCurrentUser().deleteInBackground();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
return true;
}
});
return rootView;
}
}
答案 0 :(得分:0)
我能够通过这样做来解决这个问题
<强> NotesFragment.java 强>
public class NotesFragment extends Fragment {
ParseUser user;
ListView notesList;
ProgressBar mPB;
private FloatingActionButton FAB;
ArrayList<ParseObject> list1 = (ArrayList) ParseUser.getCurrentUser().getList("notes");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes, container, false);
mPB = (ProgressBar) rootView.findViewById(R.id.progress_bar);
mPB.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mPB.setVisibility(View.GONE);
}
}, 3500);
notesList = (ListView) rootView.findViewById(R.id.lv_contact);
user = ParseUser.getCurrentUser();
FAB = (FloatingActionButton) rootView.findViewById(R.id.fab);
FAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), AddNoteActivity.class);
getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
startActivity(intent);
}
});
final ArrayAdapter<ParseObject> arrayAdapter;
arrayAdapter = new ArrayAdapter<ParseObject>(getContext(), android.R.layout.simple_list_item_1, list1);
final RelativeLayout emptyView = (RelativeLayout) rootView.findViewById(R.id.empty);
if (list1 == null) {
emptyView.setVisibility(View.VISIBLE);
mPB.setVisibility(View.GONE);
} else {
notesList.setAdapter(arrayAdapter);
emptyView.setVisibility(View.INVISIBLE);
}
notesList.setLongClickable(true);
notesList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new android.app.AlertDialog.Builder(getContext())
.setTitle("Delete?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
list1.remove(position);
ParseUser.getCurrentUser().remove("notes");
ParseUser.getCurrentUser().put("notes", list1);
ParseUser.getCurrentUser().saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(getContext(), "Deleted", Toast.LENGTH_SHORT).show();
if (list1.toString().equals("[]")) {
emptyView.setVisibility(View.VISIBLE);
mPB.setVisibility(View.GONE);
} else {
notesList.setAdapter(arrayAdapter);
emptyView.setVisibility(View.INVISIBLE);
}
}
});
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
return true;
}
});
return rootView;
}
}