2013-04-01 65 views
0

我有一個活動,我想顯示一個ListView像datagrid。數組列表ITransducer []是動態的,我想使用數組列表來生成列表視圖的列。ListView佈局生成動態可以編程方式顯示

ListView

ITransducer1 | ITransducer2 | ITransducer3

aa1 | aa2 | AA3

我不得不重寫getView()方法,但我必須加載一個佈局列表視圖這樣 視圖= 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適配器=新ListViewItemAdapter(此,R.layout.historical_data_details_item,其); // 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; 
    } 
} 

}

+0

要顯示aa1 | aa2 | aa3像coloums? – Ankit

+0

肯定的,但列是由SQLite表 – Whistler

回答

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是一類代表列,你可以用你的數據庫初始化的對象。

+0

喜ANKIT,是的AA1,AA2,AA3是一類的對象表示欄動態的,但SQLite表的列的數量是dynamic.it可以是AA1/AA2/AA3/AA4/AA5或AA1/AA2,我不知道怎麼列的許多對象有 – Whistler

+0

好了,所以基本上會在水平方向滾動,對嗎?行數是什麼? – Ankit

+0

嗨Ankit,非常感謝你的response.i找到解決方案http://stackoverflow.com/questions/5730035/cursoradapter-slow-jerky-scroll – Whistler

相關問題