Hi i am facing trouble right now facing bug while deleting a record from sqlite database. just make Sqlite class i am working on three file. One is placeorder file where i call the cartAdpter this displays all records in custom list when i click on the holder.delete button i made a obj of sqlite db and call delete method but it generate exaption .
Place Order File
public class PlaceOrder extends Activity {
String [] pIds;
String [] pNames;
String [] pPrizes;
ListView lv;
ImageView bck;
String [] listImages;
String food_id;
String userdata[];
Intent i;
TextView totalprze;
float tprize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_order);
lv=(ListView)findViewById(R.id.cart_list);
bck=(ImageView) findViewById(R.id.placeholder_bg_btn);
totalprze =(TextView) findViewById(R.id.place_order_price);
i=new Intent(this,Menu.class);
bck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle=new Bundle();
//bundle.putStringArray("images", ListImages);
bundle.putString("food_id", food_id);
bundle.putStringArray("images", listImages);
bundle.putStringArray("userData",userdata);
i.putExtras(bundle);
startActivity(i);
}
});
if(this.getIntent().getExtras()!=null)
{
Bundle b=this.getIntent().getExtras();
pIds=b.getStringArray("pId");
pNames=b.getStringArray("PName");
pPrizes=b.getStringArray("pPrize");
userdata=b.getStringArray("userData");
tprize=b.getFloat("totalprize");
food_id=b.getString("food_id");
listImages=b.getStringArray("images");
String prz=Float.toString(tprize);
totalprze.setText("$"+prz);
lv.setAdapter(new cartAdapter(PlaceOrder.this, pIds, pNames, pPrizes));
}
}
}
cartAdapter file
public class cartAdapter extends BaseAdapter{
String [] pIdz;
String [] pNamz;
String [] pPrizs;
// List<String> imges;
Context context;
private ShopingCartHelper obj;
private static LayoutInflater inflater=null;
JSONArray jCat = null;
int count=0;
ProgressDialog pDialog;
public cartAdapter(PlaceOrder ctx,
String[] pIds,String[] pNams, String[] pprise) {
pIdz=pIds;
pNamz=pNams;
context=ctx;
pPrizs=pprise;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(pIdz==null){
Toast.makeText(context, "There is issue with net connection.", Toast.LENGTH_LONG).show();
//Intent i=new Intent(context,WelcomeActivity.class);
//context.startActivity(i);
return count ;
}else{
return pIdz.length;
}
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class holder{
TextView pid;
TextView pname;
TextView pprise;
Button delete;
ListView lv;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final holder hldr=new holder();
View rowView = null;
Bitmap bitmap = null;
rowView = inflater.inflate(R.layout.place_order_item_list, null);
hldr.pid=(TextView) rowView.findViewById(R.id.item_id);
hldr.pname=(TextView) rowView.findViewById(R.id.item_name);
hldr.pprise=(TextView) rowView.findViewById(R.id.item_price);
hldr.delete=(Button) rowView.findViewById(R.id.delete);
hldr.pid.setText(pIdz[position]);
hldr.pname.setText(pNamz[position]);
hldr.pprise.setText(pPrizs[position]);
//
// Picasso.with(context).load(imgs[position]).into(hldr.img);
hldr.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// rowView.remove(position); //removing from your List
Toast.makeText(context, "Delete",Toast.LENGTH_LONG).show();
int pid=Integer.parseInt(hldr.pid.getText().toString());
obj.delProduct(pid);
}
});
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// // TODO Auto-generated method stub
// cartAdapter.this.pIdz.remove[position];
// Toast.makeText(context, "hi",Toast.LENGTH_LONG).show();
}
});
return rowView;
// TODO Auto-generated method stub
}
}
Shopping Cart Helper file
public class ShopingCartHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "cart.db";
public static final String PRODUCT_TABLE_NAME = "prodcut";
public static final String p_id = "id";
public static final int VERSION =1;
SQLiteOpenHelper helper;
SQLiteDatabase db;
// Context context;
public ShopingCartHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
this.getWritableDatabase();
db.execSQL(
"create table prodcut " +
"(id integer primary key, user_id text,product_name text,price text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS prodcut");
onCreate(db);
}
public boolean insertProduct (String user_id, String product_name, String price)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("user_id", user_id);
contentValues.put("product_name", product_name);
contentValues.put("price", price);
db.insert("prodcut", null, contentValues);
return true;
}
public void delProduct(int pid)
{
SQLiteDatabase database =this.getReadableDatabase();
db.rawQuery( "delete * from prodcut where id="+pid+"", null );
}
public Cursor getData(String user_id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from prodcut where user_id="+user_id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, PRODUCT_TABLE_NAME);
return numRows;
}
}
Error
10-27 10:28:48.608: E/AndroidRuntime(925): FATAL EXCEPTION: main
10-27 10:28:48.608: E/AndroidRuntime(925): java.lang.NullPointerException
10-27 10:28:48.608: E/AndroidRuntime(925): at student.briyani.cartAdapter$1.onClick(cartAdapter.java:114)
10-27 10:28:48.608: E/AndroidRuntime(925): at android.view.View.performClick(View.java:3511)
10-27 10:28:48.608: E/AndroidRuntime(925): at android.view.View$PerformClick.run(View.java:14105)
10-27 10:28:48.608: E/AndroidRuntime(925): at android.os.Handler.handleCallback(Handler.java:605)
10-27 10:28:48.608: E/AndroidRuntime(925): at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 10:28:48.608: E/AndroidRuntime(925): at android.os.Looper.loop(Looper.java:137)
10-27 10:28:48.608: E/AndroidRuntime(925): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-27 10:28:48.608: E/AndroidRuntime(925): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 10:28:48.608: E/AndroidRuntime(925): at java.lang.reflect.Method.invoke(Method.java:511)
10-27 10:28:48.608: E/AndroidRuntime(925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-27 10:28:48.608: E/AndroidRuntime(925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-27 10:28:48.608: E/AndroidRuntime(925): at dalvik.system.NativeStart.main(Native Method)
Aucun commentaire:
Enregistrer un commentaire