2017-09-25 55 views
0

我有名片卡。每張卡都有選項:編輯,刪除卡,信息和默認卡。當添加第一張卡時,該卡自動將卡設置爲邊框綠色。當我們有更多的卡片時,我們可以選擇卡片。但是我選擇例如第二張卡片和下一個退出我的應用程序時遇到問題。當我拒絕我的應用程序並去我的名單卡被選中第一張卡,但我選擇第二張卡。如何保存所選位置卡的位置,然後讀取所選位置卡。保存並在RecycleView中讀取選定的卡位置

List Card

這CardAdapter.class

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

    private int lastCheckedPos = 0; 
    private Context mContext; 
    private List<Card> cardsList = new ArrayList<>(); 
    private MySharepreference mySharepreference; 

    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_card, parent, false); 
     return new CardViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(CardViewHolder holder, final int position) { 
     final Card card = cardsList.get(position); 
     databaseHandler = new CardDatabaseHandler(mContext); 
     mySharepreference = new MySharepreference(); 

     holder.nameCard.setText(card.getNumberCard()); 
     holder.setCurrentDateText.setText(card.getCreateDate()); 
     holder.setExpirationDateText.setText(card.getExpirationDate()); 

     if (lastCheckedPos == position) { 
      mySharepreference.saveNumberCard(mContext, card.getId()); 

      holder.cardView.setBackgroundResource(R.drawable.bordercardview); 
      holder.menu.setOnClickListener(new View.OnClickListener() { 

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

       @Override 
       public void onClick(View v) { 
        PopupMenu popupMenu = new PopupMenu(mContext, v); 
        popupMenu.inflate(R.menu.cardmenudefault); 
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
         @Override 
         public boolean onMenuItemClick(MenuItem item) { 
          switch (item.getItemId()) { 
           case R.id.editCardD: 

            break; 
           case R.id.deleteCardD: 

            break; 
           case R.id.informationD: 

            break; 
          } 
          return false; 
         } 
        }); 
        popupMenu.show(); 
       } 
      }); 
     } else { 
      holder.cardView.setBackgroundResource(R.drawable.bordercardviewwhite); 
      holder.menu.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        PopupMenu popupMenu = new PopupMenu(mContext, v); 
        popupMenu.inflate(R.menu.cardmenu); 
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
         @Override 
         public boolean onMenuItemClick(MenuItem item) { 
          switch (item.getItemId()) { 
           case R.id.editCard: 

            break; 
           case R.id.deleteCard: 

            break; 
           case R.id.informationAboutCard: 

            break; 
           case R.id.defaultCard: 

            lastCheckedPos = position; 
            notifyDataSetChanged(); 

            break; 
          } 
          return false; 
         } 
        }); 
        popupMenu.show(); 
       } 
      }); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return cardsList.size(); 
    } 
} 

回答

-1

商店使用onSaveInstanceState()回調方法,您選擇的紙牌的位置。

@Override 
public void onSaveInstanceState(Bundle bundle) { 
    super.onSaveInstanceState(bundle); 
    bundle.putInt("CARD_POSITION", 12);    
} 

而且在onCreate()添加以下代碼

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.your_layout); 

    if (savedInstanceState != null) { 
     index = savedInstanceState.getString("CARD_POSITION");    
    } 
} 

現在你可以指定這個索引你的recyclerview。

僅供參考,請看this link

+0

儘管此鏈接可能會回答問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/17439008) –

+0

在我的回答中添加了必需的代碼 – user320676

相關問題