如何使用警告对话框删除列表视图项

How to delete listview items with an alert dialog

我尝试了很多不同的方法,但没有一种方法适用于我的代码。我知道如何制作警报对话框,但每当我将它放入我的 MainActivity 时,我都会遇到一堆似乎无法修复的错误。也许我把它放在错误的地方......或错别字?我不知道,这是我的 MainActivity,代码运行完美。我将如何在其中放置一个警报对话框,当用户单击“是”时删除一个 ListView 项目?谢谢你。

public class MainActivity extends Activity {

    EditText et;
    ListView lv;
    ArrayAdapter<String> adapter;
    Button btn;
    ArrayList<String> list = new ArrayList<String>();
    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et = (EditText) findViewById(R.id.editText);
        lv = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.button);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        lv.setAdapter(adapter);
        LoadPreferences();
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String task = et.getText().toString();


                adapter.add(task);
                adapter.notifyDataSetChanged();
                SavePreferences("LISTS", task);
            }
        });}

    protected void SavePreferences(String key, String value) {
        // TODO Auto-generated method stub
        SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = data.edit();
        editor.putString(key, value);
        editor.commit();


    }

    protected void LoadPreferences(){
        SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
        String dataSet = data.getString("LISTS", " ");

        adapter.add(dataSet);
        adapter.notifyDataSetChanged();
    }
}

这是我正在尝试使用的警报对话框:

@Override 
public void onItemClick(AdapterView<?> parent,
        View view, int position, long id) {

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Hello My Name is");
    alertDialog.setMessage("" + mNameList.get(position));
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        }
    });
    alertDialog.setButton2("Remove", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();
        }
    });

    alertDialog.show();
}

我想把它放在下面:

adapter.add(task);
            adapter.notifyDataSetChanged();
            SavePreferences("LISTS", task);
        }
    });}

这是它应该去的地方吗?

有了这个你想要:

  • 获取您要删除的字符串
  • 将其从用于填充 ListView 的字符串数组中删除
  • 用字符串的新内容更新 ListView。

    // Make the app remove an entry if the user "LONG CLICKS" the entry
        if (listViewList != null) {
            listViewList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    String strTemp = listViewList.getItemAtPosition(position).toString();
                    Toast.makeText(CustomListActivity.this, "Removed " + strTemp, Toast.LENGTH_SHORT).show();
                    strWords.remove(position);
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(CustomListActivity.this, R.layout.spinner_list, strWords);
                    listViewList.setAdapter(adapter);
                    return false;
                }
            });
        }
    

这个例子是在不同的情况下使用的,它来自我最近制作的一个应用程序 - 虽然稍微减少了,所以你只看到你需要的东西。使用警告对话框很容易适应工作。