0

私はチュートリアルからこのコードを書きました。

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.single_row, viewGroup,false);

    TextView title = (TextView) row.findViewById(R.id.txtTitle);
    TextView description = (TextView) row.findViewById(R.id.txtDescription);
    ImageView image = (ImageView) row.findViewById(R.id.imgPic);

    SingleRow temp = list.get(i);

    title.setText(temp.title);
    description.setText(temp.description);
    image.setImageResource(temp.image);


    return row;
}

このコード行では:

TextView title = (TextView) row.findViewById(R.id.txtTitle);

TextView が同じ種類の変数にコピーされていると思います。次に、このコード行で:

title.setText(temp.title);

その変数に何かを入力します。次に、ビューであり、「タイトル」変数に関連しない行変数が返されます。
使い方?これらの変数はここでは何の関係もないと思います。

4

3 に答える 3

0

このコードは、新しいビューをインフレートし、その内容を設定します。これは、プログラムで新しいビューを作成していることを意味します。多くの場合、リストにデータを入力するときに使用されます。ここでは、構造は同じですが、値が異なる多数の行があります。

仕組みは次のとおりです。

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    // Get the inflater service
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // Inflate view with ID = R.layout.single_row into the row variable
    View row = inflater.inflate(R.layout.single_row, viewGroup,false);

    // Get child views of row: title, description and image.
    TextView title = (TextView) row.findViewById(R.id.txtTitle);
    TextView description = (TextView) row.findViewById(R.id.txtDescription);
    ImageView image = (ImageView) row.findViewById(R.id.imgPic);

    // This get's some template view which will provide data: title, description and image
    SingleRow temp = list.get(i);

    // Here you're setting title, description and image by using values from `temp`.
    title.setText(temp.title);
    description.setText(temp.description);
    image.setImageResource(temp.image);

    // Return the view with all values set. This view will be later probably added somewhere as a child (maybe into a list?)
    return row;
}
于 2013-10-13T09:02:16.703 に答える
0

これは、リストビュー内の行のビューを返すために使用されるメソッドです。ここでわかるように、rowvariable は実際には に関連しています。title

TextView title = (TextView) row.findViewById(R.id.txtTitle);

つまり、titleTextView内部rowオブジェクトであり、そのコードはそれを取得してテキストを設定します。要約すると、getViewメソッド全体が を膨張させ、single_row Viewの関連するすべての子のプロパティを設定していますrow

于 2013-10-13T09:00:03.227 に答える