2017-05-12 15 views
0

名前カードを保存する際に問題があります。 PopupMenuをクリックし、名前をカードに変更するオプションを選択します。変更ダイアログを表示します。名前を編集するときはこのダイアログ名を、カードに保存をクリックします。アプリケーションが機能しなくなります。名前カードをリサイクル表示に更新できません

私のエラーログ

java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1 
    at java.util.ArrayList.set(ArrayList.java:483) 
    at com.xxxxx.smrtkarta.adapter.CardAdapter$1$1$2.onClick(CardAdapter.java:112) 
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

私のクラスCardAdapter

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> { 

    private Card card; 
    private Context mContext; 
    private ArrayList<Card> cardslist = new ArrayList<Card>(); 
    private SharedPreferences sharedPreferences; 

    public CardAdapter(Context mContext, ArrayList<Card> cardslist) { 

     this.mContext = mContext; 
     this.cardslist = cardslist; 
     notifyDataSetChanged(); 

    } 


    @Override 
    public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cardview, parent, false); 
     return new CardViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(final CardViewHolder holder, final int position) { 
     card = cardslist.get(position); 

     holder.nameCard.setText(card.getNameCard()); 
     holder.setDate.setText(card.getCreateDate()); 
     holder.expirationDate.setText(card.getExpirationDate()); 
     holder.menu.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       final PopupMenu popupMenu = new PopupMenu(mContext, holder.menu); 
       popupMenu.inflate(R.menu.cardmenu); 
       popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
        @Override 
        public boolean onMenuItemClick(MenuItem item) { 

         Typeface custom_fonts = Typeface.createFromAsset(mContext.getAssets(), "fonts/OpenSans-Regular.ttf"); 

         Typeface custom_fonts_Bold = Typeface.createFromAsset(mContext.getAssets(), "fonts/OpenSans-Bold.ttf"); 

         switch (item.getItemId()) { 
          case R.id.editCard: 

           // dialog który powoduje edycje karty 
           final AlertDialog.Builder alert = new AlertDialog.Builder(mContext); 

           final LayoutInflater inflater = LayoutInflater.from(mContext); 
           View alterView = inflater.inflate(R.layout.editcarddialog, null); 
           alert.setView(alterView); 

           TextView editTextView = (TextView) alterView.findViewById(R.id.editCardText); 
           editTextView.setTypeface(custom_fonts); 

           final EditText nameCardEditText = (EditText) alterView.findViewById(R.id.nameCardEditText); 
           nameCardEditText.setTypeface(custom_fonts); 
           nameCardEditText.setText(card.getNameCard()); 


           alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
             dialog.dismiss(); 
            } 
           }); 

           alert.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 

             final String nameCardString = nameCardEditText.getText().toString(); 


             int indexOf = cardslist.indexOf(nameCardString); 
             cardslist.set(indexOf, card); 
             notifyItemChanged(indexOf); 


             Toast toast = Toast.makeText(mContext, "Karta została zapisana ", Toast.LENGTH_LONG); 
             LinearLayout layout = (LinearLayout) toast.getView(); 
             if (layout.getChildCount() > 0) { 
              TextView tv = (TextView) layout.getChildAt(0); 
              tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 
             } 
             toast.show(); 
            } 
           }); 

           alert.create(); 
           alert.show(); 
           break; 
+0

をあなたは 'の文字列を探している

alert.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { final String newNameCardString = nameCardEditText.getText().toString(); int indexOf = cardslist.get(position).setNameCard(newNameCardString); cardslist.set(indexOf, card); notifyItemChanged(indexOf); } }); 

私のデータを修正'' ArrayListの中で '。インデックスを調べるには、リストをループし、各カードの 'card.getNameCard()'が 'nameCardString'と等しいかどうかを確認する必要があります。 – Titus

答えて

0

私はちょうどそれが動作します:)

関連する問題