我有一个显示用户列表的列表视图。我想要的是,当用户单击任何项目时,它应该从json数组中删除。下面的代码运行正常,但是当再次启动该应用程序时,它将再次显示所有用户。
private void showStudentInfo(final List<StudentDetails> studentDetails) {
if(studentDetails != null) {
final ArrayList<Map<String, Object>> itemDataList = new ArrayList<>();
int size = studentDetails.size();
for(int i=0; i<size; i++)
{
StudentDetails student = studentDetails.get(i);
Map<String,Object> listItemMap = new HashMap<>();
listItemMap.put("id", student.getId());
listItemMap.put("name", student.getName());
itemDataList.add(listItemMap);
}
final SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemDataList,android.R.layout.simple_list_item_2,
new String[]{"id","name"},new int[]{android.R.id.text1,android.R.id.text2});
studentListView.setAdapter(simpleAdapter);
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setPositiveButton("View", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, selectedItem, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("id",studentDetails.get(position).getId());
intent.putExtra("name",studentDetails.get(position).getName());
intent.putExtra("email",studentDetails.get(position).getEmail());
intent.putExtra("phone",studentDetails.get(position).getPhone());
intent.putExtra("address",studentDetails.get(position).getAddresS());
startActivity(intent);
}
});
alertDialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Data deleted", Toast.LENGTH_SHORT).show();
itemDataList.remove(position);
simpleAdapter.notifyDataSetChanged();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}