Last active
August 1, 2017 04:30
-
-
Save philoniare/88f09df8c796091240ed35dc39dba301 to your computer and use it in GitHub Desktop.
ViewHolder + ButterKnife
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ohhstudio.mitc.inventory; | |
import android.content.ContentUris; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.CursorAdapter; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.ohhstudio.mitc.inventory.data.ItemContract.ItemEntry; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
/** | |
* This is an adapter for a list or grid view | |
* that uses a Cursor of item data as its data source. This adapter knows | |
* how to create list items for each row of item data in the Cursor. | |
*/ | |
public class ItemCursorAdapter extends CursorAdapter { | |
// Number of current item's quantity | |
private int mQuantity; | |
// The view's context | |
private Context mContext; | |
// Item's name in String | |
private String mNameString; | |
// Item's price in String | |
private String mPriceString; | |
// Item's quantity in String | |
private String quantityString; | |
// Item's id in the table | |
private int mItemId; | |
/** | |
* Constructs a new ItemCursorAdapter. | |
* | |
* @param context The context | |
* @param c The cursor from which to get the data. | |
*/ | |
public ItemCursorAdapter(Context context, Cursor c) { | |
super(context, c, 0 /* flags */); | |
} | |
/** | |
* Makes a new blank list item view. No data is set (or bound) to the views yet. | |
* | |
* @param context app context | |
* @param cursor The cursor from which to get the data. The cursor is already | |
* moved to the correct position. | |
* @param parent The parent to which the new view is attached to | |
* @return the newly created list item view. | |
*/ | |
@Override | |
public View newView(Context context, Cursor cursor, ViewGroup parent) { | |
// Inflate a list item view using the layout specified in list_item.xml | |
View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); | |
ViewHolder holder = new ViewHolder(view); | |
view.setTag(holder); | |
return view; | |
} | |
/** | |
* Custom view holder to hold buttons for each item row in the list | |
*/ | |
static class ViewHolder { | |
@BindView(R.id.item_name) TextView nameTextView; | |
@BindView(R.id.item_price) TextView priceTextView; | |
@BindView(R.id.item_quantity) TextView quantityTV; | |
@BindView(R.id.button_sale) Button saleButton; | |
public ViewHolder(View view) { | |
ButterKnife.bind(this, view); | |
} | |
} | |
/** | |
* This method binds the item data (in the current row pointed to by cursor) to the given | |
* list item layout. For example, the name for the current item can be set on the name TextView | |
* in the list item layout. | |
* | |
* @param view Existing view, returned earlier by newView() method | |
* @param context app context | |
* @param cursor The cursor from which to get the data. The cursor is already moved to the | |
* correct row. | |
*/ | |
@Override | |
public void bindView(View view, Context context, Cursor cursor) { | |
// Get context | |
mContext = context; | |
final ViewHolder holder = (ViewHolder) view.getTag(); | |
// Get item id for updating item quantity | |
int itemIdIndex = cursor.getColumnIndex(ItemEntry._ID); | |
mItemId = cursor.getInt(itemIdIndex); | |
// Find the columns of item attributes that we're interested in | |
int nameColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_NAME); | |
int quantityColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_QUANTITY); | |
int priceColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_PRICE); | |
// Read the item attributes from the Cursor for the current item | |
mNameString = cursor.getString(nameColumnIndex); | |
quantityString = cursor.getString(quantityColumnIndex); | |
mPriceString = cursor.getString(priceColumnIndex); | |
// Update the TextViews with the attributes for the current item | |
holder.nameTextView.setText(mNameString); | |
holder.quantityTV.setText(quantityString); | |
holder.priceTextView.setText(mPriceString); | |
// Clicking the Sale button subtracts one from item quantity | |
// then save it to the database | |
holder.saleButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Get current quantity | |
mQuantity = Integer.parseInt(holder.quantityTV.getText().toString().trim()); | |
// Only modify quantity if it's greater than 0 | |
if (mQuantity > 0) { | |
// Minus one from item's quantity | |
mQuantity--; | |
// Display updated quantity | |
String quantity = Integer.toString(mQuantity); | |
quantityString = quantity; | |
holder.quantityTV.setText(quantity); | |
// Save the item with the new quantity | |
saveItem(); | |
} else { | |
Toast.makeText(mContext, "This item has 0 quantity, can't sell.", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
// Save the item with new quantity | |
private void saveItem() { | |
ContentValues values = new ContentValues(); | |
// Store updated quantity of the current item | |
values.put(ItemEntry.COLUMN_ITEM_QUANTITY, quantityString); | |
// Create uri for current item with item id | |
Uri currentItemUri = ContentUris.withAppendedId(ItemEntry.CONTENT_URI, mItemId); | |
// Update the item's quantity in the database | |
int rowEffected = mContext.getContentResolver().update(currentItemUri, values, null, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment