如何在listView android中显示数组列表?

时间:2016-01-12 09:56:02

标签: android listview

如何在列表视图中显示具有此权限的应用列表?

<uses-permission android:name="android.permission.INTERNET" />

我找到了代码here,但我不知道如何在列表视图中显示代码?任何帮助将受到高度赞赏! :d

private ArrayList<String> getInstalledApps(Context context) {
    ArrayList<String> results = new ArrayList<String>();
    PackageManager packageManager = context.getPackageManager();
    List<PackageInfo> applist = packageManager.getInstalledPackages(0);
    Iterator<PackageInfo> it = applist.iterator();
    while (it.hasNext()) {
        PackageInfo pk = (PackageInfo) it.next();
        if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            Log.v("system app using internet = ", ""+pk.applicationInfo.loadLabel(packageManager));
            continue;
        }
        if (PackageManager.PERMISSION_GRANTED == packageManager
                .checkPermission(Manifest.permission.INTERNET,
                        pk.packageName))
            results.add("" + pk.applicationInfo.loadLabel(packageManager));
    }

    Log.v("app using internet = ", results.toString());

    return results;
}

这是我的listview主要活动

    package com.example.antivirus;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class MainActivity extends Activity {

        // Array of strings...
        String[] mobileArray = { "Android", "IPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X" };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ArrayAdapter adapter = new ArrayAdapter<String>(this,
                    R.layout.activity_listview, mobileArray);

            ListView listView = (ListView) findViewById(R.id.mobile_list);
            listView.setAdapter(adapter);
        }
    }

2 个答案:

答案 0 :(得分:0)

您可以参考此链接Android List View

将Arraylist显示在android中的列表视图中。

ArrayList<String> results = new ArrayList<String>();

 @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      getInstalledApps(this);
      ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, results);

      ListView listView = (ListView) findViewById(R.id.mobile_list);
      listView.setAdapter(adapter);
   }

  private ArrayList<String> getInstalledApps(Context context) {
    PackageManager packageManager = context.getPackageManager();
    List<PackageInfo> applist = packageManager.getInstalledPackages(0);
    Iterator<PackageInfo> it = applist.iterator();
    while (it.hasNext()) {
        PackageInfo pk = (PackageInfo) it.next();
        if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            Log.v("system app using internet = ", ""+pk.applicationInfo.loadLabel(packageManager));
            continue;
        }
        if (PackageManager.PERMISSION_GRANTED == packageManager
                .checkPermission(Manifest.permission.INTERNET,
                        pk.packageName))
            results.add("" + pk.applicationInfo.loadLabel(packageManager));
    }

    Log.v("app using internet = ", results.toString());

    return results;
}

将数组列表(结果)用作全局变量,并将方法放在onCreate()

下面的活动中

答案 1 :(得分:0)

这是代码 我测试了它,它工作正常。

import android.Manifest;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MainActivity extends AppCompatActivity {
ArrayList<String>list;
    ListView lv;
    ArrayAdapter aa;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=new ArrayList<String>();
        list=getInstalledApps(this);
        lv= (ListView) findViewById(R.id.listview);
        aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);  //Check this
        lv.setAdapter(aa);
    }

    private ArrayList<String> getInstalledApps(Context context) {
        ArrayList<String> results = new ArrayList<String>();
        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> applist = packageManager.getInstalledPackages(0);
        Iterator<PackageInfo> it = applist.iterator();
        while (it.hasNext()) {
            PackageInfo pk = (PackageInfo) it.next();
            if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                Log.d("Mayur", ""+pk.applicationInfo.loadLabel(packageManager));
                continue;
            }
            if (PackageManager.PERMISSION_GRANTED == packageManager
                    .checkPermission(Manifest.permission.INTERNET,
                            pk.packageName))
                results.add("" + pk.applicationInfo.loadLabel(packageManager));
        }

        Log.v("app using internet = ", results.toString());

        return results;
    }
}

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.statepermission.MainActivity">

   <ListView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/listview"
       ></ListView>
</RelativeLayout>