zoukankan      html  css  js  c++  java
  • 自定义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);
    }
    }


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


  • 相关阅读:
    vue-cli3.0
    windows服务器的误解
    redis实现消息队列-java代码实现
    Docker 修改容器内的时区
    Spring boot项目分环境Maven打包,动态配置文件,动态配置项目
    rsa公钥和私钥到底哪个才是用来加密,哪个用来解密?
    session和cookie的区别和联系详解,Cookie Session相关看这篇就够了。
    面试中的nginx高可用高并发!
    js正则表达式验证、匹配数字、匹配字符串、匹配中文、匹配任意字符备忘录
    thymeleaf模板、thymeleaf语法相关中文文档教程
  • 原文地址:https://www.cnblogs.com/kelei12399/p/2340008.html
Copyright © 2011-2022 走看看