Thursday, September 12, 2013

Remove and Undo Listview Item with slide in Android

Remove and Undo Listview Item with slide in Android


This post about how to Undo removed item from listview like Gmail.
Screen Shot 


Source Code 

Activity Code :

package com.example.undo;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyListActivity extends ListActivity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              getListView().setDivider(null);
       }

       protected ArrayAdapter<String> createListAdapter() {
              return new MyListAdapter(this, getItems());
       }

       public static ArrayList<String> getItems() {
              ArrayList<String> items = new ArrayList<String>();
              for (int i = 0; i < 1000; i++) {
                     items.add(String.valueOf(i));
              }
              return items;
       }

       private static class MyListAdapter extends ArrayAdapter<String> {

              private Context mContext;

              public MyListAdapter(Context context, ArrayList<String> items) {
                     super(items);
                     mContext = context;
              }

              @Override
              public long getItemId(int position) {
                     return getItem(position).hashCode();
              }

              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                     TextView tv = (TextView) convertView;
                     if (tv == null) {
                           tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
                     }
                     tv.setText("This is row number " + getItem(position));
                     return tv;
              }
       }
}

****************************************************************************************

*********************************************************************




4 comments:

  1. Dear blogger,

    Your blogpost does not show the complete implementation. Also, if you use components of the ListViewAnimations library, which you do, please mention to do so.

    ReplyDelete
    Replies
    1. please download the code and try it. its working for me :)

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hey i want more item when i swipe on the item

    ReplyDelete

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...