gpt4 book ai didi

java - 如何在 Parse 中从数组中删除字符串

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:14 25 4
gpt4 key购买 nike

我目前正在使用适用于 Android 的 Parse API,并且我的应用程序中目前有一个字符串数组。我目前有一个包含所有字符串的 ListView。在我的应用程序的 ListView 本身内,我使用 Dialog 实现了 setOnItemLongClickListener 。我想从 Parse 中删除选定的 ListView 项目的字符串。

Image of the Database Row

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;
}

}

最佳答案

我能够通过这样做解决这个问题

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;
}
}

关于java - 如何在 Parse 中从数组中删除字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34222689/

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