1

フラグメント内に設定されたカスタムArrayAdapterを含むリストビューがあります。ListViewの最初の行のみが設定されています。

これは、カスタムArrayAdaptersのチュートリアルをここから文字まで(私が知る限り)行ってきたので、一般にフラグメントでListViewsを使用する場合の一般的な問題である可能性があります。

MyFragment:

public class MyFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment, container, false);

    ListView myListView = (ListView) v.findViewById(R.id.custom_listview);

    ArrayList<CustomRow> customList = new ArrayList<CustomRow>();
    customList.add(new CustomRow("Test A", "A", "AA");
    customList.add(new CustomRow("Test B", "B", "BB");
    customList.add(new CustomRow("Test C", "C", "CC");

    CustomAdapter customAdapter = new CustomAdapter(getActivity(), R.layout.listitem_custom, customList);
    myListView.setAdapter(customAdapter);

    //Inflate the layout for this fragment
    return v;

}

}

したがって、3つの行がリストに追加されても、最初のListItemのみが「TestA」、「A」、「AA」と表示されます。最後の2行は、レイアウトで定義されたとおりにテキストを保持するだけです(つまり、テキストが実際に何かに設定されることはなく、nullでさえも設定されません)。

カスタムArrayAdapter

public class CustomAdapter extends ArrayAdapter<CustomRowItem> {

Context context;
ArrayList<CustomRowItem> rowList;

public CustomAdapter(Context context, int textViewResourceId, ArrayList<CustomRowItem> rowList) {
    super(context, textViewResourceId, rowList);
    this.context = context;
    this.rowList = rowList;
}

public static class ViewHolder {
    public TextView tvText1;
    public TextView tvText2;
    public TextView tvText3;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    // called when listview is not instantiated yet (first run)
    if (convertView == null) {

        // assigns the layout custom_row.xml as the listitem (a single row)
        LayoutInflater alarmInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = alarmInflater.inflate(R.layout.listitem_custom, null);

        holder = new ViewHolder();
        holder.tvText1 = (TextView) convertView.findViewById(R.id.textview1);
        holder.tvText2 = (TextView) convertView.findViewById(R.id.textview2);
        holder.tvText3 = (TextView) convertView.findViewById(R.id.textview3);

        convertView.setTag(holder);

    // called when list view is already instantiated and we are updating the fields
    } else {

        holder = (ViewHolder) convertView.getTag();

        CustomRowItem newRow = rowList.get(position);

        if (newRow != null) {

            holder.tvText1.setText(newRow.getText1());
            holder.tvText2.setText(newRow.getText2());
            holder.tvText3.setText(newRow.getText3());

        }

    }


    return convertView;
}

}

何か案は?

4

1 に答える 1

1

のelseステートメントには、getView()必要以上のコードが含まれています。これがどうあるべきかです:

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    // called when listview is not instantiated yet (first run)
    if (convertView == null) {

        // assigns the layout custom_row.xml as the listitem (a single row)
        LayoutInflater alarmInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = alarmInflater.inflate(R.layout.listitem_custom, null);

        holder = new ViewHolder();
        holder.tvText1 = (TextView) convertView.findViewById(R.id.textview1);
        holder.tvText2 = (TextView) convertView.findViewById(R.id.textview2);
        holder.tvText3 = (TextView) convertView.findViewById(R.id.textview3);

        convertView.setTag(holder);

        // called when list view is already instantiated and we are updating the fields
    } else {
        holder = (ViewHolder) convertView.getTag(); // <-- that's all for here
    }

    CustomRowItem newRow = rowList.get(position);
    if (newRow != null) {
        holder.tvText1.setText(newRow.getText1());
        holder.tvText2.setText(newRow.getText2());
        holder.tvText3.setText(newRow.getText3());
    }

    return convertView;
}
于 2012-12-03T10:13:24.433 に答える