无法将名片更新为回收查看

时间:2017-05-12 07:21:07

标签: java android android-recyclerview

我有保存名片的问题。单击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;

1 个答案:

答案 0 :(得分: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);

                                }
                            });

它有效:)