如何在for循环中仅为一个String更改Textcolor?

时间:2016-09-03 10:13:06

标签: java android android-layout android-studio

一个基本问题 - >因为很多天以来我找不到答案 如果我有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");

但仍然所有输出都是黑色和布局&#34; android.R.layout.simple_list_item_1&#34;

完整代码

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];

            }


        }

只使用了第二种布局....我想这是因为&#34; title.toLowerCase()。包含(&#34; neune&#34;)&amp;&amp; title.contains(this.id.toLowerCase())&#34;是真的&#34; title.toLowerCase()。包含(&#34; neune&#34;)&#34;是真的......它必须&#34;这样,因为应评估每个(i)输出

2 个答案:

答案 0 :(得分:0)

按如下方式解析Json

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

答案 1 :(得分:0)

修改
        String 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);


    }