自定义BaseAdapter,实现列表显示功能

1.新建list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>

<ImageView
android:id="@+id/img01"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
/>
<TextView
android:id="@+id/tv01"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
/>
</LinearLayout>

2.新建信息存储类(AppInfo)

public class AppInfo
{
public String appName = null;  //应用程序名
public Drawable icon = null;  //应用程序图片
}

3.新建MyAdapter类,继承BaseAdapter

public class MyAdapter extends BaseAdapter
{
private Context context;
private List<AppInfo> appList;

public MyAdapter(Context context, List<AppInfo> appList)
{
this.context = context;
this.appList = appList;
}

@Override
public int getCount()
{
// TODO Auto-generated method stub
return appList.size();
}

@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return appList.get(position);
}

@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View view = null;
ViewHolder holder = null;

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null)
{
view = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder(view);
view.setTag(holder);
} else
{
view = convertView;
holder = (ViewHolder) convertView.getTag();
}
AppInfo appInfo = (AppInfo)getItem(position);
holder.tvInfo.setText(appInfo.appName);
holder.imgInfo.setImageDrawable(appInfo.icon);
return view;
}

class ViewHolder
{
TextView tvInfo;
ImageView imgInfo;

public ViewHolder(View view)
{
tvInfo = (TextView) view.findViewById(R.id.tv01);
imgInfo = (ImageView) view.findViewById(R.id.img01);
}
}

}

4.修改main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<ListView
android:id="@+id/mylist"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/hello"
/>
</LinearLayout>

5.编写mainactivity代码

public class MainActivity extends Activity
{
private ListView listView = null;
private List<AppInfo> appList = new ArrayList<AppInfo>();
private List<PackageInfo> manager = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

listView = (ListView) findViewById(R.id.mylist);

manager = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < manager.size(); i++)
{
PackageInfo packageInfo = manager.get(i);
//应用程序信息存储到appinfo里
AppInfo appInfo = new AppInfo();
appInfo.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
appInfo.icon = packageInfo.applicationInfo.loadIcon(getPackageManager());
appList.add(appInfo);
}

MyAdapter adapter = new MyAdapter(this, appList);
listView.setAdapter(adapter);
}
}


源码:http://files.cnblogs.com/kelei12399/BaseAdapterDemo.zip


转载于:https://www.cnblogs.com/kelei12399/archive/2012/02/06/2340008.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值