我有一个活动,我想在其中显示像 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;
}
}
}