2016-10-02 6 views
0

EDIT:SwipeLayoutクラスを置き換えて解決しました。 https://github.com/daimajia/AndroidSwipeLayout/issues/17BaseSwipeAdapterからアイテムを削除すると、最後のアイテムが常に削除されます

「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を保持します。あなたが何らかのアクションを実行する場合、それは最後のアイテムに実行されます。これを克服するには、データをリストにロードする場所と同じ場所に削除を設定する必要があります.getItemID()にviewを使用する必要があります。これがアダプタの動作です。今、あなたは編集をしなければならないでしょう。

+0

私はそれを解決することができました。それはスワイプのlibからの問題だった。とにかくおかげでhttps://github.com/daimajia/AndroidSwipeLayout/issues/17! – Daniell

関連する問題