2011-02-02 52 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); 

您可能需要做一些調整,但至少我已經給出了一些可能的解決方案的想法。

希望至少其中一個爲你的作品..