简体   繁体   English

如何永久删除列表视图项?

[英]How can I delete a listview item permanently?

I have this code, where the listview item gets deleted from the list, but after I reload the listview, the item re-appears again. 我有这个代码,listview项目从列表中删除,但在我重新加载listview后,该项目再次出现。 How do I delete the item permanently? 如何永久删除该项目? Please help me. 请帮我。

The code for deleting the listview item : 删除listview项的代码:

 b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 checkedItemPositions = listView.getCheckedItemPositions();
                int itemCount = listView.getCount();

                for(int i=itemCount-1; i >= 0; i--){
                    if(checkedItemPositions.get(i)){
                        str=list.get(i);
                        adapter.remove(str);
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();
                listView.setAdapter(adapter);

            }
        });

Edit: As asked by @Nirup Iyer, this is my complete code: 编辑:正如@Nirup Iyer所说,这是我的完整代码:

   package com.example.myapplication;

import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import static com.example.myapplication.Pop_up.s2;
import static com.example.myapplication.Pop_up.text1;
import static com.example.myapplication.Pop_up.thedate;
import static com.example.myapplication.Pop_up_2.aManager;
import static com.example.myapplication.Pop_up_2.dat;
import static com.example.myapplication.Pop_up_2.dite;
import static com.example.myapplication.Pop_up_2.pIntent;
import static com.example.myapplication.Pop_up_2.pqr;
import static com.example.myapplication.Pop_up_2.s;
import static com.example.myapplication.modify_events.st;
import static com.example.myapplication.modify_events_1.st1;
import static com.example.myapplication.modify_events_2.st2;

public class modify_events_3 extends nav_draw {
    ListView listView;
    TextView textView;
    ArrayAdapter<String> adapter;
    String day,month,year;
    Button b1,b2;
    String str=null;
    List<String> list = new ArrayList<String>();
    SparseBooleanArray checkedItemPositions;
    int i,j;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_week);
        listView = (ListView) findViewById(R.id.listView);
        textView = (TextView) findViewById(R.id.textView);
        b1=(Button)findViewById(R.id.button10);
        b2=(Button)findViewById(R.id.button11);
        final String arr[]=new String[10];
        if(dite==null)
        {
            startActivity(new Intent(modify_events_3.this,nav_draw.class));
            Toast.makeText(getApplicationContext(),"Please create an event first", Toast.LENGTH_SHORT).show();
        }else {
               DateFormat format2 = new SimpleDateFormat("EEE");
            DateFormat format3 = new SimpleDateFormat("MMM");
            DateFormat format4 = new SimpleDateFormat("yyyy");
            day = format2.format(dite);
             month = format3.format(dite);
             year = format4.format(dite);
            list.add(pqr);
            adapter = new ArrayAdapter<String>(modify_events_3.this, android.R.layout.simple_list_item_multiple_choice, list);
            //Toast.makeText(getApplicationContext(), day + "" + month + "" + year, Toast.LENGTH_LONG).show();
        }
        if (((st2.equals(day)) && (st1.equals(month)) && (st.equals(year)))) {
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            listView.setAdapter(adapter);
            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SparseBooleanArray checked = listView.getCheckedItemPositions();
                    ArrayList<String> selectedItems = new ArrayList<String>();
                    for(int i=0;i<checked.size();i++) {
                        int position;
                        position = checked.keyAt(i);
                        if (checked.valueAt(i))
                            selectedItems.add(adapter.getItem(position));
                        for (int j = 0; j < arr.length; j++) {
                            str = listView.getItemAtPosition(i).toString();
                            str = arr[j];
                        }
                    }
                        startActivity(new Intent(modify_events_3.this,Pop_up.class));
                    Intent incoming = getIntent();
                    String dat = incoming.getStringExtra("date");
                        thedate.setText(dat);
                        text1.setText(str);
                        Toast.makeText(getApplicationContext(), "Have anything else in mind? ;)", Toast.LENGTH_SHORT).show();
                }
            });
            b2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     checkedItemPositions = listView.getCheckedItemPositions();
                    int itemCount = listView.getCount();

                    for(int i=itemCount-1; i >= 0; i--){
                        if(checkedItemPositions.get(i)){
                            str=list.get(i);
                            adapter.remove(str);
                        }
                    }
                    checkedItemPositions.clear();
                    adapter.notifyDataSetChanged();
                    adapter=null;
                    listView.setAdapter(adapter);
                    aManager.cancel(pIntent);
                    Toast.makeText(getApplicationContext(), "The alarm has been deleted successfully", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(modify_events_3.this,nav_draw.class));
                }
            });
        } else {
            adapter=null;
            listView.setAdapter(adapter);
            Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "No event was created", Snackbar.LENGTH_LONG);
            snackbar.show();
        }
    }
}

Do I have to write the item in a file? 我是否必须在文件中写入该项目?

From what you have said and from seeing the code, the item will reappear on the list. 根据您的说法和查看代码,该项目将重新出现在列表中。 This is because, when you reload the activity, the onCreate() method is called again, and the entire code in the onCreate() is executed. 这是因为,当您重新加载活动时,将再次调用onCreate()方法,并执行onCreate()中的整个代码。 That means your list is created again, the adapter is initialized again and the listview is populated again. 这意味着再次创建列表,再次初始化适配器并再次填充列表视图。 Any changes you make to the list will last until the activity remains active. 您对列表所做的任何更改都将持续到活动保持活动状态。

If you want the deleted item to be deleted even after the activity dies, you can write your list item into a persistent storage (Database, SharedPreferences or internal/external storage)(More details here ). 如果您希望在活动消失后删除已删除的项目,则可以将列表项写入持久存储(数据库,SharedPreferences或内部/外部存储)( 此处更多详细信息)。 This way, any modifications you make can be saved in the storage and read back from there in the onCreate method. 这样,您所做的任何修改都可以保存在存储中,并在onCreate方法中从那里读回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM