public class MyActivity extends
Activity {
/**
* Called when the activity is first created.
*/
ListView myList;
public
void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
myList=(ListView)findViewById(R.id.myList);
BaseAdapter adapter = new
BaseAdapter() {
@Override
public
int getCount() {
//指定一共包含40个选项
return
40 ;
}
@Override
public
Object getItem( int
i) {
return
null ; //To change body of implemented methods use File | Settings | File Templates.
}
//重写该方法,该方法的返回值将作为列表项的ID
@Override
public
long getItemId( int
i) {
return
i; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public
View getView( int
i, View view, ViewGroup viewGroup) {
//创建一个LinerarLayout,并向其中添加两个组件
LinearLayout line= new
LinearLayout(MyActivity. this );
line.setOrientation( 0 );
ImageView image= new
ImageView(MyActivity. this );
image.setImageResource(R.drawable.ic_launcher);
TextView text= new
TextView(MyActivity. this );
text.setText( "第" +(i+ 1 )+ "个列表项" );
text.setTextSize( 20 );
text.setTextColor(Color.RED);
line.addView(image);
line.addView(text);
//返回LinearLayout实例
return
line;
}
};
myList.setAdapter(adapter);
}
}
|