我在listviewholder.cart上得到一个空指针异常
以下关于驱逐数据库助手: -
package com.emagic.newproject.adpaters;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.emagic.newproject.ItemsActivity;
import com.emagic.newproject.NewProject;
import com.emagic.newproject.R;
import com.emagic.newproject.model.Items;
import com.emagic.newproject.model.ItemsListRowViewHolder;
import com.emagic.newproject.util.SQLiteHelper;
import java.util.Date;
import java.util.List;
/**
* Created by mkodekar on 12/7/2015.
*/
public class ItemListAdapter extends RecyclerView.Adapter<ItemsListRowViewHolder> {
Context mContext;
public List<Items> itemsList;
ImageLoader imageLoader = NewProject.getInstance().getImageLoader();
private int focusedItem = 0;
SQLiteHelper db;
ItemsActivity itemsActivity = new ItemsActivity();
private static final int TEXT_ID = 0;
int total , price, quan;
String value,date, productName, email, rate, quantity, amount;
public ItemListAdapter(Context context, List<Items> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}
@Override
public ItemsListRowViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list_item, null);
ItemsListRowViewHolder holder = new ItemsListRowViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final ItemsListRowViewHolder listRowViewHolder, final int position) {
Items itemsListItems = itemsList.get(position);
listRowViewHolder.itemView.setSelected(focusedItem == position);
listRowViewHolder.getLayoutPosition();
imageLoader = NewProject.getInstance().getImageLoader();
listRowViewHolder.thumbnail.setImageUrl(itemsListItems.getImage(), imageLoader);
listRowViewHolder.thumbnail.setDefaultImageResId(R.drawable.ic_launcher);
listRowViewHolder.name.setText(itemsListItems.getName());
setAnimation(listRowViewHolder.itemView, focusedItem);
listRowViewHolder.productPrice.setText(itemsListItems.getPrice());
listRowViewHolder.description.setText(itemsListItems.getDescription());
listRowViewHolder.quantity.setGravity(Gravity.CENTER);
listRowViewHolder.quantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Quantity");
builder.setMessage("Add the amount of pieces to be required");
builder.setCancelable(true);
final EditText input = new EditText(mContext);
input.setId(TEXT_ID);
input.setGravity(Gravity.CENTER_HORIZONTAL);
input.setSingleLine();
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
listRowViewHolder.quantity.setText(value);
Log.d(ItemsActivity.TAG, "Value: " + value);
try {
// ** here you can perform ur calculations
price = Integer.parseInt(listRowViewHolder.productPrice.getText().toString());
quan = Integer.parseInt(listRowViewHolder.quantity.getText().toString());
total = price * quan;
amount = String.valueOf(total);
listRowViewHolder.totalAmount.setText(amount);
} catch (Exception ex) {
Toast.makeText(mContext, "Enter a number", Toast.LENGTH_SHORT).show();
}
return;
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
return;
}
});
builder.show();
}
});
listRowViewHolder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String count = listRowViewHolder.quantity.getText().toString();
int cnt = Integer.parseInt(count);
cnt++;
listRowViewHolder.quantity.setText("" + cnt);
try {
// ** here you can perform ur calculations
price = Integer.parseInt(listRowViewHolder.productPrice.getText().toString());
quan = Integer.parseInt(listRowViewHolder.quantity.getText().toString());
total = price * quan;
amount = String.valueOf(total);
listRowViewHolder.totalAmount.setText(amount);
} catch (Exception ex) {
Toast.makeText(mContext, "Enter a number", Toast.LENGTH_SHORT).show();
}
}
});
listRowViewHolder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String count = listRowViewHolder.quantity.getText().toString();
int cnt = Integer.parseInt(count);
if (cnt == 0) {
listRowViewHolder.quantity.setText("0");
} else {
cnt--;
listRowViewHolder.quantity.setText("" + cnt);
}
try {
// ** here you can perform ur calculations
price = Integer.parseInt(listRowViewHolder.productPrice.getText().toString());
quan = Integer.parseInt(listRowViewHolder.quantity.getText().toString());
total = price * quan;
amount = String.valueOf(total);
listRowViewHolder.totalAmount.setText(amount);
} catch (Exception ex) {
Toast.makeText(mContext, "Enter a number", Toast.LENGTH_SHORT).show();
}
}
});
listRowViewHolder.cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ItemsActivity itemsActivity = new ItemsActivity();
int objectLength = itemsActivity.object;
int position;
for(position = 0; position <= objectLength; position++) {
listRowViewHolder.quantity.setText(value);
quantity = String.valueOf(value);
productName = listRowViewHolder.name.getText().toString();
rate = listRowViewHolder.productPrice.getText().toString();
SharedPreferences mPrefs = mContext.getSharedPreferences("Email", 0);
email = mPrefs.getString("SessionEmail", "");
Date d = new Date();
CharSequence dateChar = DateFormat.format("d-MM-yyyy ", d.getTime());
date = String.valueOf(dateChar);
amount = String.valueOf(total);
db.createOrder(date , productName, email, rate, quantity, amount); //problemn is here.
Toast.makeText(mContext, date + "" + productName+ " " + email + " " + rate + " " + quantity + " " + amount , Toast.LENGTH_SHORT).show();
}
}
});
}
public void clearAdapter() {
itemsList.clear();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return (null != itemsList ? itemsList.size() : 0);
}
private void setAnimation(View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && position >= focusedItem) {
Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
focusedItem = position;
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && position >= focusedItem) {
focusedItem = position;
}
}
这是sqlite助手
package com.emagic.newproject.util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.HashMap;
public class SQLiteHelper extends SQLiteOpenHelper {
private static final String TAG = SQLiteHelper.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
// Login table name
private static final String TABLE_USER = "user";
private static final String TABLE_ORDER = "order";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_PASSWORD = "confirmPassword";
private static final String KEY_MOBILE_NUMBER = "mobileNumber";
private static final String KEY_ADDRESS = "address";
private static final String ORDER_ID = "order_id";
private static final String PRODUCT_NAME = "productName";
private static final String QUANTITY = "quantity";
private static final String RATE = "rate";
private static final String AMOUNT = "amount";
private static final String ORDER_DATE = "date";
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_PASSWORD + " TEXT,"
+ KEY_MOBILE_NUMBER + " TEXT," + KEY_ADDRESS + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
String CREATE_ORDER_TABLE = "CREATE TABLE " + TABLE_ORDER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + PRODUCT_NAME + " TEXT," + KEY_EMAIL +" TEXT,"
+ QUANTITY + "TEXT," + RATE + " TEXT," + AMOUNT + " TEXT," + ORDER_DATE + " TEXT" + ")";
db.execSQL(CREATE_ORDER_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
db.execSQL("DROP TABLE IF EXISTS" + TABLE_ORDER);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
*/
public void addUser(String name, String email, String confirmPassword, String mobileNumber, String address) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_PASSWORD, confirmPassword); // Email
values.put(KEY_MOBILE_NUMBER, mobileNumber); // Created At
values.put(KEY_ADDRESS, address);
// Inserting Row
long id = db.insert(TABLE_USER, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
/**
* Getting user data from database
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("confirmPassword", cursor.getString(3));
user.put("mobileNumber", cursor.getString(4));
user.put("address", cursor.getString(5));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
/**
* Re crate database Delete all tables and create them again
*/
public void deleteUsers() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_USER, null, null);
db.close();
Log.d(TAG, "Deleted all user info from sqlite");
}
public void createOrder(String date, String productName, String email, String rate, String quantity, String amount) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ORDER_DATE, date);
values.put(PRODUCT_NAME, productName); // Email
values.put(KEY_EMAIL, email);
values.put(RATE, rate); // Created At
values.put(QUANTITY, quantity);
values.put(AMOUNT, amount);
// Inserting Row
long id = db.insert(TABLE_ORDER, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
/**
* Getting order data from database
*/
public HashMap<String, String> getOrderDetails() {
HashMap<String, String> order = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_ORDER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
order.put("date", cursor.getString(1));
order.put("productName", cursor.getString(2));
order.put("email", cursor.getString(3));
order.put("rate", cursor.getString(4));
order.put("quantity", cursor.getString(5));
order.put("amount", cursor.getString(6));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching order from Sqlite: " + order.toString());
return order;
}
/**
* Re crate database Delete all tables and create them again
*/
public void deleteOrder() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_ORDER, null, null);
db.close();
Log.d(TAG, "Deleted all order info from sqlite");
}
}
任何帮助将不胜感激
Porblem是我发送一个onclick甚至是从数据库和适配器
保存数据上面提到的问题。