如何声明 ArrayList 中视图的 ID?

huangapple go评论57阅读模式
英文:

How to declare the ID of a view inside an ArrayList?

问题

我正在处理一个包含数百个 ID 的布局,所以我需要一种声明多个 ID 的方法。我尝试过这样做,但应用程序崩溃了。

英文:

I´m working with a layout with hundreds of ID, so I need a way of declaring several id. I tried to do that, but the app crashed.

public class middleScreenfragment extends Fragment {

    private FragmentMiddleScreenfragmentBinding binding;
    List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
    ProgressBar progressBar;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMiddleScreenfragmentBinding.inflate(inflater,container,false);
        View view = binding.getRoot();

        for (int i = 0; i < 10; i++) 
            progressBarList.add(progressBar);
        

        progressBarList.get(0).getRootView().findViewById(R.id.id_1);

        return view;
    }

}

答案1

得分: 0

崩溃的原因是因为progressBar未正确创建。您只是将空对象添加到progressBarList中。

要解决异常,您需要为其分配一个ProgressBar

public class MiddleScreenfragment extends Fragment {
    private FragmentMiddleScreenfragmentBinding binding;
    List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
    ProgressBar progressBar;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ...
        // 分配进度条以使其不为空
        progressBar = new ProgressBar(getContext());
        ...
    }
}

要以编程方式分配ID,您可以使用此函数View.generateViewId()

生成适用于setId(int)的值。此值不会与aapt在构建时为R.id生成的ID值发生冲突。

您可以像这样使用它:

public class MiddleScreenfragment extends Fragment {
    private FragmentMiddleScreenfragmentBinding binding;
    List<ProgressBar> progressBarList = new ArrayList<ProgressBar>();
    ProgressBar progressBar;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMiddleScreenfragmentBinding.inflate(inflater, container, false);
        View view = binding.getRoot();

        for (int i = 0; i < 10; i++) {
            progressBar = new ProgressBar(getContext());
            // 以编程方式设置新创建的进度条的ID
            progressBar.setId(View.generateViewId());
            progressBarList.add(progressBar);
        }

        ...
        
        return view;
    }
}
英文:

The reason why you get crash is because the progressBar is not properly created. You are just adding null Object into your progressBarList.

So to solve the Exception, you have to assign a ProgressBar to it:

public class MiddleScreenfragment extends Fragment {
    private FragmentMiddleScreenfragmentBinding binding;
    List&lt;ProgressBar&gt; progressBarList = new ArrayList&lt;ProgressBar&gt;();
    ProgressBar progressBar;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ...
        // Assign progressBar so that it is not null
        progressBar = new ProgressBar(getContext());
        ...
    }

}

And to assign ID programmatically, you can make use of this function View.generateViewId():
> Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.

And you can use it like this:

public class MiddleScreenfragment extends Fragment {

    private FragmentMiddleScreenfragmentBinding binding;
    List&lt;ProgressBar&gt; progressBarList = new ArrayList&lt;ProgressBar&gt;();
    ProgressBar progressBar;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMiddleScreenfragmentBinding.inflate(inflater,container,false);
        View view = binding.getRoot();

        for (int i = 0; i &lt; 10; i++) {
          progressBar = new ProgressBar(getContext());
          // Set ID programmatically of the newly created Progress Bar
          progressBar.setId(View.generateViewId());
          progressBarList.add(progressBar);
        }

        ...

        return view;
    }

}

huangapple
  • 本文由 发表于 2023年2月14日 06:30:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定