在列表视图中显示正在运行的应用程序列表的问题

时间:2019-04-23 02:49:37

标签: java android android-listview android-arrayadapter

我正在尝试在ListView中显示后台运行的应用程序的列表。我尝试过的是创建AppAdapter扩展ArrayAdapter并使用Async任务来调用该方法,该方法返回正在运行的服务的程序包名称的列表。我在下面发布了代码。

AppAdapter:

df1 <- merge(data, lookup) #This merges by class and type columns

df1$new <- with(df1, (condition1 * percentage_condition1) + 
                     (condition2 * percentage_condition2))


df1
#  class type percentage_condition1 percentage_condition2 condition1 condition2  new
#1     1    A                   0.3                   0.7         50         80 71.0
#2     1    A                   0.2                   0.8         50         80 74.0
#3     1    B                   0.5                   0.5         55         86 70.5
#4     2    A                   0.1                   0.9         53         83 80.0
#5     2    B                   0.6                   0.4         60         85 70.0
#6     2    B                   0.4                   0.6         60         85 75.0

主要活动中的代码:

public class AppAdapter extends ArrayAdapter<String> {


private List<ActivityManager.RunningServiceInfo> applist =null;
private Context context;
private PackageManager packageManager;
private List<String> applist1 = null;

public AppAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
    super(context, resource, objects);

    this.context = context;
    this.applist1 = objects;
    packageManager = context.getPackageManager();
}

@Override
public int getCount() {
    return ((applist1 != null) ? applist1.size() : 0);
}


@Override
public String getItem(int position) {
    return ((applist1 != null) ? applist1.get(position) : null);
}

@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 layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_item, null);
    }

    ActivityManager.RunningServiceInfo data = applist.get(position);
    Log.d("Test", "data is  : " + data);

    if (data != null){
        Log.d("Test", "data is not null : " + data);
        TextView appName = view.findViewById(R.id.applabel);
        TextView packageName = view.findViewById(R.id.pname);
        ImageView iconView = view.findViewById(R.id.appicon);

        ComponentName mComponentName = data.service;
        appName.setText(mComponentName.getClassName());
        packageName.setText(mComponentName.getPackageName());

    } else {
        Log.d("Test", "data is null : " + data);

    }
    return view;
}
}

private class LoadApplications extends AsyncTask<Void, Void, Void>{ private ProgressDialog progressDialog = null; @Override protected Void doInBackground(Void... voids) { applist1 = runningServices(); listadapter = new AppAdapter(Taskmanager.this, R.layout.list_item, applist1); Log.d("Test", "applist1 is : " + applist1); return null; } @Override protected void onPostExecute(Void aVoid) { listView.setAdapter(listadapter); progressDialog.dismiss(); super.onPostExecute(aVoid); } @Override protected void onPreExecute() { progressDialog = ProgressDialog.show(Taskmanager.this, null, "Loading.."); super.onPreExecute(); } } 方法正确返回包名称列表(如果您想让我知道的话)。

问题在于应用启动后,它什么也没显示(没有列表)。我没有忘记在活动的onCreate方法中调用异步任务。

有人可以找出可能出问题的地方吗?我只发布了有限的代码,如果您想要更多,请在评论中问我。

1 个答案:

答案 0 :(得分:0)

从后台任务获取列表后,请执行必要的工作。从doInBackgroind方法返回List值,然后将其分配给在UI线程上运行的onPostExecute方法中的适配器。

ua = LWP::UserAgent->new;

sub new {
    my ($class, %args) = @_;

    return bless(\%args, $class);
}