Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么ProgressBar';s id必须是唯一的吗?_Android_Android Progressbar_Layout Inflater - Fatal编程技术网

Android 为什么ProgressBar';s id必须是唯一的吗?

Android 为什么ProgressBar';s id必须是唯一的吗?,android,android-progressbar,layout-inflater,Android,Android Progressbar,Layout Inflater,我声明了一个空的LinearLayout,在onCreate方法中,我调用了一个函数,该函数有一个循环,该循环重复五次以膨胀另一个布局并将其添加到LinearLayout中 private void setupList() { LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout itemList = (Line

我声明了一个空的LinearLayout,在onCreate方法中,我调用了一个函数,该函数有一个循环,该循环重复五次以膨胀另一个布局并将其添加到LinearLayout中

private void setupList() {
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout itemList = (LinearLayout) findViewById( R.id.itemList );
    itemList.removeAllViews();
    for ( Category category :: categories ) {
        View rowView = layoutInflater.inflate(R.layout.category_row, null);
        initializeRow( rowView, category.percentComplete );
        itemList.addView( rowView );
    }
}
然后,在initializeRow方法中,我初始化了我刚刚膨胀的视图中的TextView和ProgressBar

private void initializeRow( final View view, final int percentComplete ) {
    TextView topicView = ((TextView) view.findViewById(R.id.topic));
    topicView.setText( category.title );

    ProgressBar progressBar = (ProgressBar) view.findViewById( R.id.progressBar );
    progressBar.setMax( 100 );
    progressBar.setProgress( percentComplete );

    TextView textView = (TextView) view.findViewById( R.id.progressText );
    textView.setText( percentComplete "% Complete" ); 
}
第一次创建此活动时,进度栏和文本视图将以正确的值显示。但是,如果旋转设备,文本视图将显示正确的值,但所有进度条都显示与最后一个进度条对应的进度。为什么在最初调用onCreate时,而在设备旋转后调用onCreate方法时,这种方法不起作用

我意识到所有ProgressBar都有相同的id。但是在我的代码中,我通过使用我膨胀的视图的findViewById来获得对特定ProgressBar的引用。通过调用ProgressBar为每个ProgressBar提供一个唯一的id,我可以实现这一点

    progressBar.setId( progressBarIds[ position ] );

在我的initializeRow方法中。我很好奇这种行为是否是由于ProgressBar中的某些错误造成的,或者是否有一些关于布局展开器或ProgressBar的规则我不了解。

onCreate和旋转事件之间的区别在于,在
onCreate
中,您间接地放大了只有1个ProgressBar的视图。调用
view.findviewbyd(R.id.progressBar)时只能找到1个可能的进度条。然后将其值设置为percentComplete

当你旋转手机时,Android会破坏并创建活动。然后,它还会尝试恢复活动的控件/视图的值。它将视图的值和ID保存在一个捆绑包中,然后尝试从中恢复它们。我怀疑,由于所有ProgressBar都具有相同的id,因此从捆绑包中读取值时,只有分配给该id的值。即,对于每个控件,它检查其id,并尝试在捆绑包中找到相应的值。因为它们都有相同的id,所以它们都得到相同的值

假设这是要保存到bundle的代码:bundle(“valueKey”,integer),可能所有ProgressBar最终都具有相同的键

但是,不管值是如何存储的,
id
是区分每个progressbar appart和其他appart的关键。系统如何知道哪个是哪个

由于progressbars的数量取决于类别的数量,因此您可能需要自己处理这些问题。可能要实现您自己的和函数,这样您就可以存储每个类别百分比,以便在需要恢复它们时告诉它们appart