在BaseAdapter类中不工作notifyDataSetChanged()

时间:2016-04-21 06:45:26

标签: android

这是我设置列表项的代码库适配器。我在notifyDataSetChanged()中的代码不起作用。删除Btn点击删除listview项目如何更改工作此代码请帮帮我

public class CustomListviewMyCartitem extends BaseAdapter {

    ArrayList<Mycartitem> myList = new ArrayList<Mycartitem>();
    LayoutInflater inflater;
    Context context;
    int loader = R.drawable.loader;
    static int minteger = 0;
    public String Response_code;
    Mycartitem currentListData;
    String cid, qcount;
    UserSessionManager session;
    String rem, b, userid, btntag;

    public CustomListviewMyCartitem(Context context, ArrayList<Mycartitem> list) {

        this.myList = list;
        this.context = context;
        inflater = LayoutInflater.from(context);

        session = new UserSessionManager(context);
        HashMap<String, String> user = session.getUserDetails();
        userid = user.get(UserSessionManager.KEY_ID);

        Log.e("", "@myList" + myList);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

    /*
     * public void updateReceiptsList() { myList.clear(); this.myList = list;
     * this.notifyDataSetChanged(); }
     */
    public void updateResults(ArrayList<Mycartitem> results) {
        this.myList = results;
        // Triggers the list update
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        final MyViewHolder mViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.customcartlist, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        currentListData = myList.get(position);

        mViewHolder.txtproductnamecart.setText(currentListData.getCategoryName());
        mViewHolder.txtprizecart.setText("$" + currentListData.getCharge());
        mViewHolder.txttotalitemcart.setText(currentListData.getQuantity());

        mViewHolder.delete.setTag(currentListData.getCartItemId());

        mViewHolder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btntag = mViewHolder.delete.getTag() + "";
                new DeleteItem().execute();

            }
        });

        return convertView;
    }

    private class MyViewHolder {
        TextView txtproductnamecart, txtprizecart, txttotalitemcart;
        Button delete;

        public MyViewHolder(View item) {
            txtproductnamecart = (TextView) item.findViewById(R.id.txtproductnamecart);
            txtprizecart = (TextView) item.findViewById(R.id.txtprizecart);
            txttotalitemcart = (TextView) item.findViewById(R.id.txttotalitemcart);
            delete = (Button) item.findViewById(R.id.btndelete);
        }
    }

    private class DeleteItem extends AsyncTask<String, Void, Void> {

        protected void onPreExecute() {
            // Utils.Pdialog(Cart.this);
        }

        // Call after onPreExecute method
        protected Void doInBackground(String... urls) {
            request();
            return null;
        }

        protected void onPostExecute(Void unused) {
            Utils.Pdialog_dismiss();
            try {

                JSONObject jsonObject;
                jsonObject = new JSONObject(Response_code);
                String msg = jsonObject.getString("status");

                if (msg.equals("200")) {

                    JSONObject jsonArray = jsonObject.getJSONObject("payload");

                    updateResults(myList);

                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                Toast.makeText(context, "No item Found", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }
    }

    private void request() {
        try {

            List<NameValuePair> values = new ArrayList<NameValuePair>(2);

            values.add(new BasicNameValuePair("userId", userid));
            values.add(new BasicNameValuePair("cartItemId", btntag));
            values.add(new BasicNameValuePair("action", "singleItem"));
            values.add(new BasicNameValuePair("cartId", Utils.CARTID));

            Log.e("", "@Parameteruserid" + userid);
            Log.e("", "@ParametercartItemId" + currentListData.getCartItemId());
            Log.e("", "@Parameteaction" + "singleItem");
            Log.e("", "@ParametercartId" + Utils.CARTID);

            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
            HttpConnectionParams.setSoTimeout(httpParams, 10000);
            HttpClient client = new DefaultHttpClient(httpParams);

            String url = Utils.link + "index.php/api/deletecartitem/";

            HttpPost httppost = new HttpPost(url);

            httppost.setEntity(new UrlEncodedFormEntity(values));

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            Response_code = client.execute(httppost, responseHandler);
            Log.e("Cart item delete Response :---", "CratItemDelete" + Response_code);

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

而不是方法:

public void updateResults(ArrayList<Mycartitem> results) {
    this.myList = results;
    // Triggers the list update
    this.notifyDataSetChanged();
}

使用以下方法:

public void updateResults(ArrayList<Mycartitem> results) {
    this.myList.clear();
    this.myList.addAll(results);
    // Triggers the list update
    this.notifyDataSetChanged();
}

答案 1 :(得分:0)

您没有更新myList变量。清除列表中的所有项目并使用新项目进行更新,然后调用notifyDatasetchange。