Red Junior Red Junior - 5 months ago 27
Android Question

How to delete listview items with an alert dialog

I have tried many different methods, but non of them work with my code. I know how to make an Alert Dialog, but whenever I put it in my MainActivity, I have a bunch of error I can't seem to fix. Maybe I am putting it in the wrong place...or typos? I don't know, here is my MainActivity, the code runs perfectly. How would I go about putting an Alert Dialog in there, that deletes a ListView item when the user clicks yes? Thank you.

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


Here is the Alert Dialog I am trying to use:

@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();
}


And I am trying to put it right under:

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


Is that where it's supposed to go?

Answer

With this you'd want to:

  • Get the string that you're wanting to remove
  • Remove it from the string array that's used to populate the ListView
  • Update the ListView with the new contents of the string.

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

This example is used in a different situation, it's from an app that I have made recently - although slightly cut down so you only see what you need to. It's very easy to adapt to work with an alert dialog.