0

I am kinda new to the android programming and i would like to implement a ListView which items are a custom LinearLayout, programmatically. I have tried something like

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

But it crashes. Can anyone give me an example of how this should be done? Thanks!

Pavle37
  • 571
  • 9
  • 24

3 Answers3

0

You must implement a custom Adapter for your ListView. An Adapter is like a row in the list. Please take a look at this tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html or to this similar question: Custom Adapter for List View

Community
  • 1
  • 1
Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

please find.. ListView good tutorials

User10001
  • 1,295
  • 2
  • 18
  • 30
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;           
    }
}

And in main code

 // create the  array adapter
 myArrayAdapter = new MyChannelAdapter(Activity Cntext, Custom row layout, lList);
 // bind the array adapter to the list view
 ListView.setAdapter(myArrayAdapter);
Manitoba
  • 8,522
  • 11
  • 60
  • 122
Parnit
  • 1,032
  • 2
  • 8
  • 16