Android Open Source - Lists-android Deletable Item Adapter






From Project

Back to project page Lists-android.

License

The source code is released under:

Apache License

If you think the Android project Lists-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.dropbox.examples.lists;
// w  w w  .  jav a2s  .  co  m
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

// A generic adapter that supports deleting elements, with a callback when items are deleted.
public abstract class DeletableItemAdapter<T> extends ArrayAdapter<T> {
    private List<T> items;
    private Context context;
    private OnItemDeletedListener<T> listener;
    private Boolean editable;

    public DeletableItemAdapter(List<T> items, Context ctx, Boolean editable) {
        super(ctx, R.layout.listitem, items);
        this.items = items;
        this.context = ctx;
        this.editable = editable;
    }

    protected abstract String getText(int position);

    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.listitem, parent, false);
        }

        TextView tv = (TextView) convertView.findViewById(R.id.text);
        tv.setText(this.getText(position));

        final T item = items.get(position);

        Button deleteButton = (Button) convertView.findViewById(R.id.deleteButton);

        if (editable) {
            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) listener.onItemDeleted(item);
                }
            });
        } else {
            deleteButton.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    public void setOnItemDeleted(OnItemDeletedListener<T> listener) {
        this.listener = listener;
    }
}




Java Source Code List

com.dropbox.examples.lists.DeletableItemAdapter.java
com.dropbox.examples.lists.ListActivity.java
com.dropbox.examples.lists.ListAdapter.java
com.dropbox.examples.lists.ListItemAdapter.java
com.dropbox.examples.lists.ListsApplication.java
com.dropbox.examples.lists.MainActivity.java
com.dropbox.examples.lists.OnItemDeletedListener.java
com.dropbox.examples.lists.SharingDialogFragment.java