2014-03-13 96 views
0

我有點新的android編程,我想實現一個ListView的項目是一個自定義的LinearLayout,以編程方式。 我試過類似ListView與LinearLayout

ListView lv = (ListView) findViewById(R.layout.list_view); 
LinearLayout ll = (LinearLayout) findViewById(R.id.list_item); 
lv.addView(ll); 

但它崩潰。 任何人都可以給我一個如何做到這一點的例子嗎? 謝謝!

+0

我猜你正在尋找定製適配器 – Raghunandan

回答

0
protected class MyArrayAdapter extends ArrayAdapter<E> 
{ 
    //List of Events 
    private ArrayList<E> myList; 

    /* 
    * Creates the Adapter for the list 
    */ 
    public MyArrayAdapter (Context context, int textViewResourceId, ArrayList<E> list) 
    { 
     super(context, textViewResourceId, list); 
     myList= list; 
    } 

    /** 
    * inflate view for each row 
    */ 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    {   
     View v = convertView; 
     // if the given channel row view is not being updated 
     if (v == null) 
     { 
      // inflate you linear 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.channel_list_row, null,false); 
     } 


     // edit view here, as shown below 
     TextView topTextView = (TextView)v.findViewById(R.id.mytextview); 
        ... 

     // return the view for the given position 
     return v;   
    } 
} 

而且在主代碼

// create the array adapter 
myArrayAdapter = new MyChannelAdapter(Activity Cntext, Custom row layout, lList); 
// bind the array adapter to the list view 
ListView.setAdapter(myArrayAdapter);