Listview项目删除最后一项ID

时间:2016-02-06 05:55:28

标签: android listview

logcat的:

02-06 00:43:21.981: E/parkId(4374): 120
02-06 00:43:21.982: E/parkId(4374): 122
02-06 00:43:21.984: E/parkId(4374): 146
02-06 00:43:21.986: E/parkId(4374): 148
02-06 00:43:21.990: E/parkId(4374): 149
02-06 00:43:21.992: E/parkId(4374): 150
02-06 00:43:21.994: E/parkId(4374): 151
02-06 00:43:21.996: E/parkId(4374): 152
02-06 00:43:39.137: E/FinalparkId(4374): 122
02-06 00:43:40.070: E/response(4374): {"Error":false,"message":"The parking spot has been deleted.","data":[]}
02-06 00:43:40.078: D/ResponseParkId(4374): 152

MainActivity.java

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

    @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;
    }
    @Override
    public View getView(final int position, View convertView,
        ViewGroup parent) {
    View view = convertView;

    if (convertView == null) {
        holder = new ViewHolder();
        view = mInlfater.inflate(R.layout.myspot_list_item, null);

        holder.favTitleTv = (TextView) view
                .findViewById(R.id.fav_list_titletv);
        holder.favTitle2Tv = (TextView) view
                .findViewById(R.id.fav_list_title1tv);
        holder.rating_reviews = (RatingBar) view
                .findViewById(R.id.ratingBar);
        holder.spot_image = (ImageView) view
                .findViewById(R.id.spot_list_iv);

        holder.favSpaceTv = (TextView) view
                .findViewById(R.id.fav_list_spaceTv);
        holder.favTypeTv = (TextView) view
                .findViewById(R.id.fav_parking_type);

        Button delButton = (Button) view.findViewById(R.id.delButton);

        final MySpotsMenuModel modelObj = spotlist.get(position);



        delButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                spotIDforVol = modelObj.getParking_spot_id();


                hitDeleteListResponse();


                spotlist.remove(position);


                adapter.notifyDataSetChanged();
                Toast.makeText(MySpotsMenuActivity.this,
                        "Item deleted", Toast.LENGTH_SHORT).show();

                finish();
                startActivity(getIntent());

            }

            private void hitDeleteListResponse() {
                final ProgressDialog dialog = new ProgressDialog(
                        MySpotsMenuActivity.this);
                dialog.setMessage("Loading...");
                dialog.setCancelable(false);
                dialog.show();




          String url = OS_MYSPOTS_DELETE_URL(spotIDforVol, getPassword(getApplicationContext()));

                        Log.e("FinalparkId", spotIDforVol);

                StringRequest request = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>() {

                            @Override
                            public void onResponse(String response) {
                                if (response != null) {
                                    Log.e("response", response);
                                    dialog.dismiss();

                                    try {

                                        JSONObject search_obj = new JSONObject(response);
                                        if (search_obj.getString("Error").equals("false")) {


                                            Log.d("ResponseParkId",""+parkId);

                                            toastShort(MySpotsMenuActivity.this,
                                                    search_obj.getString("message"));
                                        }
                                            else {

                                                toastShort(MySpotsMenuActivity.this,
                                                        "null");
                                            }

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

                                    } else {
                                        dialog.dismiss();
                                    }
                                }
                            }, new Response.ErrorListener() {

                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    if (error != null) {
                                        Log.e("error", error.toString());
                                        dialog.dismiss();
                                    }

                                }
                            });

                    RequestQueue queue = Volley.newRequestQueue(MySpotsMenuActivity.this);
                    queue.add(request);
                    queue.getCache().remove(url);

            }
        });



        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.favTitleTv.setText(spotlist.get(position)
            .getParking_spot_title());
    // holder.favTitle2Tv
    // .setText(list.get(position).getParking_desc());
    holder.favTitle2Tv.setText(spotlist.get(position)
            .getParking_spot_note());
    holder.favSpaceTv.setText(spotlist.get(position)
            .getParking_spot_space());
    holder.favTypeTv.setText(spotlist.get(position)
            .getParking_spot_type());

    imageLoader.DisplayImage(spotlist.get(position).getImage(),
            holder.spot_image);

    return view;
}

