Deleting item from ListView and SharedPreferences
I've been having a lot of trouble trying to figure out how to delete an
item from a ListView. I saw some similar questions, though none with
answers that helped me to produce a working solution.
I have a layout which includes the listview, an edittext and a button. On
button click, the edittext's string is added to the listview. Clicking on
a listview item opens a dialog which asks for confirmation to delete that
item. When confirmation is received, the item appears to delete. But when
the delete button is clicked on another item, the item deletes, but the
previous item reappears in its spot. From my limited understanding, I
assume that the item doesn't delete from the SharedPreferences, and so
when they are loaded again, the item reappears. I've tried clearing the
SharedPreferences before saving after delete, but this didn't help (though
I could've been doing it wrong).
I've included the whole class incase anyone has time to test it out in
their IDE, and incase any other beginners can make use of the working
parts.
package com.example.send2omni;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.AndroidCharacter;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ContextsList extends Activity {
public static final String CONTEXTS = "contexts_list";
EditText display;
ListView lv;
public ArrayAdapter<String> adapter;
Button addButton;
String temp_appender;
String appender = "";
List<String> dataset;
String[] splitup;
public String items;
ArrayList<String> itemsarraylist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contexts_list);
display = (EditText) findViewById(R.id.editText);
lv = (ListView) findViewById(R.id.listView);
addButton = (Button) findViewById(R.id.bAdd);
LoadPreferences();
lv.setAdapter(adapter);
LoadPreferences();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appender = LoadPreferences();
if(display.getText().toString() != null){
temp_appender = display.getText().toString();
String string_to_split = appender + "," + temp_appender;
List<String> items =
Arrays.asList(string_to_split.split(","));
adapter = new ArrayAdapter<String>
(getApplicationContext(), android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", string_to_split, null);
LoadPreferences();
}
display.setText("");
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
deleteItem(arg1);
}
});
}
protected void deleteItem(final View arg1)
{
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Delete");
dialogBuilder.setMessage
("Do you want to delete \"" + ((TextView) arg1).getText() + "\"?");
dialogBuilder.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String splitit = LoadPreferences();
List<String> listit = Arrays.asList(splitit.split(","));
ArrayList<String> itemsarraylist = new ArrayList<String>();
for(int i = 0; i <listit.size(); i++){
itemsarraylist.add(i, listit.get(i));
}
try {
adapter.remove(((TextView) arg1).getText().toString());
adapter.notifyDataSetChanged();
itemsarraylist.remove(((TextView)
arg1).getText().toString());
try{
SavePreferences("LISTS", null, itemsarraylist);
}catch(Exception e){
}
} catch (Exception e) {
Log.e("remove", "failed");
}
}
});
dialogBuilder.setNeutralButton("No", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog deleteDialog = dialogBuilder.create();
deleteDialog.show();
}
protected void SavePreferences(String key, String value, ArrayList<String>
opt) {
// TODO Auto-generated method stub
SharedPreferences data =
PreferenceManager.getDefaultSharedPreferences(this);
if (opt != null){
for (int i = 0 ; i <= opt.size(); i++)
value += opt.get(i) + ",";
}
SharedPreferences.Editor editor = data.edit();
editor.putString(key, value);
editor.commit();
}
protected String LoadPreferences(){
SharedPreferences data =
PreferenceManager.getDefaultSharedPreferences(this);
String dataSet = data.getString("LISTS", "");
dataset = Arrays.asList(dataSet.split(","));
splitup = dataSet.split(",");
List<String> items = Arrays.asList(dataSet.split(","));
ArrayList<String> itemsarraylist = new ArrayList<String>();
for(int i = 0; i <items.size(); i++){
itemsarraylist.add(i, items.get(i));
}
adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, itemsarraylist);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
return dataSet;
}
}
And here's the layout to save some time:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Context"
android:id="@+id/bAdd"
android:layout_gravity="center_horizontal|top"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/editText"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center"
android:longClickable="true"
android:headerDividersEnabled="false"
/>
</LinearLayout>
</LinearLayout>
I cannot express how grateful I'd be if anyone could get this to work.
It's the last step I have in completing my first app.
Thanks for your time everyone.
No comments:
Post a Comment