标签:android style blog http color os io ar
BaseAdapter创建这么一个对象,需要些四个方法。 int getCount(); Object getItem(int position); long getItemId(int position);View getView(int position, View convertView, ViewGroup parent);(1)列表中的项数(2)返回值的列表内容(3)获得postion处的列表项的ID(4)该列表项里的组件
package com.example.baseadapter; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { ListView mylist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mylist=(ListView) findViewById(R.id.myList); BaseAdapter adapter=new BaseAdapter(){ @Override public int getCount() { return 40; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //创建一个LinearLayout,并向其中添加两个组件 LinearLayout line=new LinearLayout(MainActivity.this); line.setOrientation(0); ImageView image=new ImageView(MainActivity.this); image.setImageResource(R.drawable.ic_launcher); TextView textview=new TextView(MainActivity.this); textview.setText("第"+(position+1)+"个列表项"); textview.setTextSize(20); textview.setTextColor(Color.RED); line.addView(image); line.addView(textview); //返回LinearLayout 实例 return line; } }; mylist.setAdapter(adapter); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/myList" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
BaseAdapter是通过写其自带默认方法实现ListView
所以他的部分布局不是写在Xml里,而是写在Activity里
安卓学习第13课——BaseAdapter,布布扣,bubuko.com
标签:android style blog http color os io ar
原文地址:http://www.cnblogs.com/Yvettey-me/p/3923540.html