0

**Hi there i am facing issue in making cart in app i want to remove row from custom list view and record from SQlite Database need help.I just want the particular row delete from the Custom list adapter and delete particular record from database **

This is cartAdapter file

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

this is database 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;

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

Exception trace

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) 
nano_nano
  • 12,351
  • 8
  • 55
  • 83
Babar Ali
  • 181
  • 1
  • 18
  • but i used integer same time i faced the null pointer exeception – Babar Ali Oct 22 '15 at 08:16
  • what is the errormessage you get? – nano_nano Oct 22 '15 at 08:16
  • this query line generate exception when deleting the record don't know why it's generate exception – Babar Ali Oct 22 '15 at 08:17
  • please post the exception – nano_nano Oct 22 '15 at 08:18
  • 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) – Babar Ali Oct 22 '15 at 08:20

0 Answers0