排序应用程序列表Android Java

排序应用程序列表Android Java,java,android,Java,Android,我安装了一个AppsAdapter import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater;

我安装了一个AppsAdapter

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class InstalledAppsAdapter extends RecyclerView.Adapter<InstalledAppsAdapter.ViewHolder> {

    private Context mContext;
    private List<String> mDataSet;

    public InstalledAppsAdapter(Context context, List<String> list){
        mContext = context;
        mDataSet = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public CardView mCardView;
        public TextView mTextViewLabel;
        public TextView mTextViewPackage;
        public ImageView mImageViewIcon;

        public ViewHolder (View v){
            super(v);
            // Get the widgets reference from custom layout
            mCardView = (CardView) v.findViewById(R.id.card_view);
            mTextViewLabel = (TextView) v.findViewById(R.id.app_label);
            mTextViewPackage = (TextView) v.findViewById(R.id.app_package);
            mImageViewIcon = (ImageView) v.findViewById(R.id.iv_icon);
        }
    }

    @Override
    public InstalledAppsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View v = LayoutInflater.from(mContext).inflate(R.layout.apps_list,parent,false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        // Initialize a new instance of AppManager class
        AppsManager appsManager = new AppsManager(mContext);

        // Get the current package name
        final String packageName = (String) mDataSet.get(position);

        // Get the current app icon
        Drawable icon = appsManager.getAppIconByPackageName(packageName);

        // Get the current app label
        String label = appsManager.getApplicationLabelByPackageName(packageName);

        // Set the current app label
        holder.mTextViewLabel.setText(label);

        // Set the current app package name
        holder.mTextViewPackage.setText(packageName);

        // Set the current app icon
        holder.mImageViewIcon.setImageDrawable(icon);

        // Set a click listener for CardView
        holder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Get the intent to launch the specified application
                Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
                if(intent != null){
                    mContext.startActivity(intent);
                }else {
                    Toast.makeText(mContext,packageName + " Launch Error.", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount(){
        // Count the installed apps
        return mDataSet.size();
    }

}

只需将应用程序列表按应用程序名称升序排序,而不是按程序包名称排序,如果您的
InstalledAppsAdapter
ApplicationInfo
支持,而不是
String
(应用程序包名称)支持,您会感觉更好。然后,您可以创建一个
比较器
,该比较器根据
应用程序信息#loadLabel()
进行排序:

final PackageManager pm=mContext.getPackageManager();
排序(新的比较器(){
@凌驾
公共整数比较(ApplicationInfo lhs、ApplicationInfo rhs){
最后一个字符串lhsLabel=lhs.loadLabel(pm.toString();
最后一个字符串rhsLabel=rhs.loadLabel(pm.toString();
返回字符串。不区分大小写。\u顺序。比较(lhsLabel,rhsLabel);
}
});

您希望
getInstalledPackages
返回一个
列表
,而不是
列表
。你的适配器同样应该为它的
mDataSet
使用
List
,好了,伙计们,在搞乱了支架和bla-bla之后,简单的排序是这样的

在AppsManager.java中

Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator(pm));

完成

@androidbeginnerjon查看我的更新。这里的关键是,不只是将包名存储在列表中,而是将整个
ApplicationInfo
对象存储在列表中,是的,我明白了您返回Applicationinfo的意思,我们可以从中使用应用程序名称对其进行排序,以便在排序后packagename和图标位于正确的位置,但我不知道如何实现它:(我试着制作holder并向其添加数据,但列表中始终只显示一个相同的应用程序,如果您能修改上述代码,将对bro非常有帮助。)
mLayoutManager = new LinearLayoutManager(mContext); // new GridLayoutManager(mContext,2);
mRecyclerView.setLayoutManager(mLayoutManager);

// Initialize a new adapter for RecyclerView
mAdapter = new InstalledAppsAdapter(
        mContext,
        new AppsManager(mContext).getInstalledPackages()
);

// Set the adapter for RecyclerView
mRecyclerView.setAdapter(mAdapter);
final PackageManager pm = mContext.getPackageManager();
mAdapter.sort(new Comparator<ApplicationInfo>() {
    @Override
    public int compare(ApplicationInfo lhs, ApplicationInfo rhs) {
        final String lhsLabel = lhs.loadLabel(pm).toString();
        final String rhsLabel = rhs.loadLabel(pm).toString();
        return String.CASE_INSENSITIVE_ORDER.compare(lhsLabel, rhsLabel);
    }
});
Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator(pm));