1

I have Custom Adapter class, in which on button click we make Asynchronous call to server. So in the onPostExecute() method i want to delete particular Row on which button is clicked. Here is My Adapter class

public class CartAdapter extends BaseAdapter {

    private List<Products> cartList;

    private LayoutInflater inflater;

    Context context;

    public static final String URLL ="http://192.168.1.3/wordpress/upmeapi/class-woocommerce.php?function=remove_cart_api";
    RequestObject requestObject;


    public CartAdapter(Context ctx,List<Products> list){
        this.context = ctx;
        this.cartList = list;
    }
    @Override
    public int getCount() {
        return cartList.size();
    }

    @Override
    public Object getItem(int position) {
        return cartList.get(position);
    }

    @Override
    public long getItemId(int position) {
        Products c = cartList.get(position);
        long id = c.getProductId();
        return id;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View row = convertView;
        CartHolder holder = null;
        if (row == null){
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.cartview_helper,parent,false);
            holder = new CartHolder(row);
            row.setTag(holder);
        }
        else {
            holder =(CartHolder)row.getTag();
        }
        Products c = cartList.get(position);
        Picasso.Builder builder = new Picasso.Builder(context);
        Picasso picasso = builder.build();
        picasso.with(context).cancelRequest(holder.myImage);
        picasso.load(c.getProductImage())
                .placeholder(R.drawable.ic_plusone_tall_off_client)
                .resize(100,100)
                .into(holder.myImage);
       /* Picasso.with(context)
                .load(c.getProductImage())
                .placeholder(R.drawable.ic_plusone_tall_off_client)
                .resize(100, 75)
                .into(holder.myImage);*/

        holder.title.setText(c.getTitle());
        String stringdouble= Double.toString(c.getPrice());
        holder.price.setText(stringdouble);
        holder.quantity.setText(String.valueOf(c.getProductQuantity()));
        holder.totalPrice.setText(c.getTotalPrice());
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              long productId = getItemId(position);
                //holder.button.setTag(position);
                try {
                    ProductHelper productHelper = new ProductHelper();
                    productHelper.setProductId(productId);
                    ObjectMapper objectMapper = new ObjectMapper();
                    String req = objectMapper.writeValueAsString(productHelper);

                    requestObject = ExceptionRequest.generateRequest(req);
                    requestObject.setUrl(URLL);
                    new RemovefromList().execute(requestObject);
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });



        return row;
    }

    static class CartHolder {
        ImageView myImage;

        TextView title;

       //TextView descriptions;

         TextView price;

        TextView quantity;

        TextView totalPrice;

        Button button;


        public CartHolder(View v){
            myImage =(ImageView)v.findViewById(R.id.imageView2);

            title =(TextView)v.findViewById(R.id.carttitle);

           // descriptions =(TextView)v.findViewById(R.id.product_description);

            price =(TextView)v.findViewById(R.id.cart_price);

            quantity =(TextView)v.findViewById(R.id.item_quantity);

            totalPrice =(TextView)v.findViewById(R.id.sub_total);

            button =(Button)v.findViewById(R.id.remove_cart);



        }

    }


    private class RemovefromList extends AsyncTask<RequestObject, Void,JSONObject> {


        @Override
        protected JSONObject doInBackground(RequestObject... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();
            // RequestObject requestObject = new RequestObject();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(arg0[0], ServiceHandler.POST);

            JSONObject products = new JSONObject();

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {

                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    products = jsonObj.getJSONObject("rsBody");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return products;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);
            try {
                if (result!=null){
                    String status = result.getString("status");


                  //  cartList.remove();
                   // notifyDataSetChanged();
                    Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return;

        }
    }



}

inside onPostExecute() if i get status success this means on server side that particular product has been removed.but At our side we still see that products. so i want to remove that row and also want to update count of cartitem. Any help would be Appreciated in advanced.

2
  • You have to remove product in your list whatever your product is selected after call notify listener Commented Nov 6, 2015 at 4:56
  • yeah.. but problem is that inside postExecute() method i couldn't get position of item selected.. Commented Nov 6, 2015 at 5:02

4 Answers 4

2

please check this

send position in Async task constructor when button is clicked

private List<Products> cartList;
private LayoutInflater inflater;
Context context;

public static final String URLL ="http://192.168.1.3/wordpress/upmeapi/class-woocommerce.php?function=remove_cart_api";
RequestObject requestObject;


public CartAdapter(Context ctx,List<Products> list){
    this.context = ctx;
    this.cartList = list;
}
@Override
public int getCount() {
    return cartList.size();
}

@Override
public Object getItem(int position) {
    return cartList.get(position);
}

@Override
public long getItemId(int position) {
    Products c = cartList.get(position);
    long id = c.getProductId();
    return id;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    CartHolder holder = null;
    if (row == null){
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.cartview_helper,parent,false);
        holder = new CartHolder(row);
        row.setTag(holder);
    }
    else {
        holder =(CartHolder)row.getTag();
    }
    Products c = cartList.get(position);
    Picasso.Builder builder = new Picasso.Builder(context);
    Picasso picasso = builder.build();
    picasso.with(context).cancelRequest(holder.myImage);
    picasso.load(c.getProductImage())
            .placeholder(R.drawable.ic_plusone_tall_off_client)
            .resize(100,100)
            .into(holder.myImage);
   /* Picasso.with(context)
            .load(c.getProductImage())
            .placeholder(R.drawable.ic_plusone_tall_off_client)
            .resize(100, 75)
            .into(holder.myImage);*/

    holder.title.setText(c.getTitle());
    String stringdouble= Double.toString(c.getPrice());
    holder.price.setText(stringdouble);
    holder.quantity.setText(String.valueOf(c.getProductQuantity()));
    holder.totalPrice.setText(c.getTotalPrice());
    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          long productId = getItemId(position);
            //holder.button.setTag(position);
            try {
                ProductHelper productHelper = new ProductHelper();
                productHelper.setProductId(productId);
                ObjectMapper objectMapper = new ObjectMapper();
                String req = objectMapper.writeValueAsString(productHelper);

                requestObject = ExceptionRequest.generateRequest(req);
                requestObject.setUrl(URLL);
                new RemovefromList(position).execute(requestObject);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    });



    return row;
}

