2015-10-27 84 views
-3

嗨,我現在面臨着從sqlite數據庫刪除記錄時面臨的問題。只是讓Sqlite類我正在處理三個文件。一個是placeorder文件,我稱之爲cartAdpter,它顯示自定義列表中的所有記錄,當我點擊holder.delete按鈕時,我做了一個sqlite數據庫的obj和調用delete方法,但它生成exaption。面臨錯誤,同時從sqlite數據庫中刪除記錄

下單文件

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) 
+0

什麼是cartAdapter文件中的第114行?檢查兩個點擊監聽器中的NULL是什麼。 –

+2

可能的重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – John3136

+0

至少看起來'obj'爲空。 –

回答

0

在適配器構造函數添加obj = new ShoppingCartHelper(context)

+1

應該沒有問題,他調用super(context,DATABASE_NAME,null,1),並且getWritableDatabase()和getReadableDatabase()不被覆蓋,因此從超類中調用。剛剛檢查了我自己的SQL代碼,我在OpenHelper中獲得了可用的數據庫,而無需存儲上下文。 –

+0

但是這個方法至少是過分的,我不確定它是否必須完全放棄 –

+0

謝謝Benjamin我已經解決了這個問題,這是正確的解決方案 –