class ViewHolder {
        TextView favAddBtn, favRemoveBtn;
        TextView favTitleTv, favTitle2Tv, favAddrTv, favSpaceTv, favTypeTv;
        RatingBar rating_reviews;
        ImageView spot_image;
    }

我正在点击delete的响应。我得到了响应成功消息。我唯一的问题是,前四个listview项删除了相应的id。但是如果我向下滚动并删除列表项意味着它需要最后的回复id.Anyone可以帮助我。

2 个答案:

答案 0 :(得分:0)

请使用以下代码替换您的代码并尝试删除该位置。因为您选择的ID位置错误。请尝试使用此代码。

if (convertView == null) {
    holder = new ViewHolder();
    view = mInlfater.inflate(R.layout.myspot_list_item, null);

    holder.favTitleTv = (TextView) view
            .findViewById(R.id.fav_list_titletv);
    holder.favTitle2Tv = (TextView) view
            .findViewById(R.id.fav_list_title1tv);
    holder.rating_reviews = (RatingBar) view
            .findViewById(R.id.ratingBar);
    holder.spot_image = (ImageView) view
            .findViewById(R.id.spot_list_iv);

    holder.favSpaceTv = (TextView) view
            .findViewById(R.id.fav_list_spaceTv);
    holder.favTypeTv = (TextView) view
            .findViewById(R.id.fav_parking_type);

    Button delButton = (Button) view.findViewById(R.id.delButton);

    final MySpotsMenuModel modelObj = spotlist.get(position);



    delButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            spotIDforVol = spotlist.get(position).getParking_spot_id();

            hitDeleteListResponse();
        }

        private void hitDeleteListResponse() {
            final ProgressDialog dialog = new ProgressDialog(
                    MySpotsMenuActivity.this);
            dialog.setMessage("Loading...");
            dialog.setCancelable(false);
            dialog.show();

      String url = OS_MYSPOTS_DELETE_URL(spotIDforVol, getPassword(getApplicationContext()));

                    Log.e("FinalparkId", spotIDforVol);

            StringRequest request = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            if (response != null) {
                                Log.e("response", response);
                                dialog.dismiss();

                                try {

                                    JSONObject search_obj = new JSONObject(response);
                                    if (search_obj.getString("Error").equals("false")) {


                                        Log.d("ResponseParkId",""+parkId);

                                        toastShort(MySpotsMenuActivity.this,                                                   search_obj.getString("message"));
spotlist.remove(position);


        adapter.notifyDataSetChanged();
        Toast.makeText(MySpotsMenuActivity.this,
                "Item deleted", Toast.LENGTH_SHORT).show();

        finish();
        startActivity(getIntent());
                                    }
                                        else {
                                         toastShort(MySpotsMenuActivity.this,
                                                    "null");
                                        }

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

                                } else {
                                    dialog.dismiss();
                                }
                            }
                        }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                if (error != null) {
                                    Log.e("error", error.toString());
                                    dialog.dismiss();
                                }

                            }
                        });

                RequestQueue queue = Volley.newRequestQueue(MySpotsMenuActivity.this);
                queue.add(request);
                queue.getCache().remove(url);

        }
    });



    view.setTag(holder);
} else {
    holder = (ViewHolder) view.getTag();
}

holder.favTitleTv.setText(spotlist.get(position)
        .getParking_spot_title());
// holder.favTitle2Tv
// .setText(list.get(position).getParking_desc());
holder.favTitle2Tv.setText(spotlist.get(position)
        .getParking_spot_note());
holder.favSpaceTv.setText(spotlist.get(position)
        .getParking_spot_space());
holder.favTypeTv.setText(spotlist.get(position)
        .getParking_spot_type());

imageLoader.DisplayImage(spotlist.get(position).getImage(),
        holder.spot_image);

return view;
}

class ViewHolder {
    TextView favAddBtn, favRemoveBtn;
    TextView favTitleTv, favTitle2Tv, favAddrTv, favSpaceTv, favTypeTv;
    RatingBar rating_reviews;
    ImageView spot_image;
}

答案 1 :(得分:0)

使用这两种方法从arraylist中获取所选项目的位置。

将此代码放在getView方法

之后
pull

此处优惠 BEAN课程。您可以在此处使用您的班级名称