将字符串listview转换为自定义列表视图

时间:2015-03-23 15:06:19

标签: android android-listview android-studio

我创建了一个列表视图,其中显示了Android设备上当前安装的所有应用。代码还会检索应用程序的图标,但我无法在列表中显示它,因为它只能显示字符串。如何更改我的代码,以便它还显示名称旁边的应用程序图标?

另外,在我这样做之后,我还想在每个应用标题和图标旁边添加一个复选框

    final ArrayList<String> list = new ArrayList<String>();


    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
    for (Object object : pkgAppsList)
    {
        ResolveInfo info = (ResolveInfo) object;
        Drawable icon    = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
        String strAppName   = info.activityInfo.applicationInfo.publicSourceDir.toString();
        String strPackageName  = info.activityInfo.applicationInfo.packageName.toString();
        final String title  = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
        list.add(title);
    }

    final ListView listview = (ListView) findViewById(R.id.listView);
    final ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

感谢

1 个答案:

答案 0 :(得分:0)

你需要3件事:

  1. 单行布局
  2. 用于存储您的App-Name&amp;图标
  3. 自定义列表适配器
  4. custom_row_layout.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@drawable/nav_item_bg"
    android:clickable="true"
    android:orientation="horizontal">
    
    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp" />
    
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:textSize="16sp" />
    
    </LinearLayout>
    

    AppDetails.java

    public class AppDetails {
        String name;
        Drawable icon;
    
        public AppDetails(String name, Drawable icon) {
            this.name = name;
            this.icon = icon;
        }
    }
    

    AppDetailsAdapter.java

    public class AppDetailsAdapter extends BaseAdapter {
    
    private ArrayList<AppDetails> data;
    private Context context;
    
    public AppDetailsAdapter(Context context, List<AppDetails> data){
        this.context = context;
        this.data = data;
    }
    
    @Override
    public int getCount(){
        return data.size();
    }
    
    @Override
    public Object getItem(int position){
        return data.get(position);
    }
    
    @Override
    public long getItemId(int position){
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
    
        if(view == null){
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.custom_row_layout, null);
        }
    
        ImageView icon = (ImageView) view.findViewById(R.id.icon);
        TextView text = (TextView) view.findViewById(R.id.text);
    
        final AppDetails item = data.get(position);
        text.setText(item.name); 
        icon.setImageDrawable(item.icon);
    
        return view;
    }
    

    要在您的代码中实现此功能,您应该执行以下操作:

    List<AppDetails> appList = new ArrayList<>();
    for (Object object : pkgAppsList)
    {
        //This is your foreach loop over all apps
        //.. Add these 2 line at the end of your loop
        AppDetails tmp = new AppDetails(strAppName, icon);
        appList.add(tmp)
    }
    

    而不是使用exsstig ArrayAdapter,你应该使用我在上面创建的那个:

    ListView listview = (ListView) findViewById(R.id.listView);
    AppDetailsAdapter adapter = new AppDetailsAdapter(this, appList);
    listview.setAdapter(adapter);