gpt4 book ai didi

java - 在 RecyclerView 中显示列表

转载 作者:行者123 更新时间:2023-11-29 20:52:16 25 4
gpt4 key购买 nike

我正在使用 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;
}

}

最佳答案

According to the Android Developer Training :

  1. 您需要将 RecyclerView 添加到您的 Activity/fragment 布局
  2. 创建一个可以传递给 RecyclerView 的自定义适配器
  3. 创建您的适配器可用于呈现单个项目的布局

Activity/fragment :

    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;
}
}

请查看上面的链接,如果您需要任何进一步的帮助,请告诉我。

关于java - 在 RecyclerView 中显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28818288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com