2016-10-02 150 views
0

編輯:通過替換SwipeLayout類來解決。 https://github.com/daimajia/AndroidSwipeLayout/issues/17從BaseSwipeAdapter刪除項目總是刪除最後一項

如果我從「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 
} 

回答

0

問題是,當您使用數據填充列表時,適配器將加載所有值以及ItemID分配給它的列表。最後,適配器保存在列表中加載的最後一項的itemId。所以如果您將執行任何操作,它將執行到最後一個項目。爲了克服這個問題,您需要在將數據加載到列表的相同位置設置刪除。您需要使用view來getItemID()。這是適配器的工作原理。現在您必須進行編輯。

+0

我已經設法解決它。這是一個來自刷卡庫的問題。 https://github.com/daimajia/AndroidSwipeLayout/issues/17謝謝! – Daniell