可以以编程方式显示ListView布局生成动态

时间:2013-04-01 07:22:11

标签: android listview dynamic

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

的ListView

ITransducer1 | ITransducer2 | ITransducer3

aa1 | aa2 | AA3

我有覆盖getView()方法,但我必须像这样加载listview的布局 view = vi.inflate(R.layout.historical_data_details_item,null);

我可以通过ITransducer []生成listview布局动态吗?

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 adapter = new ListViewItemAdapter(this,R.layout.historical_data_details_item,its); // lvDetails.setAdapter(adapter);     }

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;
    }
}

}

1 个答案:

答案 0 :(得分: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是表示列的对象,您可以使用数据库初始化该列。