私はInstalledAppsAdapterソートのアプリ一覧のAndroidのJava
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();
}
}
とAppsManager
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
public class AppsManager {
private Context mContext;
public AppsManager(Context context){
mContext = context;
}
// Get a list of installed app
public List<String> getInstalledPackages(){
// Initialize a new Intent which action is main
Intent intent = new Intent(Intent.ACTION_MAIN,null);
// Set the newly created intent category to launcher
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// Set the intent flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// Generate a list of ResolveInfo object based on intent filter
List<ResolveInfo> resolveInfoList = mContext.getPackageManager().queryIntentActivities(intent,0);
// Initialize a new ArrayList for holding non system package names
List<String> packageNames = new ArrayList<>();
// Loop through the ResolveInfo list
for(ResolveInfo resolveInfo : resolveInfoList){
// Get the ActivityInfo from current ResolveInfo
ActivityInfo activityInfo = resolveInfo.activityInfo;
// If this is not a system app package
if(!isSystemPackage(resolveInfo)){
// Add the non system package to the list
packageNames.add(activityInfo.applicationInfo.packageName);
}
}
return packageNames;
}
// Custom method to determine an app is system app
public boolean isSystemPackage(ResolveInfo resolveInfo){
return ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
// Custom method to get application icon by package name
public Drawable getAppIconByPackageName(String packageName){
Drawable icon;
try{
icon = mContext.getPackageManager().getApplicationIcon(packageName);
}catch (PackageManager.NameNotFoundException e){
e.printStackTrace();
// Get a default icon
icon = ContextCompat.getDrawable(mContext,R.mipmap.ic_apps);
}
return icon;
}
// Custom method to get application label by package name
public String getApplicationLabelByPackageName(String packageName){
PackageManager packageManager = mContext.getPackageManager();
ApplicationInfo applicationInfo;
String label = "Unknown";
try {
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
if(applicationInfo!=null){
label = (String)packageManager.getApplicationLabel(applicationInfo);
}
}catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return label;
}
}
を持っている私はそれを行うことができる方法、パッケージ名とアプリ名でアダプターをしませソートしたいですか?
私はちょうどあなたのInstalledAppsAdapter
がApplicationInfo
ではなく、に裏打ちされた場合は、オフに良いだろう、パッケージ名
が行われます。ここで重要な点は、パッケージ名だけをリストに格納するのではなく、 'ApplicationInfo'オブジェクト全体をリストに格納し、残りのコードをそのベースに置くことです。 – pathfinderelite
はい私はアプリケーション名を使用してアプリケーション名を返すことを意味するものを得ました。パッケージ名とアイコンは並べ替え後に正しい位置に移動しますが、実装方法は混乱しています。( – AndroidBeginnerJhon
ホルダーとそれにデータを追加しましたが、上記のコードで変更を加えることができれば、常に同じアプリがリストに表示されます – AndroidBeginnerJhon