title: Move item to bottom of listView on click
body: <p>I'm having a hard time figuring this one out, I need to be able to click on an item of a listview, and move that item to the bottom of the list with some background color change to make a difference, then if clicked back return it to the original position in the list.</p>
  
  <p>I've tried with list.addFooterView() but I can't make it work that way, it just attach an empty item to the footer, I couldn't make it go back to it's original position.</p>
  
  <p>Here is my code so far. Any ideas on how to tackle this are appreciated.</p>
  
  <p>ActivityProductsBuy.java</p>
  
  <pre><code>package com.roneskinder;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.database.Cursor;
  import android.database.DatabaseUtils;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteException;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Matrix;
  import android.graphics.Paint;
  import android.graphics.drawable.BitmapDrawable;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.ListView;
  import android.widget.TextView;
  
  import com.roneskinder.model.DataProducts;
  import com.roneskinder.utils.DatabaseHandler;
  import com.roneskinder.x111.R;
  
  public class ActivityProductsBuy extends Activity {
  
      protected Context mContext;
  
      protected DatabaseHandler db;
      private SQLiteDatabase newDB;
      private String tableProducts = DatabaseHandler.TABLE_PRODUCTS;
  
      protected ListView lvProducts;
      protected String title, idProduct, idGroceryList, ProductCode, ProductName;
      protected int totalItems, itemsInCart = 0;
      protected Button btn_itemsInCart;
      protected TextView tv_percentText;
  
      /** The data used for products */
      private List&lt;DataProducts&gt; info;
  
      /** The Adapter holds the listview. */
      private ProductsItemAdapter adapter1;
  
      private static final int ZBAR_SCANNER_REQUEST = 0;
  
      protected SharedPreferences preferences;
  
      protected String TAG = ActivityProductsBuy.class.getName();
  
      ImageView imgHolder;
      Paint paint;
      EditText edtPercentage;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_products_buy);
  
          mContext = getApplicationContext();
  
          // Get Database values for User
          db = new DatabaseHandler(mContext);
  
          preferences = PreferenceManager.getDefaultSharedPreferences(this);
  
          // remove and add products to DB
          //db.removeAllProducts();
          // this.addProducts();
  
          // get intent Extras (the title, list id)
          //title = getIntent().getExtras().getString("title");
          idGroceryList = getIntent().getExtras().getString("idGroceryList");
  
          lvProducts = (ListView) findViewById(R.id.lvProducts);
  
          info = new ArrayList&lt;DataProducts&gt;();
          imgHolder = (ImageView) findViewById(R.id.coringImage);
          btn_itemsInCart = (Button) findViewById(R.id.btn_itemsInCart);
          tv_percentText = (TextView) findViewById(R.id.percentText);
          tv_percentText.setText("0%");
  
          paint = new Paint();
  
          adapter1 = new ProductsItemAdapter(this, info);
  
          // load all database products into listview
          lvProducts.setAdapter(adapter1);
          loadProducts();
          totalItems = adapter1.getCount();
  
          lvProducts.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  itemsInCart++;
                  if(itemsInCart &lt;= totalItems){
                      btn_itemsInCart.setText("(" + itemsInCart + "/"+ totalItems +")");
                      // Fill percentage of image
                      float percentage = ((float)itemsInCart/(float)totalItems)*100;
                      Log.d(TAG, "percentage: " + percentage);
                      imgHolder.setImageResource(R.drawable.cart_default);
  
                      if (percentage &gt; 0 &amp;&amp; percentage &lt;= 100) {
                          imgHolder.setImageBitmap(doBottomToTopOperation(Math.round(percentage)));
                          tv_percentText.setText((int)percentage + "%");
                      }
                  }
              }
          });
      }
  
      public void loadProducts() {
          info.clear();
          ((BaseAdapter) lvProducts.getAdapter()).notifyDataSetChanged();
          Cursor cur = null;
          try {
              newDB = db.getWritableDatabase();
              cur = newDB.rawQuery(
                      "SELECT idProduct, ProductCode, name FROM "
                              + tableProducts + " WHERE idGroceryList = " + idGroceryList, null);
  
              Log.d(TAG, DatabaseUtils.dumpCursorToString(cur));
  
              if (cur != null) {
                  // tvNoProducts.setVisibility(View.GONE);
                  if (cur.moveToFirst()) {
                      do {
                          idProduct = cur.getString(cur.getColumnIndex("idProduct"));
                          ProductCode = cur.getString(cur.getColumnIndex("ProductCode"));
                          ProductName = cur.getString(cur.getColumnIndex("name"));
  
                          info.add(new DataProducts(ProductName, ProductCode,idProduct, "Vegetales", "0", idGroceryList));
                      } while (cur.moveToNext());
                  }
              } else {
                  // tvNoProducts.setVisibility(View.VISIBLE);
              }
          } catch (SQLiteException se) {
              Log.e(getClass().getSimpleName(),
                      "Could not create or Open the database");
          }
  
      }
  
      /**
       * The Class ProductsItemAdapter.
       */
      private class ProductsItemAdapter extends BaseAdapter {
  
          /** The context. */
          private Context context;
  
          /** The list. */
          private List&lt;DataProducts&gt; list;
  
          /** The inflater. */
          private LayoutInflater inflater;
  
          /**
           * Instantiates a new deal item adapter.
           * 
           * @param context
           *            the context
           * @param list
           *            the list
           */
          public ProductsItemAdapter(Context context, List&lt;DataProducts&gt; list) {
              this.context = context;
              this.list = list;
          }
  
          /*
           * (non-Javadoc)
           * 
           * @see android.widget.Adapter#getCount()
           */
          @Override
          public int getCount() {
              // TODO Auto-generated method stub
              return list.size();
          }
  
          /*
           * (non-Javadoc)
           * 
           * @see android.widget.Adapter#getItem(int)
           */
          @Override
          public Object getItem(int position) {
              // TODO Auto-generated method stub
              return null;
          }
  
          /*
           * (non-Javadoc)
           * 
           * @see android.widget.Adapter#getItemId(int)
           */
          @Override
          public long getItemId(int position) {
              // TODO Auto-generated method stub
              return 0;
          }
  
          /*
           * (non-Javadoc)
           * 
           * @see android.widget.Adapter#getView(int, android.view.View,
           * android.view.ViewGroup)
           */
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              if (convertView == null) {
                  inflater = (LayoutInflater) context
                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  convertView = inflater.inflate(R.layout.adapter_products, null);
              }
              TextView txt1 = (TextView) convertView
                      .findViewById(R.id.tvProductName);
              TextView txt2 = (TextView) convertView
                      .findViewById(R.id.tvProductCode);
              TextView tv1 = (TextView) convertView
                      .findViewById(R.id.tvProductPrice);
              TextView tv2 = (TextView) convertView
                      .findViewById(R.id.tvProductCategory);
  
              final DataProducts dInfo = list.get(position);
              txt1.setText(dInfo.getProductName());
              txt2.setText(dInfo.getProductCode());
  
              tv1.setText(dInfo.getProductPrice());
              // tv1.setPaintFlags(tv1.getPaintFlags() |
              // Paint.STRIKE_THRU_TEXT_FLAG);
  
              tv2.setText(dInfo.getProductCategory());
              return convertView;
          }
  
      }
  
      public Bitmap doBottomToTopOperation(int percentage) {
          Bitmap bitmapOriginal = ((BitmapDrawable) imgHolder.getDrawable()).getBitmap();
  
          Bitmap bitmapTarget = BitmapFactory.decodeResource(getResources(), R.drawable.cart_filled);
  
          int heightToCrop = bitmapTarget.getHeight() * (100 - percentage) / 100;
  
          Bitmap croppedBitmap = Bitmap.createBitmap(bitmapTarget, 0, heightToCrop, bitmapTarget.getWidth(), bitmapTarget.getHeight() - heightToCrop);
  
          Bitmap bmOverlay = Bitmap.createBitmap(bitmapOriginal.getWidth(),
                  bitmapOriginal.getHeight(), bitmapOriginal.getConfig());
          Canvas canvas = new Canvas(bmOverlay);
          canvas.drawBitmap(bitmapOriginal, new Matrix(), null);
          canvas.drawBitmap(croppedBitmap,
                  canvas.getWidth() - croppedBitmap.getWidth(),
                  canvas.getHeight() - croppedBitmap.getHeight(), null);
  
          return bmOverlay;
      }
  
  }
  </code></pre>
  
  <p>activity_products_buy.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;com.sothree.slidinguppanel.SlidingUpPanelLayout
          xmlns:sothree="http://schemas.android.com/apk/res-auto"
          android:id="@+id/sliding_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="bottom"
          sothree:umanoPanelHeight="68dp"
          sothree:umanoShadowHeight="4dp" &gt;
  
          &lt;!-- MAIN LAYOUT --&gt;
  
          &lt;ListView
              android:id="@+id/lvProducts"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@+id/myFilter" &gt;
          &lt;/ListView&gt;
  
          &lt;!-- SLIDING LAYOUT --&gt;
  
          &lt;LinearLayout
              android:id="@+id/dragView"
              android:layout_width="match_parent"
              android:layout_height="250dp"
              android:background="#ffffff"
              android:clickable="true"
              android:focusable="false"
              android:orientation="vertical" &gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="68dp"
                  android:background="#f0f0f0"
                  android:orientation="horizontal" &gt;
  
                  &lt;TextView
                      android:id="@+id/name"
                      android:layout_width="0dp"
                      android:layout_height="match_parent"
                      android:layout_weight="1"
                      android:gravity="center_vertical"
                      android:paddingLeft="10dp"
                      android:text="Total de Productos en el carrito"
                      android:textColor="#000"
                      android:textSize="14sp" /&gt;
  
                  &lt;Button
                      android:id="@+id/btn_itemsInCart"
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:gravity="center_vertical|right"
                      android:paddingLeft="10dp"
                      android:paddingRight="10dp"
                      android:text="(0)"
                      android:textColor="#000"
                      android:textSize="14sp" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"&gt;
  
                  &lt;ImageView
                      android:id="@+id/coringImage"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:src="@drawable/cart_default" /&gt;
  
                  &lt;TextView
                      android:id="@+id/percentText"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignBottom="@+id/coringImage"
                      android:layout_alignLeft="@+id/coringImage"
                      android:layout_alignRight="@+id/coringImage"
                      android:layout_alignTop="@+id/coringImage"
                      android:layout_margin="1dp"
                      android:gravity="center"
                      android:text="0%"
                      android:textColor="#ffffff" /&gt;
              &lt;/RelativeLayout&gt;
          &lt;/LinearLayout&gt;
      &lt;/com.sothree.slidinguppanel.SlidingUpPanelLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>adapter_products.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/top"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center_vertical"
      android:orientation="horizontal"
      android:paddingLeft="@dimen/pad_10dp"
      android:paddingRight="@dimen/pad_10dp"
      android:paddingTop="@dimen/pad_15dp" &gt;
  
      &lt;LinearLayout
          android:id="@+id/left_layout"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical" &gt;
  
          &lt;ImageView
              android:id="@+id/item_img"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:paddingBottom="@dimen/pad_10dp"
              android:src="@drawable/icon_mypurchase1" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:paddingLeft="@dimen/pad_10dp" &gt;
  
          &lt;LinearLayout
              android:id="@+id/right_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal" &gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" &gt;
  
                  &lt;TextView
                      android:id="@+id/tvProductName"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Nombre del Producto" /&gt;
  
                  &lt;TextView
                      android:id="@+id/tvProductCode"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:paddingTop="@dimen/pad_2.5dp"
                      android:text="Codigo" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;View
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:gravity="right"
                  android:orientation="vertical" &gt;
  
                  &lt;TextView
                      android:id="@+id/tvProductPrice"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/$45"
                      android:textColor="@color/red_main"
                      android:textSize="@dimen/txt_18sp" /&gt;
  
                  &lt;TextView
                      android:id="@+id/tvProductCategory"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Categoria"
                      android:textSize="@dimen/txt_12sp" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/LinearLayout&gt;
  
          &lt;View
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:layout_marginTop="@dimen/pad_15dp"
              android:background="@color/my_purchase_list_sap" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>