如何在按钮点击(Android)上从列表视图中删除特定行

时间:2015-11-06 04:49:10

标签: android

我有自定义适配器类,在按钮单击时我们对服务器进行异步调用。因此,在onPostExecute()方法中,我想删除单击按钮的特定行。这是我的适配器类

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;

        }
    }



}

在onPostExecute()内部,如果我获得状态成功,这意味着在服务器端已删除特定产品。但在我们这边,我们仍然看到该产品。 所以我想删除那一行,并希望update计算一下cartitem。 任何帮助都会得到高级评估。

4 个答案:

答案 0 :(得分:2)

请检查

单击按钮时在异步任务构造函数中发送位置

Parse.Cloud.run('createGuestlistInviteForExistingUser', {
   phoneNumber: phoneNumber,
   guestlist: guestlist,
   guest: user
   }).then(function(guestlistInvite) {
      response.success(successfully created guestlistInvite');
 }); 

答案 1 :(得分:2)

您也可以在asynctask参数

中发送该位置
 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;

    }
}

答案 2 :(得分:0)

从你的列表列表中删除该特定项目,然后调用adapter.notifyDataSetChanged();

答案 3 :(得分:0)

//对于从列表中删除

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

//在你的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();

            }
   }