2012-05-29 37 views
1

下午好,ArrayAdapter磺酰基顯示一個記錄

我以前也問過這個問題與其他適配器一樣SimpleAdapter但對於簡單的適配器的解決方案是不工作ArrayAdapter。在SimpleAdapter中,我獲得了屏幕寬度&高度,然後將其傳遞給我的RelativeLayout以獲取數據行。其效果是每次只顯示一個填滿整個屏幕的記錄。靜態數據沒問題,但現在我想使用可變數據。總而言之,我真的不希望通過高度和寬度。我的xml佈局正在使用dip,所以屏幕尺寸可以改變他們想要的。我真正在做的是一次控制getView調用一次只記錄一條記錄的方法。

無論如何使用SimpleAdapter的解決方法我認爲傳遞的高度和寬度就像以前一樣會工作,但由於某種原因,這次我得到一個錯誤,我不明白。這裏是設定的寬度和高度的代碼:

RelativeLayout.LayoutParams szParms = new RelativeLayout.LayoutParams(mWidth, mHeight); 
vw_BigRow.setLayoutParams(szParms); 

的高度和寬度在別處確定並且可以在我已經測試了所有的設備準確。 不知道還有什麼要發佈,所以只要告訴我,我添加該代碼/信息的問題。

E/AndroidRuntime(404): FATAL EXCEPTION: main 
E/AndroidRuntime(404): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams 

編輯: 由於已經正確地指出了一個CastClassException但我仍然沒有做分配權利,我繼續得到錯誤發生時我的錯誤。我曾嘗試在設置高度/寬度具有以下活性:

AbsListView.LayoutParams szParms = new AbsListView.LayoutParams(mWidth, mHeight); 
lstvw_LiftData.setLayoutParams(szParms); 

E/AndroidRuntime(1886): FATAL EXCEPTION: main 
E/AndroidRuntime(1886): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams 

ListView.LayoutParams szParms = new ListView.LayoutParams(mWidth, mHeight); 
lstvw_LiftData.setLayoutParams(szParms); 

E/AndroidRuntime(1886): FATAL EXCEPTION: main 
E/AndroidRuntime(1886): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams 

的lstvw_LiftData是有效ref和不爲空。

裏面一個ArrayAdapter的getView()重寫我也試着用同樣的結果 @覆蓋 公共查看getView(INT位置,查看convertView,ViewGroup以及母公司) {...}

設置的值

我唯一可以工作的就是這個,然後是一對夫婦,不是全部,但是我的一些元素忽略了他們的佈局定位,並且走向底部。

View vw_BigRow = convertView; 

if (vw_BigRow == null) 
{ 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    vw_BigRow = inflater.inflate(R.layout.liftdatalayout, null); 
} 

vw_BigRow.setMinimumHeight(mHeight); 
vw_BigRow.setMinimumWidth(mWidth); 
+0

你解決了這個問題嗎?從您添加的代碼中,我看不到您如何使用視圖設置數據。 – dreamtale

回答

1

1.如果您想call to just one record at a time,只是控制你實現適配器的getCount將返回1.

2. java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams例外是因爲視圖想要添加到列表視圖應該使用AbsListView。 LayoutParams而不是其他LayoutParams,例如RelativeLayout$LayoutParams

編輯:

見下面這是由谷歌推薦getView代碼。

/** 
* Make a view to hold each row. 
* 
* @see android.widget.ListAdapter#getView(int, android.view.View, 
*  android.view.ViewGroup) 
*/ 
public View getView(int position, View convertView, ViewGroup parent) { 
    // A ViewHolder keeps references to children views to avoid unneccessary calls 
    // to findViewById() on each row. 
    ViewHolder holder; 

    // When convertView is not null, we can reuse it directly, there is no need 
    // to reinflate it. We only inflate a new View when the convertView supplied 
    // by ListView is null. 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_item_icon_text, null); 

     // Creates a ViewHolder and store references to the two children views 
     // we want to bind data to. 
     holder = new ViewHolder(); 
     holder.text = (TextView) convertView.findViewById(R.id.text); 
     holder.icon = (ImageView) convertView.findViewById(R.id.icon); 

     convertView.setTag(holder); 
    } else { 
     // Get the ViewHolder back to get fast access to the TextView 
     // and the ImageView. 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    // Bind the data efficiently with the holder. 
    holder.text.setText(DATA[position]); 
    holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 

    return convertView; 
} 

static class ViewHolder { 
    TextView text; 
    ImageView icon; 
} 
+0

請參閱編輯,因爲我仍然在做錯事。 – GPGVM

+0

@ user1278561請將您的所有getview代碼或所有適配器代碼。這將幫助我找到真正的問題。你的物品佈局是什麼?佈局或只是一個視圖。如果它只是一個視圖,您可以將視圖放在XML的根目錄下。 – dreamtale

+0

感謝您的更新。今天早上我會在公共汽車上閱讀/改進公共汽車,並在工作中發佈更多的代碼。你也建議覆蓋getCount阻止我前進到下一個記錄......我確信我做錯了。我在這裏發佈了一個沒有答案的問題。 http://stackoverflow.com/questions/10693112/advance-listview-next-item-setselectionfromtop-not-working-inhibit-user-scro – GPGVM