简体   繁体   English

Android 广告请求。 字符串数组中的关键字

[英]Android ad request. Keywords from string array

I'm trying to add each item in an array list stored in R.array.list to an adrequest for admob.我正在尝试将存储在 R.array.list 中的数组列表中的每个项目添加到 admob 的 adrequest。 When i log the adrequest i get something like "com.google.ads.AdRequest@42168310".当我记录 adrequest 时,我得到类似“com.google.ads.AdRequest@42168310”的信息。 This doesnt seem right since i used to get an actual list when outputting the request, I used to add each item in the java manually by doing:这似乎不对,因为我曾经在输出请求时获取实际列表,我曾经通过执行以下操作手动添加 java 中的每个项目:

adRequest.addKeyword("thisisakeyword");

Here is what i'm trying now:这是我现在正在尝试的:

keyWords = getResources().getStringArray(R.array.key_words);

    int count = 0;
    while (count < keyWords.length) {
        adRequest.addKeyword(keyWords[count]);
        count++;
    }
    System.out.println(String.valueOf(adRequest.toString()));

any ideas?有任何想法吗?

Figured it out myself.我自己想出来的。 Incase anyone comes across this.以防万一有人遇到这个。 The trick is to add the array to a set and then add the set to the ad request.诀窍是将数组添加到集合中,然后将集合添加到广告请求中。

    AdRequest adRequest = new AdRequest();
    String[] keywords = getResources().getStringArray(R.array.key_words);
    Set<String> set = new HashSet<String>();
    int count = 0;
    while (count < keywords.length) {
        set.add(keywords[count]);
        count++;
    }
    adRequest.addKeywords(set);
    adView.loadAd(adRequest);

I just came across this while trying to do the same thing and realised that it's overly-complicated and you're creating unnecessary variables.我只是在尝试做同样的事情时遇到了这个问题,并意识到它过于复杂,并且您正在创建不必要的变量。 You don't actually need to create a set or a counter.您实际上不需要创建集合或计数器。

Try this instead:试试这个:

Builder adRequestBuilder = new AdRequest.Builder();
String[] keywords = getResources().getStringArray(R.array.key_words);
for (String keyword : keywords) {
    adRequestBuilder.addKeyword(keyword);
}

Hope this helps.希望这可以帮助。

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

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