【问题标题】:How change Textcolor for only one String in for loop?如何在 for 循环中仅更改一个字符串的 Textcolor?
【发布时间】:2016-09-03 10:13:06
【问题描述】:

一个基本的问题-> 因为我很多天都找不到答案... 如果我有一个for loop(10 次)并且每个输出都是不同的string。如何制作特定的String-output 彩色?

for(int i=0; i< posts.length();i++ ) {
        JSONObject post = posts.optJSONObject(i);
        this.title = post.optString("title");

        blogTitles[i] = title;
        }

当然我可以按代码对特定的blogtitles[i] 进行排序

title.toLowerCase().contains("neune")

但是如果我尝试给它新的layout,他layout 用于所有(10x)输出 有什么可以帮忙的吗?

感谢回答,现在我改为

JSONObject post = posts.getJSONObject(i);
            this.title = post.getString("title");

但所有输出仍然是黑色的,并且布局为“android.R.layout.simple_list_item_1”

完整代码

for(int i=0; i< posts.length();i++ ) {
            JSONObject post = posts.getJSONObject(i);
            this.title = post.getString("title");


            arrayAdapter1 = new ArrayAdapter(MainActivity.this, R.layout.mytextview, blogTitles1);
            arrayAdapter2 = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, blogTitles2);



            if (title.toLowerCase().contains("neune") && title.contains(this.id.toLowerCase())) {         //ean nicht neu (!kontrolliert!)



               //     listView.setTheme(R.layout.mytextview);
                    listView.setAdapter(arrayAdapter1);
                    String title2= title.replace("neune*", "");
                    String title3 = title2.replace(this.id + ",", "-eigene Bewertung");
                    blogTitles1[i] = title3;
                    bewertungseingabe.setVisibility(View.VISIBLE);
                    bewertungsueberschrift.setVisibility(View.VISIBLE);
                    bewertbutton.setVisibility(View.VISIBLE);
                    editBtn.setVisibility(View.VISIBLE);

            }

            if (title.toLowerCase().contains("neune") ) {
                listView.setAdapter(arrayAdapter2);
                    String title2= title.replace("neune*", "error!!!!");
                    String title3 = title2.replace(this.id + ",", "*");
                    blogTitles2[i] = title3;

            }

            if (title.toLowerCase().contains("neuja")&& title.contains(this.id.toLowerCase())) {            //ean neu (!noch nicht kontrolliert!)

                    String title2= title.replace("neuja*", "");
                    String title3 = title2.replace(this.id + ",", "-eigene Bewertung");
                    blogTitles1[i] = title3;

            }
            if (title.toLowerCase().contains("neuja") ) {
                    blogTitles2[i] = "Dieser Artikel ist in Ordnung!";

            }

            if (title.toLowerCase().contains("ganz")) {          //ean neu (!noch nicht kontrolliert!)
                blogTitles2[i] = "Du hast uns so eben eine neue EAN mitgeteilt" +
                        "füge eine Bezeichnung und Bewertung hinzu, wenn du magst...";
              //  zaehler[i]=blogTitles[i];

            }


        }

只使用了第二个布局....我猜这是因为“title.toLowerCase().contains("neune") && title.contains(this.id.toLowerCase())” 是真的并且“title .toLowerCase().contains("neune")" 是真的......它“必须”是这样的,因为每个(i)输出都应该被评估

【问题讨论】:

  • 使用post.getJsonObject(i)
  • 你的意思是 post.getJsonObject(i).contains("neune")? (对不起,我是 android 的新手)
  • @Newbster optstring 与 getString() 相比是一个好方法。stackoverflow.com/a/13790789/3960700

标签: java android android-layout android-studio


【解决方案1】:

如下解析Json

JSONObject post = posts.getJSONObject(i);
this.title = post.getString("title");

【讨论】:

  • 谢谢,但它仍然正常工作......所有都在寻找布局“android.R.layout.simple_list_item_1”(观看主要要求更新代码)
【解决方案2】:

编辑:
字符串 getStr;

    for(int i=0; i< posts.length();i++ ) {
    JSONObject post = posts.optJSONObject(i);
    this.title = post.optString("title");

     blogTitles[i] = title;

       getStr = blogTitles[i];

     if("neune".equals(getStr)){

    SpannableStringBuilder builder = new SpannableStringBuilder();

    SpannableString redSpannable= new SpannableString(getStr);
     redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, getStr, 0);
     builder.append(redSpannable);


    }

【讨论】:

  • 我不能使用equals,因为标题是一个信息包,如“neune,blabla,更多的bla bla,android id,bla”在这个数据包中它应该找到“neune”排序它在帮助下指定这个词并稍后使用其他子集....但我会尝试 spannableStringbulder...需要一些时间来考虑这部分
  • 我无法使用 red.length() “无法解析符号‘红色’”
  • 它不工作失败......“getstring”是一个字符串,对吗?比我不能在“redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, getStr, 0);”处使用它一个 int 是必需的,否则我不能使用 equals 查看更多帖子->输入字符串是一个数据包...
猜你喜欢
  • 2020-01-22
  • 2021-10-11
  • 2020-05-22
  • 2022-01-10
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多