0
如何在應用程序重新啓動時使用回收視圖中所選卡片項目開始活動。開始活動,在回收視圖中選擇位置項目
詳情:
我有三個活動:閃屏,卡冊和卡詳細。
卡冊活動包含的卡列表。列表中的每個單項都有菜單。在菜單中有一個選項是「設置默認卡」。
卡詳細信息此活動顯示卡的詳細信息。
問題:
現在,如果我選擇設置默認卡,選擇卡包含綠色邊框。
當我設置默認卡並退出我的應用程序時,所以接下來如果我打開我的應用程序,那麼它應該使用選定卡進行卡詳細信息活動。
注:我使用數據庫來存儲卡的詳細信息和默認卡
我的代碼:
CardAdapter.class
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private static int lastCheckedPos = 0;
private Context mContext;
private ArrayList<Card> cardsList;
boolean isError;
public CardAdapter(Context mContext, ArrayList<Card> cardsList, String key) {
this.key = key;
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 DatabaseHandler(mContext);
[enter image description here][1]
//ustawienie zaznaczenia na wybranej pozycji
if (position == lastCheckedPos) {
holder.cardView.setBackgroundResource(R.drawable.bordercardview);
} else {
holder.cardView.setCardBackgroundColor(Color.WHITE);
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.cardmenu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.defaultCard:
int prePos = lastCheckedPos;
lastCheckedPos = position;
notifyItemChanged(prePos);
notifyItemChanged(lastCheckedPos);
break;
}
return false;
}
});
popupMenu.show();
}
});
}
}
@Override
public int getItemCount() {
return cardsList.size();
}
}
將您的lastCheckedPos存儲到SharedPreferences並使用它來填充適配器 – anonymous