从BaseSwipeAdapter中删除项目始终会删除最后一项

时间:2016-10-02 11:02:22

标签: java android list adapter

编辑:替换SwipeLayout类解决。 https://github.com/daimajia/AndroidSwipeLayout/issues/17

如果我从" rowItem"删除项目并调用" notifyDataSetChanged();"它将始终删除最后一项。位置和ID很好,因为我在数据库和日志中得到了适当的修改。 删除按钮位于滑动视图上,因此我无法编辑适配器外的列表。

我如何避免这个问题?

public class CalendarListArrayAdapter extends BaseSwipeAdapter {

private static final String TAG = CalendarListArrayAdapter.class.getSimpleName();
Context context;
List<Programare> rowItem;

CalendarListArrayAdapter(Context context, List<Programare> rowItem) {
    this.context = context;
    this.rowItem = rowItem;
}

@Override
public int getCount() {
    return rowItem.size();
}

@Override
public Object getItem(int position) {

    return rowItem.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public int getSwipeLayoutResourceId(int i) {
    return R.id.swipe;
}

@Override
public View generateView(final int position, final ViewGroup viewGroup) {
    final View v = LayoutInflater.from(context).inflate(R.layout.calendar_list_row, null);
    SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));

    TextView txtServ = (TextView) v.findViewById(R.id.service);
    TextView txtDate = (TextView) v.findViewById(R.id.date);

    final Programare row_pos = rowItem.get(position);

    String[] dateTokens = row_pos.getDate().split(" ",-1);
    txtServ.setText(row_pos.getServ());
    txtDate.setText(dateTokens[1]);

    swipeLayout.addSwipeListener(new SimpleSwipeListener() {
        @Override
        public void onOpen(SwipeLayout layout) {
            YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.timeIcon));
        }
    });
    v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /**
             * function to reschedule appointment
             * */
            // Tag used to cancel the request
            String tag_string_req = "req_reschedule";

            StringRequest strReq = new StringRequest(Request.Method.POST,
                    Config.URL_RESCHEDULE, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "Reschedule response: " + response);

                    try {
                        JSONObject jObj = new JSONObject(response);
                        boolean error = jObj.getBoolean("error");

                        // Check for error node in json
                        if (!error) {
                            Toast.makeText(context, "Reprogramat!", Toast.LENGTH_SHORT).show();
                            Log.d("position", String.valueOf(position));
                            Log.d("row_pos", String.valueOf(row_pos));
                            Log.d("row_item to remove", row_pos.getObjId());

                            row_pos.setConfirm(3);
                            rowItem.remove(position);
                            notifyDataSetChanged();
                            //rescheduleSMS(row_pos.getDate(),row_pos.getClientPhone());
                        } else {
                            // Error in login. Get the error message
                            String errorMsg = jObj.getString("error_msg");
                            Toast.makeText(context,
                                    errorMsg, Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        // JSON error
                        e.printStackTrace();
                        Toast.makeText(context, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                }
            }
// code
}

1 个答案:

答案 0 :(得分:0)

问题在于,当您使用数据填充列表时,适配器会将所有值加载到列表以及分配给它的ItemID。最后,适配器保存列表中加载的最后一项的itemId。因此,如果您将执行任何操作,将执行最后一项。要解决此问题,您需要在将数据加载到列表的同一位置设置删除。您需要使用view to getItemID()。这是适配器的工作方式。现在,您必须进行编辑。