【问题标题】:How to get specific TextView (View ) from a ListView without use of custom Adapter?如何在不使用自定义适配器的情况下从 ListView 获取特定的 TextView (View)?
【发布时间】:2016-02-26 15:27:09
【问题描述】:

这是我想要获得ListView 的特定子视图的代码:

ListView list = (ListView) findViewById(R.id.lst);

ArrayAdapter<String> adap = new ArrayAdapter<String>(this,
        R.layout.text, R.id.txt1, data);

list.setAdapter(adap);
int num = list.getCount();
for (int j = 0; j < num; j++) 
{
     view1 = findViewById((int)list.getItemIdAtPosition(j));
     text_view = list.getChildAt(j);
    long lg = list.getItemIdAtPosition(j);
    if (text_view != null) {
        text_view.startAnimation(animation);  //  getting null in text_view .
    }
    if (view1 != null) {
        view1.startAnimation(animation);     //  getting null in view1 .
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    使用 getChildAt 获得的视图是列表视图中包含的一行。这取决于您如何定义该行。如果您的行包含一个布局,然后是一个 textView,那么您获得的子视图就是布局。那么 textView 就是那个布局的子视图。

    【讨论】:

      【解决方案2】:

      假设您选择的项目位置在名为 pos 的变量中。

        pos=2; // selected item position
        int nFirstPos = listNotification.getFirstVisiblePosition();
              int nWantedPos = pos - nFirstPos;
              if ((nWantedPos >= 0) && (nWantedPos <= listNotification.getChildCount()))
              {
                  View view = listNotification.getChildAt(nWantedPos);
                  if (view!=null) {
      
                         //add your code here
                      TextView textData=(TextView) view.findViewById(R.id.txt1);
                  }
      
              }
      

      【讨论】:

        【解决方案3】:

        我认为您可以仅对可见项目执行此操作,并且您应该知道 listview 的所有项目都不是一次可见的。请检查以下链接,您将解决您的问题:

        Android: Access child views from a ListView

        In an android ListView, how can I iterate/manipulte all the child views, not just the visible ones?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-23
          • 2015-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多