// TODO: The real methods to update cards on Anki should be implemented instead of this public void updateAllCards() { Cursor cursor = AnkiDb.database.rawQuery("SELECT id, factId " + "FROM cards", null); while (cursor.moveToNext()) { // Get card Card card = new Card(); card.fromDB(cursor.getLong(0)); Log.i(TAG, "Card id = " + card.id); // Get the related fact Fact fact = card.getFact(); // Log.i(TAG, "Fact id = " + fact.id); // Generate the question and answer for this card and update it HashMap<String, String> newQA = CardModel.formatQA(fact, card.getCardModel()); card.question = newQA.get("question"); Log.i(TAG, "Question = " + card.question); card.answer = newQA.get("answer"); Log.i(TAG, "Answer = " + card.answer); card.modified = System.currentTimeMillis() / 1000.0; card.toDB(); } }
/** * Uses Reflection to create a new {@link AbstractCard} from the given {@link CardModel} by * copying fields. * * @param model The {@link CardModel} to "inflate" into an {@link AbstractCard} * @return An {@link AbstractCard} that matches the model data * @throws InstantiationException Thrown when the class specified by the model cannot be * instantiated (no default ctor). * @throws IllegalAccessException Thrown if I missed a setAccessible(true) somewhere, or if the * default ctor for the target class is not visible. */ public static AbstractCard createCard(CardModel model) throws InstantiationException, IllegalAccessException { /* * Instantiate a new object (must be AbstractCard or extend it, makes * sense for the CardsUI mechanic) from a Class descriptor. */ AbstractCard newCard = model.cardClass.newInstance(); Log.i("CardFactory", "Creating a new card! We're making a new " + model.cardClass.getName()); /* * We will also need the Class descriptor for the object we just created * in order to access its fields. */ Class<? extends AbstractCard> newCardClazz = newCard.getClass(); /* * Java objects do not flatten their hierarchy at runtime (that's good), * which means that we actually need to collect all fields from all the * ancestors. Here, we collect them into a HashMap for convenience. */ HashMap<String, Field> destinationFields = new HashMap<String, Field>(); Class<?> clazzUnderInspection = newCardClazz; while (clazzUnderInspection != null) { /* * Get all fields for the current point in the hierarchy, and * collect them into the HashMap. */ Field[] clazzFields = clazzUnderInspection.getDeclaredFields(); for (int i = 0; i < clazzFields.length; i++) { Field f = clazzFields[i]; destinationFields.put(f.getName(), f); } /* * Okay, now examine the ancestor (for java.lang.Object it is null, * which means the loop will exit). */ clazzUnderInspection = clazzUnderInspection.getSuperclass(); } /* * Obtain a list of fields within the model. As most of them match those * in AbstractCard, the card's content will be preserved. Since the * model inherits only from Object, all fields collected here will be * what we need. */ Field[] sourceFields = model.getClass().getDeclaredFields(); /* * Now iterate over the fields in the model. */ for (int i = 0; i < sourceFields.length; i++) { // Just a reference for the field we're copying in this pass Field curField = sourceFields[i]; /* * This is to prevent IllegalAccessExceptions when accessing the * field. Yes, this violates the visibility set by the field * declarations, but that's the whole point of this routine. */ curField.setAccessible(true); String fieldName = curField.getName(); Log.d("CardFactory", " > Now copying field: " + fieldName); Field destField = destinationFields.get(fieldName); if (destField != null) { /* * We need to get the specific field that matches the one in the * model and set it to the same value as the corresponding field * in the model. */ destField.setAccessible(true); // "Trust me." destField.set(newCard, curField.get(model)); Log.d( "CardFactory", String.format( " > Field %s (= %s) -> Field %s (=%s)", curField, curField.get(model), destField.getName(), destField.get(newCard))); } else { /* * We have encountered a field (CardModel.data, * CardModel.cardClass, ...) in the model that does not exist in * the view that can represent it, so we just skip it. Yes, this * breaks the "for loop is fixed" convention, but this really is * the easiest way. * * "Go to Next Iteration. Go directly to Next Iteration. Do not * pass Go. Do not collect $200." */ Log.d( "CardFactory", String.format( " > Skipping over an unmapped Field %s/%s (= %s)", model.cardClass.getName(), curField, curField.get(model))); } } return newCard; }
public void addCardModel(CardModel card) { card.model = this; this.cardModels.add(card); // this.toDB(); }
public List<CardModel> getList() { List<Integer> cardList = new ArrayList<>(); cardList.add(R.drawable.photo1); cardList.add(R.drawable.photo2); cardList.add(R.drawable.photo3); List<Integer> cardList2 = new ArrayList<>(); cardList2.add(R.drawable.photo4); cardList2.add(R.drawable.photo5); cardList2.add(R.drawable.photo6); List<Integer> cardList3 = new ArrayList<>(); cardList3.add(R.drawable.photo7); cardList3.add(R.drawable.photo8); cardList3.add(R.drawable.photo9); List<CardModel> mItems = new ArrayList<>(); CardModel cardModel = new CardModel(); cardModel.setMode(0); cardModel.setImageIdList(cardList); // mItems.add(cardModel); cardModel = new CardModel(); cardModel.setMode(0); cardModel.setImageIdList(cardList2); // mItems.add(cardModel); cardModel = new CardModel(); cardModel.setMode(0); cardModel.setImageIdList(cardList3); // mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo1); cardModel.setName( "http://marinsharetheroad.org/wp-content/uploads/2015/06/nature-slide-background-tgk1xo4c.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo5); cardModel.setName( "http://www.sixer.biz/wallpaper/nature/nature-wallpapers-for-apple-iphone-nature-wallpapers-wallpaper-vs-nurture-images-quotes-made-journal-jobs-republic.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo6); cardModel.setName("http://notenoughgood.com/wp-content/uploads/2011/11/verticalfarm.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo4); cardModel.setName( "https://image.freepik.com/free-vector/hello-spring-background_23-2147507394.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo1); cardModel.setName( "http://marinsharetheroad.org/wp-content/uploads/2015/06/nature-slide-background-tgk1xo4c.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo5); cardModel.setName( "http://www.sixer.biz/wallpaper/nature/nature-wallpapers-for-apple-iphone-nature-wallpapers-wallpaper-vs-nurture-images-quotes-made-journal-jobs-republic.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo6); cardModel.setName("http://notenoughgood.com/wp-content/uploads/2011/11/verticalfarm.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo4); cardModel.setName( "https://image.freepik.com/free-vector/hello-spring-background_23-2147507394.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo1); cardModel.setName( "http://marinsharetheroad.org/wp-content/uploads/2015/06/nature-slide-background-tgk1xo4c.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo5); cardModel.setName( "http://www.sixer.biz/wallpaper/nature/nature-wallpapers-for-apple-iphone-nature-wallpapers-wallpaper-vs-nurture-images-quotes-made-journal-jobs-republic.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo6); cardModel.setName("http://notenoughgood.com/wp-content/uploads/2011/11/verticalfarm.jpg"); cardModel.setMode(1); mItems.add(cardModel); cardModel = new CardModel(); cardModel.setImageId(R.drawable.photo4); cardModel.setName( "https://image.freepik.com/free-vector/hello-spring-background_23-2147507394.jpg"); cardModel.setMode(1); mItems.add(cardModel); return mItems; }