static class CartHolder {
    ImageView myImage;

    TextView title;

   //TextView descriptions;

     TextView price;

    TextView quantity;

    TextView totalPrice;

    Button button;


    public CartHolder(View v){
        myImage =(ImageView)v.findViewById(R.id.imageView2);

        title =(TextView)v.findViewById(R.id.carttitle);

       // descriptions =(TextView)v.findViewById(R.id.product_description);

        price =(TextView)v.findViewById(R.id.cart_price);

        quantity =(TextView)v.findViewById(R.id.item_quantity);

        totalPrice =(TextView)v.findViewById(R.id.sub_total);

        button =(Button)v.findViewById(R.id.remove_cart);



    }

}


private class RemovefromList extends AsyncTask<RequestObject, Void,JSONObject> {
int selectedPos;
public RemovefromList(int pos){
selectedPos = pos;
}

    @Override
    protected JSONObject doInBackground(RequestObject... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        // RequestObject requestObject = new RequestObject();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(arg0[0], ServiceHandler.POST);

        JSONObject products = new JSONObject();

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {

            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                products = jsonObj.getJSONObject("rsBody");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return products;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        try {
            if (result!=null){
                String status = result.getString("status");


                cartList.remove(selectedPos);
                notifyDataSetChanged();
                Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return;

    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

2

You send the position also in asynctask parameter

 new RemovefromList().execute(requestObject,position);

private class RemovefromList extends AsyncTask<String, Void,JSONObject> {

public RemovefromList(RequestObject obj, int pos) {
        super();
        RequestObject robj = obj;
        int position= pos;
    }
    @Override
    protected JSONObject doInBackground(String... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        // RequestObject requestObject = new RequestObject();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(robj, ServiceHandler.POST);

        JSONObject products = new JSONObject();

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {

            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                products = jsonObj.getJSONObject("rsBody");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return products;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        try {
            if (result!=null){
                String status = result.getString("status");


                cartList.remove(position);
                notifyDataSetChanged();
                Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return;

    }
}

Comments

0

Remove that particular item from ur List list and then call adapter.notifyDataSetChanged();

3 Comments

but problem is that inside postExecute() method i couldn't get position of item selected..
Then pass that parameter to your asyncTask.
yes in that case u should pass position as parameter to ur asyntask
0

// For Delete from list

new AsynceTaskDeleteData().execute(position);
listData.remove(position);

// In your AsyncTask

   private class AsynceTaskDeleteData extends AsyncTask<Integer, String, String> {

            int position;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected String doInBackground(Integer... arg0) {
                // Creating service handler class instance
                position = arg0[0];
                // Now pass position for delete

            }

            @Override
            protected void onPostExecute(String result) {

            notifyDataSetChanged();

            }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.