M.Bill M.Bill - 4 months ago 24
Android Question

How to wire up a delete button to delete one item on a listview?

I know there are lots of threads with more or less same topic but none of them covers my situation:

I am trying to get my delete button to delete one row on the list view and the delete button appear in each row I got this part to work but I can't get it to work in my Main Activity. The code keeps breaking every time I put this part in my code:

ImageButton removeButton = (ImageButton) findViewById(R.id.btnDel);
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onRemoveButtonClick();
}
private void onRemoveButtonClick() {
ToDoItem item = (ToDoItem) v.getTag();
notifyDataSetChanged();
}


my
MainActivity
works fine without this bit of code. I don't know if it is the code or if it is where I am placing it in my
MainActivity
if someone would please tell that would much appreciated.

MainActivity.java

public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "ToDoApp";
private ToDoListManager listManager;

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

ListView todoList = (ListView) findViewById(R.id.todo_list);
listManager = new ToDoListManager(getApplicationContext());
ToDoItemAdapter adapter = new ToDoItemAdapter(this, listManager.getList());


todoList.setAdapter(adapter);
ImageButton addButton = (ImageButton) findViewById(R.id.add_item);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onAddButtonClick();
}
});
}

@Override
protected void onPause() {
super.onPause();
listManager.saveList();
}

private void onAddButtonClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.add_item);

final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);

builder.setPositiveButton(
R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Add item to list
ToDoItem item = new ToDoItem(
input.getText().toString(),
false
);
listManager.addItem(item);
Log.i(LOG_TAG, input.getText().toString());
}
});
builder.setNegativeButton(
R.string.cancel,
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int Which) {
dialog.cancel();
}
});

builder.show();
}

private class ToDoItemAdapter extends ArrayAdapter<ToDoItem> {

private Context context;
private List<ToDoItem> items;
public ToDoItemAdapter(Context context, List<ToDoItem> items){
super(context,-1, items);

this.context = context;
this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.to_do_item_layout, parent, false);
}

TextView textView = (TextView) convertView.findViewById(R.id.item);
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);

textView.setText(items.get(position).getDescription());
checkBox.setChecked(items.get(position).isComplete());

convertView.setTag(items.get(position));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToDoItem item = (ToDoItem) v.getTag();
item.toggleComplete();
notifyDataSetChanged();
}
});
return convertView;
}
}
}


ToDoListManager.java

public class ToDoListManager {


private static final String APP_PREFERENCES = "todoapp";
private static final String TODO_ITEMS = "itemslist";

private List<ToDoItem> items;
private SharedPreferences savedData;

public ToDoListManager(Context context) {

savedData = context.getSharedPreferences (
APP_PREFERENCES,
Context.MODE_PRIVATE
);

String json = savedData.getString(TODO_ITEMS, null);
if (json == null) {
items = new ArrayList<>();
} else {

Type type = new TypeToken<List<ToDoItem>>() {}.getType();
items = new Gson().fromJson(json, type);
}


}
public List<ToDoItem> getList() {
return items;
}
public void addItem(ToDoItem item) {
items.add(item);
saveList();

}
public void saveList() {
SharedPreferences.Editor edit =savedData.edit();
edit.clear();
String json = new Gson().toJson(items);

edit.putString(TODO_ITEMS, json);
edit.apply();
}

}


ToDoItem.java

public class ToDoItem {
private String description;
private boolean isComplete;

public ToDoItem (String description,boolean isComplete) {
this.description = description;
this.isComplete = isComplete;
}

public String getDescription() {
return description;
}
public boolean isComplete() {
return isComplete;
}
public void toggleComplete() {
isComplete = !isComplete;
}
@Override
public String toString() {
return description;
}
}


enter image description here

Answer

1.) Add a button to your to_do_item_layout first

2.) Now add this code to add a button to each item

Button btn = (Button) convertView.findViewById(R.id.my_btn);

3.) Add a listener to it

btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         items.remove(items.get(position));         
      }
});