2011-02-02 82 views
0

我试图从xml文件动态地添加一些ProgressBar视图到膨胀的布局。我继续得到的ClassCastExceptions运行下面的代码时:检索dyamily膨胀的布局与

 // === This is the part I'm having trouble with === 
    ProgressBar v = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll); 
    ProgressBar p = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll); 

下面的代码是如何工作的,而不管型和无差错运行,但我需要的意见,进度条工作。有没有什么办法可以做到这一点没有得到一个ClassCastException

/** 
    * Pulls the layout from R.layout.listview and creates a single list 
    * entry and returns it as a view to be put into the listview 
    */ 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // the view to be returned 
     View itemLayoutView = convertView; 

     // if the view doesn't exist, create the layout from the inflator 
     if (itemLayoutView == null) { 
      LayoutInflater mLayoutInflater = (LayoutInflater) getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      // use listview.xml 
      itemLayoutView = mLayoutInflater.inflate(R.layout.listview, 
        null); 

      LinearLayout ll = (LinearLayout) itemLayoutView.findViewById(R.id.List_Main_LinearLayout_ProgressBars); 

      // === This is the part I'm having trouble with === 
      View v = mLayoutInflater.inflate(R.layout.dayprogressbar, ll); 
      View p = mLayoutInflater.inflate(R.layout.dayprogressbar, ll); 


     } 

这里是R.layout.dayprogressbar.xml我正在不断膨胀:

<ProgressBar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:progress="50" 
    android:indeterminateOnly="false" 
    android:progressDrawable="@android:drawable/progress_horizontal" 
    android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal" 
    android:minHeight="1dip" 
    android:maxHeight="1dip" 
    android:max="100" 
    android:paddingLeft="1dp" 
    android:layout_weight="2" 
/> 
+0

我不能undrstnd乌尔need.you要在布局中添加进度或想要定制进度? – 2011-02-02 07:53:45

+0

我已经膨胀了一个xml文件,但是我想向文件内的一个linearlayout添加2个进度条。这里是我拉线性布局的地方 LinearLayout ll =(LinearLayout)itemLayoutView.findViewById(R.id.List_Main_LinearLayout_ProgressBars); 我想将进度条添加到此布局,然后更改条的进度。要更改条的进度,我需要使用2个充气的dayprogressbar.xml对象作为ProgressBar对象而不是View对象,所以我需要以某种方式进行投射或转换。我想知道如何做到这一点,而不会例外:) – 2011-02-02 08:02:09

回答

0

没有尝试过这些,但我无论如何都会发布它们。 :)

我有两个观点:

1)演员v和P,因为你R.layout.dayprogressbar.xml到进度是一个进度条

ProgressBar v = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll); 
ProgressBar p = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll); 

我不知道,如果你能充气两个视图以及同一时间..有没有做我的家庭作业lately.lol

2)您可以抬高这些意见,只是把它添加到LL

ProgressBar v = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, null); 
ProgressBar p = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, null); 
ll.addView(v); 
ll.addView(p); 

您可能需要做一些调整,但至少我已经给出了一些可能的解决方案的想法。

希望至少其中一个为你的作品..