0

我有一个活动,我想在其中显示像 datagrid 这样的 ListView。arraylist ITransducer[] 是动态的,我想使用arraylist 来生成listview 的列。

列表显示

ITransducer1 | ITransducer2 | ITransducer3

aa1 | aa2 | aa3

我已经覆盖了 getView() 方法,但是我必须像这样为 listview 加载一个布局 view = vi.inflate(R.layout.historical_data_details_item, null);

我可以通过 ITransducer[] 动态生成列表视图布局吗?

public class HistoricalDataDetailsActivity extends Activity {
ListView lvDetails;
Context context;
IDAL idal;
TransducerFactory tFactory;

@Override
protected void onCreate(Bundle savedInBundleState) {
    super.onCreate(savedInBundleState);
    setContentView(R.layout.historical_data_details);
    context = this;
    tFactory = new TransducerFactory();
    lvDetails = (ListView) findViewById(R.id.lvDetails);
}

@Override
protected void onStart() {
    super.onStart();
    ITransducer[] its = new ITransducer[]{
        new AbsTransducer("aa1", TransducerType.AirPressure),
        new AbsTransducer("aa2", TransducerType.AirPressure),
        new AbsTransducer("aa3", TransducerType.Temperature)
    };

// ListViewItemAdapter 适配器 = new ListViewItemAdapter(this, R.layout.historical_data_details_item,its); // lvDetails.setAdapter(适配器); }

class ListViewItemAdapter extends ArrayAdapter<ITransducer>{
    boolean HasHeader = false;

    public ListViewItemAdapter(Context context, int textViewResourceId, ITransducer[] objects){
        super(context, textViewResourceId, objects);

    }

    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
        if(view == null){
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.historical_data_details_item, null);

            TextView tvDate = new TextView(context);
            tvDate.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            tvDate.setText("DateTime");

            LinearLayout layout = (LinearLayout) view.findViewById(R.id.tLayout);
            layout.addView(tvDate);
            view = layout;

        }else
            view = convertView;

        return view;
    }
}

}

4

1 回答 1

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

       if (aa1!= null) {
            holder.aa1_tv.setText(aa1.getName());
        }
       if (aa2!= null) {
            holder.aa2_tv.setText(aa2.getName());
        }
       if (aa3!= null) {
            holder.aa3_tv.setText(aa3.getName());
        }
}

aa1/aa2/aa3是代表列的类的对象,您可以使用数据库对其进行初始化。

于 2013-04-09T10:59:29.107 回答