android 查找最近使用的列表元素的索引Java

bkhjykvo  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(148)

我正在构建一个食谱应用程序。我创建了一个食谱数据库,其中的信息是使用ArrayList设置的。我的想法是创建一个“Recent Recipes”部分,其中我之前创建的模板被替换为食谱本身的按钮。问题:如何找到之前使用过的三个配方的索引?
我的数据库:`

public class ReceiptsBase {

    ArrayList<ArrayList<Integer>> recipesAll = new ArrayList<>();

    public ReceiptsBase() {
        ArrayList<Integer> mushroom_soup = new ArrayList<>();
        mushroom_soup.add (R.drawable.mush);
        mushroom_soup.add (R.string.mush_name);
        mushroom_soup.add (R.string.mush_info);
        mushroom_soup.add (R.string.mush_prep);
        mushroom_soup.add(R.drawable.mush_blur);
        mushroom_soup.add(R.string.mush_ingr);

        ArrayList<Integer> tomato_soup = new ArrayList<>();
        tomato_soup.add (R.drawable.tomato);
        tomato_soup.add (R.string.tomato_name);
        tomato_soup.add (R.string.tomato_info);
        tomato_soup.add (R.string.tomato_prep);
        tomato_soup.add(R.drawable.tomato_blur);
        tomato_soup.add(R.string.tomato_ingr);

        ArrayList<Integer> potato_soup = new ArrayList<>();
        potato_soup.add (R.drawable.potatosoup);
        potato_soup.add (R.string.potsoup_name);
        potato_soup.add (R.string.potsoup_info);
        potato_soup.add (R.string.potsoup_prep);
        potato_soup.add(R.drawable.potatosoup_blur);
        potato_soup.add(R.string.potsoup_ingr);

        ArrayList<Integer> onion_meal = new ArrayList<>();
        onion_meal.add (R.drawable.onion);
        onion_meal.add (R.string.onion_name);
        onion_meal.add (R.string.onion_info);
        onion_meal.add (R.string.onion_prep);
        onion_meal.add(R.drawable.onion_blur);
        onion_meal.add(R.string.onion_ingr);

        ArrayList<Integer> pancakes = new ArrayList<>();
        pancakes.add (R.drawable.pancakes);
        pancakes.add (R.string.pank_name);
        pancakes.add (R.string.pank_info);
        pancakes.add (R.string.pank_prep);
        pancakes.add(R.drawable.pancakes_blur);
        pancakes.add(R.string.pank_ingr);

        ArrayList<Integer> dumplings = new ArrayList<>();
        dumplings.add (R.drawable.dumpl);
        dumplings.add (R.string.dumpl_name);
        dumplings.add (R.string.dumpl_info);
        dumplings.add (R.string.dumpl_prep);
        dumplings.add(R.drawable.dumpl_blur);
        dumplings.add(R.string.dumpl_ingr);

        ArrayList<Integer> croquettes = new ArrayList<>();
        croquettes.add (R.drawable.croquettes);
        croquettes.add (R.string.croq_name);
        croquettes.add (R.string.croq_info);
        croquettes.add (R.string.croq_prep);
        croquettes.add(R.drawable.croquettes_blur);
        croquettes.add(R.string.croq_ingr);

        ArrayList<Integer> katsudon = new ArrayList<>();
        katsudon.add (R.drawable.katsudon);
        katsudon.add (R.string.kats_name);
        katsudon.add (R.string.kats_info);
        katsudon.add (R.string.kats_prep);
        katsudon.add(R.drawable.katsudon_blur);
        katsudon.add(R.string.kats_ingr);

        ArrayList<Integer> garlic_bread = new ArrayList<>();
        garlic_bread.add (R.drawable.garlic);
        garlic_bread.add (R.string.garl_name);
        garlic_bread.add (R.string.garl_info);
        garlic_bread.add (R.string.garl_prop);
        garlic_bread.add(R.drawable.garlic_blur);
        garlic_bread.add(R.string.garlic_ingr);

        ArrayList<Integer> potato_pancakes = new ArrayList<>();
        potato_pancakes.add (R.drawable.pot_pank);
        potato_pancakes.add (R.string.potpank_name);
        potato_pancakes.add (R.string.potpank_info);
        potato_pancakes.add (R.string.potpank_prep);
        potato_pancakes.add(R.drawable.pot_pank_blur);
        potato_pancakes.add(R.string.potpank_ingr);

        recipesAll.add(mushroom_soup);
        recipesAll.add(tomato_soup);
        recipesAll.add(potato_soup);
        recipesAll.add(onion_meal);
        recipesAll.add(pancakes);
        recipesAll.add(dumplings);
        recipesAll.add(croquettes);
        recipesAll.add(katsudon);
        recipesAll.add(garlic_bread);
        recipesAll.add(potato_pancakes);
    }

    public ArrayList<Integer> getReceipt(int receiptIndex) {
        return recipesAll.get(receiptIndex);
    }

    public ArrayList<ArrayList<Integer>> getReceipts() {
        return recipesAll;
    }

`
The section
我的代码:

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

        Button button_recipes = findViewById(R.id.button2);
        button_recipes.setOnClickListener(view -> openRecipes());

        Button button_search = findViewById(R.id.button3);
        button_search.setOnClickListener(view -> openSearch());

        Button button_supriseme = findViewById(R.id.button4);
        button_supriseme.setOnClickListener(view -> openSupriseMe());

        ImageButton recent1 = findViewById(R.id.rec1);
        ImageButton recent2 = findViewById(R.id.rec2);
        ImageButton recent3 = findViewById(R.id.rec3);

//        recent1.setImageDrawable(getDrawable());
//        recent2.setImageDrawable(getDrawable());
//        recent3.setImageDrawable(getDrawable());
//
//        recent1.setOnClickListener(view -> openRecipe());
//        recent2.setOnClickListener(view -> openRecipe());
//        recent3.setOnClickListener(view -> openRecipe());

    }
    public void openRecipes(){
        Intent rec = new Intent(this, recipes.class);
        startActivity(rec);
        finish();
    }
    public void openSearch(){
        Intent sea = new Intent(this, search.class);
        startActivity(sea);
        finish();
    }
    public void openSupriseMe(){
        Intent sup = new Intent(this, Example.class);
        startActivity(sup);
        finish();
    }
    public void openRecipe(int recipeIndex){
        Intent sup = new Intent(this, Example.class);
        sup.putExtra("recipeIndex", recipeIndex);
        startActivity(sup);
        finish();
    }

`

xzv2uavs

xzv2uavs1#

创建一个新的长度为3的数组,并在使用该数组时在其中添加一个配方。当数组已满并使用了一个配方时,设置:

array[0] = array[1];
array[1] = array[2];
array[2] = most recently used.

当然,你也可以用相反的方法,array[0]是最近使用的数组。

Recipe temp0 = array[0]
array[0] = most recently used
Recipe temp1 = array[1]
array[1] = temp0;
array[2] = temp1;

我不知道你的食谱是什么类型的对象,所以我把它们叫做食谱类型。

相关问题