从sqlite数据库中删除记录时遇到错误

时间:2015-10-27 05:59:59

标签: android eclipse sqlite android-intent

您好,我正面临着从sqlite数据库删除记录时遇到的问题。只是制作Sqlite类我正在处理三个文件。一个是placeorder文件,我调用cartAdpter,当我点击holder.delete按钮时,它显示自定义列表中的所有记录我做了一个sqlite db的obj并调用了delete方法,但它产生了exaption。

下订单文件

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文件

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
    }
}

购物车帮助文件

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;
   }


}

错误

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)

1 个答案:

答案 0 :(得分:0)

在适配器构造函数中添加obj = new ShoppingCartHelper(context)