1

こんにちは、

以前、SimpleAdapter などの他のアダプターでこの質問をしましたが、単純なアダプターのソリューションは ArrayAdapter では機能しません。SimpleAdapter で画面の幅と高さを取得し、それをデータ行の RelativeLayout に渡しました。その結果、一度に 1 つのレコードだけが画面全体に表示されました。静的データでは問題ありませんでしたが、可変データを使用したいと考えています。要するに、高さと幅をまったく渡したくありません。私のxmlレイアウトはdipを使用しているため、画面サイズを自由に変更できます。私が本当に求めているのは、getView 呼び出しを一度に 1 つのレコードだけに制御する方法です。

とにかく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 は有効な参照であり、null ではありません。

ArrayAdapter の getView() オーバーライド内で、同じ結果の値を設定しようとしました @Override public View getView(int position, View convertView, ViewGroup parent) {...}

私が作業できる唯一のことは、これといくつかの、すべてではありませんが、私の要素のいくつかは、レイアウトの配置を無視して、一番下にまっすぐ進んでいます。

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);
4

1 に答える 1

1

1.必要に応じてcall to just one record at a time、実装したアダプターの getCount を制御して 1 を返すようにします。

2.java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams例外は、リストビューに追加するビューが、のような他のLayoutParamsの代わりにAbsListView.LayoutParamsRelativeLayout$LayoutParamsを使用する必要があるためです。

編集:

getViewGoogleが推奨する以下のコードを参照してください。

/**
 * 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;
}
于 2012-05-30T01:30:38.377 に答える