欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ListView展示列表数据的案例演示之获取系统已经安装的应用列表

程序员文章站 2022-05-31 13:29:32
...

代码如下:

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.imooc.demo.R;

import java.util.List;

public class AppListActivity extends AppCompatActivity{

private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.list_view_demo);
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View headerView = layoutInflater.inflate(R.layout.header_list_demo, null);
    mListView.addHeaderView(headerView);
    List<ResolveInfo> infos = getAppInfos();
    mListView.setAdapter(new AppListAdapter(this, infos));
}

private List<ResolveInfo> getAppInfos() {
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    return getPackageManager().queryIntentActivities(mainIntent, 0);
}

public class AppListAdapter extends BaseAdapter{

    private Context mContext;
    private List<ResolveInfo> mInfos;

    public AppListAdapter(Context context, List<ResolveInfo> infos) {
        mContext = context;
        mInfos = infos;
    }

    @Override
    public int getCount() {
        return mInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_demo_list, null);
            // 获取控件
            viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.title_text_view);
            viewHolder.avatarImageView = (ImageView) convertView.findViewById(R.id.icon_image_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 和数据之间进行绑定
        viewHolder.nameTextView.setText(mInfos.get(position).activityInfo.loadLabel(mContext.getPackageManager()));
        viewHolder.avatarImageView.setImageDrawable(mInfos.get(position).activityInfo.loadIcon(mContext.getPackageManager()));

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ResolveInfo info = mInfos.get(position);

                //该应用的包名
                String pkg = info.activityInfo.packageName;
                //应用的主activity类
                String cls = info.activityInfo.name;

                ComponentName componet = new ComponentName(pkg, cls);

                Intent intent = new Intent();
                intent.setComponent(componet);
                startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView avatarImageView;
        TextView nameTextView;
    }
}
}

代码的解析为:
这一部分是获取手机上应用的所有信息

private List<ResolveInfo> getAppInfos() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
return getPackageManager().queryIntentActivities(mainIntent, 0);
}

同时这一部分的参数也要进行修改同时这一部分的参数也要进行修改

mListView.setAdapter(new AppListAdapter(this, infos));

其次设置出来点击事件,来实现点击目标后的效果
一种可以在getView中来实现下面的方法

public void onClick(View v) {
            ResolveInfo info = mInfos.get(position);

            //该应用的包名
            String pkg = info.activityInfo.packageName;
            //应用的主activity类
            String cls = info.activityInfo.name;

            ComponentName componet = new ComponentName(pkg, cls);

            Intent intent = new Intent();
            intent.setComponent(componet);
            startActivity(intent);
        }
    });

第二种为的实例为:
这里要将上边的变量给提取出来,如前两行所示。
ListView展示列表数据的案例演示之获取系统已经安装的应用列表
这个地方为设置出来顶端的图片代码

LayoutInflater layoutInflater = 	(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View headerView = layoutInflater.inflate(R.layout.header_list_demo, null);
mListView.addHeaderView(headerView);

格式为
ListView展示列表数据的案例演示之获取系统已经安装的应用列表
这个案例的代码为优化过后的代码,使运行的速度更加快捷。
这一部分为优化过后的代码:

 if(convertView == null){
        viewHolder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.item_demo_list, null);
        // 获取控件
        viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.title_text_view);
        viewHolder.avatarImageView = (ImageView) convertView.findViewById(R.id.icon_image_view);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
相关标签: ListView android