简体   繁体   English

在Android上将数组中的黑名单字符串

[英]Blacklist strings in an array on android

I have an app that I have made for friends to randomize races for a board game, such that a player gets a randomly selected species every time. 我有一个我为朋友制作的应用程序,用于将一个棋盘游戏的比赛随机化,这样玩家每次都会获得一个随机选择的物种。 I want to include a feature in an update that will allow for a blacklist so that certain races cannot be chosen. 我想在更新中包括一个允许加入黑名单的功能,以便无法选择某些种族。 What would be the best way to go about this? 最好的方法是什么? I'll take any and all advice. 我会接受所有建议。 Much thanks in advance. 提前非常感谢。

Of note: The array consists of strings of names, and I'd like for this to be persistent for as long as they have the app installed or until they change it. 注意:数组由名称字符串组成,并且我希望此名称在安装了应用程序或更改应用程序之前一直保持不变。

Edit: Apologies for my lack of clarity. 编辑:抱歉,我缺乏明确性。 I know generally how I need to do it, but what about saving the settings from the settings menu so that upon closing and reopening, blacklisted races are persistent? 我通常知道我该怎么做,但是如何从设置菜单中保存设置,以便在关闭和重新打开后持久保存列入黑名单的种族呢? Even when the app is closed, upon reopening I'd like for the settings to stay the same. 即使关闭了应用程序,重新打开后我也希望设置保持不变。 That way next time they play the game (weeks later), assuming their tastes haven't changed, they can go to clicking without blacklisting again. 这样,下一次(几周后)他们玩游戏时,假设他们的口味没有改变,他们可以单击而无需再次列入黑名单。

Without any code, it's hard to say for sure how to go about this, because I don't know how you're implementing the rest of the app. 没有任何代码,很难确定该怎么做,因为我不知道您如何实现应用程序的其余部分。

One way I can think of doing this is if you associate an integer with each race, eg by using constants: 我可以想到的一种方法是,如果将整数与每个种族相关联,例如通过使用常量:

public static final GIRAFFE = 1;
public static final GOOSE = 2;

Then you could generate random integers to randomly pick each race. 然后,您可以生成随机整数以随机选择每个种族。 If you created a method for the random integer generation, you could then pass certain numbers that would be excluded. 如果您创建了用于随机整数生成的方法,则可以传递某些将被排除的数字。 You could keep generating a random integer while the integer generated was one of the excluded numbers. 当生成的整数是排除的数字之一时,您可以继续生成随机整数。

Eg (Since it's Android I'll code in Java) 例如(因为它是Android,所以我将用Java编写代码)

// assume min = smallest integer assigned to an animal,  
// and max = largest integer assigned to an animal

public static int randomNumber(int ... exclude)
{
    int random = min + (int)(Math.random() * ((max - min) + 1));
    for (int i = 0; i < exclude.length; i++)
    {
         if (exclude[i] == random)
             random = Min + (int)(Math.random() * ((Max - Min) + 1));
    }
    return random;
}

I'm a little new to StackOverflow, so let me know if this helps. 我对StackOverflow有点陌生,所以请告诉我这是否有帮助。

What you need to do is persist data. 您需要做的是持久化数据。 You can use two solutions: 1) The SharedPreferences framework for saving key-value pairs without any effort. 您可以使用两种解决方案:1)SharedPreferences框架可轻松保存键值对。 See here to see how to save a list Store a List or Set in SharedPreferences 请参阅此处以了解如何保存列表在SharedPreferences存储列表或设置

2) use SQL database. 2)使用SQL数据库。 (custom solution) (自定义解决方案)

In case 1 you would persist the blacklisted strings and in case 2 you would be best to create a table with all strings and have a boolean as to whether its blacklisted or not. 在情况1中,您将保留被列入黑名单的字符串,在情况2中,最好创建一个包含所有字符串的表,并对是否将其列入黑名单使用布尔值。

I recommend the shared preferences one since it should take you 5 min to do whereas, unless you are familiar with databases, the database solution will take you a while to work out. 我建议使用共享首选项,因为这需要5分钟的时间,而除非您熟悉数据库,否则数据库解决方案将花费您一段时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM