【问题标题】:ListView with LinearLayout带有线性布局的 ListView
【发布时间】:2014-03-14 00:43:51
【问题描述】:
我对 android 编程有点陌生,我想以编程方式实现一个 ListView,其中项目是自定义的 LinearLayout。
我尝试过类似的东西
ListView lv = (ListView) findViewById(R.layout.list_view);
LinearLayout ll = (LinearLayout) findViewById(R.id.list_item);
lv.addView(ll);
但它崩溃了。
谁能给我一个应该如何做的例子?
谢谢!
【问题讨论】:
标签:
android
android-layout
listview
android-linearlayout
【解决方案1】:
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);