如果找到匹配项,Android 将在 viewflipper 上设置视图

发布于 2024-12-11 04:07:38 字数 2534 浏览 5 评论 0原文

我知道这可能是一个简单的解决方案,但我无法弄清楚。我想将 viewflipper 上的第一个视图设置为在 if 语句中找到的 true 视图。这是位于 while 循环内的代码:

String itrstr = itr.next();
RelativeLayout rl = new RelativeLayout(this);
TextView titletv= new TextView(this);
TextView bodytv = new TextView(this);
            
if(itrstr.equalsIgnoreCase(titlestr)){
    titletv.setText(Html.fromHtml(itrstr));
    titletv.setPadding(0, 0, 0, 0);
    bodytv.setText(Html.fromHtml(news.get(itrstr)));
    titletv.setId(count);
    bodytv.setPadding(0, 50,0, 0);
    rl.addView(titletv);
    rl.addView(bodytv);
    rl.setId(1);
 }else{    
        titletv.setText(Html.fromHtml(itrstr));
        titletv.setPadding(0, 0, 0, 0);
        bodytv.setText(Html.fromHtml(desc.get(count)));
        titletv.setId(count);
        bodytv.setPadding(0, 50,0, 0);
        rl.addView(titletv);
        rl.addView(bodytv);
        rl.setId(2+count);
  }
 vf.addView(rl);
 count++;

谢谢

Edit-1

我通过在循环之前添加匹配的标题和正文解决了这个问题,但我在匹配的视图和其余视图之间得到了一个空屏幕。

titletv.setText(Html.fromHtml(titlestr));
        titletv.setPadding(0, 0, 0, 0);
       bodytv.setText(Html.fromHtml(news.get(titlestr)));
      
        titletv.setId(count);
        bodytv.setPadding(0, 50,0, 0);
        
        bodytv.setHorizontalScrollBarEnabled(true);
        bodytv.setVerticalScrollBarEnabled(true);
        bodytv.setMovementMethod(new ScrollingMovementMethod());
        rl.addView(titletv);
        rl.addView(bodytv);
        rl.setId(1);
        vf.addView(rl);
        
      //add the view 
      while(itr.hasNext()){
          //Log.i("ArrayList: "+Integer.toString(count),itr.next()+" Desc: "+desc.get(count));
          String itrstr = itr.next();
          rl = new RelativeLayout(this);
            titletv= new TextView(this);
            bodytv = new TextView(this);
            
            if(!itrstr.equalsIgnoreCase(titlestr)){
                
                 
                    
                    titletv.setText(Html.fromHtml(itrstr));
                    titletv.setPadding(0, 0, 0, 0);
                   bodytv.setText(Html.fromHtml(desc.get(count)));
                    titletv.setId(count);
                    bodytv.setPadding(0, 50,0, 0);
                    bodytv.setMovementMethod(new ScrollingMovementMethod());
                    rl.addView(titletv);
                    rl.addView(bodytv);
                    
                    rl.setId(2+count);
            }
            
            vf.addView(rl);
          count++;
      }

I know that it might be a simple solution for this but I am not able to figure it out. I want to set the first view on the viewflipper to be the one which was found true in if statement. Heres the code, which is inside a while loop:

String itrstr = itr.next();
RelativeLayout rl = new RelativeLayout(this);
TextView titletv= new TextView(this);
TextView bodytv = new TextView(this);
            
if(itrstr.equalsIgnoreCase(titlestr)){
    titletv.setText(Html.fromHtml(itrstr));
    titletv.setPadding(0, 0, 0, 0);
    bodytv.setText(Html.fromHtml(news.get(itrstr)));
    titletv.setId(count);
    bodytv.setPadding(0, 50,0, 0);
    rl.addView(titletv);
    rl.addView(bodytv);
    rl.setId(1);
 }else{    
        titletv.setText(Html.fromHtml(itrstr));
        titletv.setPadding(0, 0, 0, 0);
        bodytv.setText(Html.fromHtml(desc.get(count)));
        titletv.setId(count);
        bodytv.setPadding(0, 50,0, 0);
        rl.addView(titletv);
        rl.addView(bodytv);
        rl.setId(2+count);
  }
 vf.addView(rl);
 count++;

Thanks

Edit-1

I solved this by adding the matched title and body before looping through them, but I am getting an empty screen in between the matched view and the rest of the views.

titletv.setText(Html.fromHtml(titlestr));
        titletv.setPadding(0, 0, 0, 0);
       bodytv.setText(Html.fromHtml(news.get(titlestr)));
      
        titletv.setId(count);
        bodytv.setPadding(0, 50,0, 0);
        
        bodytv.setHorizontalScrollBarEnabled(true);
        bodytv.setVerticalScrollBarEnabled(true);
        bodytv.setMovementMethod(new ScrollingMovementMethod());
        rl.addView(titletv);
        rl.addView(bodytv);
        rl.setId(1);
        vf.addView(rl);
        
      //add the view 
      while(itr.hasNext()){
          //Log.i("ArrayList: "+Integer.toString(count),itr.next()+" Desc: "+desc.get(count));
          String itrstr = itr.next();
          rl = new RelativeLayout(this);
            titletv= new TextView(this);
            bodytv = new TextView(this);
            
            if(!itrstr.equalsIgnoreCase(titlestr)){
                
                 
                    
                    titletv.setText(Html.fromHtml(itrstr));
                    titletv.setPadding(0, 0, 0, 0);
                   bodytv.setText(Html.fromHtml(desc.get(count)));
                    titletv.setId(count);
                    bodytv.setPadding(0, 50,0, 0);
                    bodytv.setMovementMethod(new ScrollingMovementMethod());
                    rl.addView(titletv);
                    rl.addView(bodytv);
                    
                    rl.setId(2+count);
            }
            
            vf.addView(rl);
          count++;
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-12-18 04:07:38

您需要创建两个相对布局(或其他布局)。 ViewFlipper 中的位置不是通过 id 设置的。到了添加它们的时候了。首先添加->首先显示

You need to create two relative layouts (or what ever). The position in a ViewFlipper is not set through the id. It is by the time of adding them. First added -> first shown

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文