制作Final Android Studio

问题描述 投票:-1回答:1

[我正在在线观看视频,但是我坚持这样做,无法将变量定为final。此代码正在android studio中完成。

错误:找不到符号变量i该错误可以在最后5行找到。

intent.putExtra(Common.KEY_TIME_SLOT,i);

我唯一的选择是:创建局部变量建立栏位创建参数重命名参考

这里有完整的代码:

@ Override

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.txt_time_slot.setText(new StringBuilder(Common.convertTimeSlotToString(position)).toString());
    if(timeSlotList.size()==0)//if all is available, show list
    {
        holder.card_time_slot.setCardBackgroundColor(context.getResources().getColor(android.R.color.white));
        holder.txt_time_slot_description.setText("Available");
        holder.txt_time_slot_description.setTextColor(context.getResources().getColor(android.R.color.black));
        holder.txt_time_slot.setTextColor(context.getResources().getColor(android.R.color.black));
    }
    else //if fully booked
    {
        for(TimeSlot slotValue:timeSlotList)
        {
            //loop all time slot from sever and set different color
            int slot = Integer.parseInt(slotValue.getSlot().toString());
            if(slot==position) // if slot == position
            {
                holder.card_time_slot.setTag(Common.DISABLE_TAG);
                holder.card_time_slot.setCardBackgroundColor(context.getResources().getColor(android.R.color.darker_gray));
                holder.txt_time_slot_description.setText("BOOKED");
                holder.txt_time_slot_description.setTextColor(context.getResources()
                        .getColor(android.R.color.white));
                holder.txt_time_slot.setTextColor(context.getResources().getColor(android.R.color.white));

            }
        }
    }

    //add available time slot
    if (!cardViewList.contains(holder.card_time_slot))
        cardViewList.add(holder.card_time_slot);
    //check if slot is available
    holder.setiRecyclerItemSelectedListener(new IRecyclerItemSelectedListener() {
        @Override
        public void onItemSelectedListener(View view, int pos) {
            //loop all slots
            for(CardView cardView:cardViewList)
            {
                if (cardView.getTag() == null)
                    cardView.setCardBackgroundColor(context.getResources()
                            .getColor(android.R.color.white));
            }
            //selected slot will change color
            holder.card_time_slot.setCardBackgroundColor(context.getResources()
            .getColor(android.R.color.holo_orange_dark));

            //once selected, send signal to next button
            Intent intent = new Intent(Common.KEY_ENABLE_BUTTON_NEXT);
            intent.putExtra(Common.KEY_TIME_SLOT, i); // error. in the video it shows that i can make "i" final but on my end it does not show that option.
            intent.putExtra(Common.KEY_STEP,3); //going step 3
            localBroadcastManager.sendBroadcast(intent);
        }
    });
java android final
1个回答
0
投票

问题不在于它不是最终的(您可以通过在其前面输入单词final来使变量成为最终的)。问题在于它根本不存在。您在任何地方都没有名为i的变量。]​​>

我可能应该是pos,是实际选择的项目。

© www.soinside.com 2019 - 2024. All rights reserved.