0

RelativeLayoutの行をどのように配置するかを定義するのハンドルを取得しようとしていListViewます。私はnullになっています.2つの理由の1つが原因だと思います.

可能性 1 つは、まだインフレになっていないため、null を返すことです....これについて読んだことがありますが、インフレの概念については非常に不安定です。私はJavaで3週間プログラミングしています(C#から)。だから私はテストする方法がわかりません。たとえば、アダプターをリスト ビューに設定するだけです。どのイベントが発生したのか、またはインフレーションがカバーの下で正確にいつ発生したのかはわかりません。

可能性 2 は、適切にツリーを歩いていないことです。

私の親:

<RelativeLayout 
    android:minHeight="320dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/rlay_LftDataParent" 
    android:orientation="vertical" >

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:id="@+id/lstvw_LiftData" />

行の定義 (最小の高さを設定しようとしているレイアウト)

<RelativeLayout 
    android:layout_width="fill_parent"
    android:orientation="vertical" 
    android:id="@+id/rlay_LftDataRw" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background">
...
</RelativeLayout>

私が試したアクティビティで: (lstvw_LiftData は私の ListView オブジェクトです)

RelativeLayout RowLayout = (RelativeLayout)lstvw_LiftData.findViewById(R.id.rlay_LftDataRw);

RelativeLayout RowLayout = (RelativeLayout)findViewById(R.id.rlay_LftDataRw);

全体像を知りたい場合は、この投稿からの質問の続きです。RelativeLayout を使用して ListView の行の高さを制御します。行のレイアウトを画面いっぱいに表示しようとしています。

4

2 に答える 2

1

。を使用して行Viewからにアクセスすることはできません。代わりに、アダプターを使用してメソッド内の参照を取得し、そこで幅と高さを設定します。どのアダプタを使用するかは言わなかったので、ここに基本の簡単な例を示します。ListViewfindViewByIdRelativeLayoutgetViewArrayAdapter

編集:メソッドを自分で 呼び出さないでください。新しい行を表示するときにgetView()、アダプターがメソッドを自動的に呼び出します。getView最後のコメントからの質問に関して、カスタムAdapterクラスがそれを使用する場所の内部クラスであるActivity場合:

public class SomeActivity extends ListActivity {

     private int mHeight, mWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...do what ever stuff you need in this activity
       // find out the height/width values:
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        mHeight = metrics.heightPixels;
        mWidth = metrics.widthPixels;   
        // set the adapter
        setListAdapter(new CustomAdapter(this,
                R.layout.adapters_listview_rowheight, R.id.textView1, items));
    }

    //you use your own adapter class
    private class CustomAdapter extends ArrayAdapter<String> {

        public CustomAdapter(Context context, int resource,
                int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                    //do whatever logic you need here
                    // finally you end up with the RelativeLayout you'll return as the view for the row
                    // before you return set the desired values for its width and height
            rl.getLayoutParams().height = mHeight;
            rl.getLayoutParams().width = mWidth;
            return rl;
        }


    }

}

Adapterクラスが別のファイルにある場合は、そのアダプターのコンストラクターを作成してint、幅と高さを表す2つの値を取得します。

//the constructor
public CustomAdapter(/*whatever parameters you already have*/, int height, int width) {
    //do stuff
    //now you also have a reference for the width and height that you can use in the getView method.
}

およびActivity(onCreateメソッドと同じコードを使用して):

setListAdapter(new CustomAdapter(/*whatever parameters you already have*/, mHeight, mWidth));

これがあなたの望むものかどうかはわかりません。

于 2012-05-17T16:18:15.930 に答える
0

を介して行にアクセスしますAdapter。のようなものですが、使用getViewしているものによってはAdapter異なる場合があります。

于 2012-05-17T15:54:15.880 に答える