RT;
Main.xml
?
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version=
"1.0" encoding=
"utf-8"
?> <LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"vertical"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<ListView
android:id=
"@+id/myList"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
/> </LinearLayout>
|
Myactivity.java
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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);
} }
|
Effect
To create a Badeadapter object, you need to override the following 4 methods to extend the object
* GetCount (): The return value of this method controls how many list items the adapter will contain.
* GetItem (): The return value of the method determines the contents of the list item at postion.
* GETITEMID (int i): The return value of the method determines the ID of the list item at postion.
* GetView (int i,view view,viewgroup viewgroup): The return value of this method determines the list item component in section I