面对购物车中的困难:NPE

时间:2015-10-22 08:11:11

标签: android sqlite

**您好我在app中制作购物车时遇到问题我想从自定义列表视图中删除行并从SQlite数据库记录需要帮助。我只想从自定义列表适配器删除特定行并从中删除特定记录数据库**

这是cartAdapter文件

import org.json.JSONArray;
import com.squareup.picasso.Picasso;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class cartAdapter  extends BaseAdapter{

     String [] pIdz;
     String [] pNamz;
     String [] pPrizs;
    // List<String> imges;
     Context context;
     ShopingCartHelper obj=new ShopingCartHelper(context);

     private static LayoutInflater inflater=null;
     JSONArray jCat = null;
     int count=0;
     ProgressDialog pDialog;


     public cartAdapter(PlaceOrder cat,
             String[] pIds,String[] pNams, String[] pprise) {

         pIdz=pIds;
         pNamz=pNams;

         context=cat;
         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.deleteRec(pid);
                //hldr.pid.remove(position);
                 //cartAdapter.this.
              }
        });
           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;

   private HashMap hp;
   SQLiteDatabase db;
 //Context context;
   public ShopingCartHelper(Context context)
   {
      super(context, DATABASE_NAME , null, 1);
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
       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 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;
   }



   public boolean deleteRec(int pid){
       db.delete(PRODUCT_TABLE_NAME, p_id+"="+Integer.toString(pid), null);
     return true;
   }
}

异常跟踪

FATAL EXCEPTION: main java.lang.NullPointerException at
 student.briyani.ShopingCartHelper.deleteRec(ShopingCartHelper.java:106)     at 
student.briyani.cartAdapter$1.onClick(cartAdapter.java:113) at android.view.View.performClick(View.java:3511) at android.view.View$PerformClick.run(View.java:14105) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) 

0 个答案:

没有答案