我正在使用Android Studio制作我的Android应用。
我有这个代码输出到控制台安装在我的Android手机上的所有应用程序。
如何更改此设置以便将列表输出到Android应用上的RecyclerView,而不是将其输出到控制台?
package com.example.launcher;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class AppInfo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_info);
}
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
System.out.println(appname + "t" + pname + "t" + versionName + "t" + versionCode + "t");
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
}
答案 0 :(得分:2)
According to the Android Developer Training:
活动/片段:
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mAdapter = new PInfoAdapter(apps);
mRecyclerView.setAdapter(mAdapter);
适配器:
public class PInfoAdapter extends RecyclerView.Adapter<PInfoAdapter.ViewHolder> {
private PInfo[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(ViewGroup viewGroup) {
super(v);
mTextView = viewGroup.findViewById(R.id.app_name);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public PInfoAdapter(PInfo[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public PInfoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
ViewGroup v = (ViewGroup) LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
...
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position].getAppName());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
请查看上面的链接,如果您需要任何进一步的帮助,请与我们联系。