这是我在gridview
中显示系统安装应用程序名称的示例。但我想在gridvew
之上显示具体的应用程序名称我该怎么办?以下是我在listview
中显示所选应用程序的完整源代码,其中提供了我提供的名称,我只想显示此应用程序
((p.packageName).equals("com.qurankarim.urdu"))
gridview
的第一个位置 public class Learning extends Activity implements OnItemClickListener {
/* whether or not to include system apps */
private static final boolean INCLUDE_SYSTEM_APPS = false;
private ListView mAppsList;
private AppListAdapter mAdapter;
private List<App> mApps;
boolean isPressed = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.learning);
final ImageButton Searchbtn = (ImageButton) findViewById(R.id.poweron);
Searchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPressed) {
Searchbtn.setImageResource(R.drawable.power_off);
finish();
} else {
Searchbtn.setImageResource(R.drawable.power_on);
}
isPressed = !isPressed;
}
});
GridView mAppsList = (GridView) findViewById(R.id.appslist);
// mAppsList = (ListView) findViewById(R.id.appslist);
mAppsList.setOnItemClickListener(this);
mApps = loadInstalledApps(INCLUDE_SYSTEM_APPS);
mAdapter = new AppListAdapter(getApplicationContext());
mAdapter.setListItems(mApps);
mAppsList.setAdapter(mAdapter);
new LoadIconsTask().execute(mApps.toArray(new App[] {}));
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final App app = (App) parent.getItemAtPosition(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String msg = "";
builder.setMessage(msg)
.setCancelable(true)
.setTitle(app.getTitle())
.setIcon(mAdapter.getIcons().get(app.getPackageName()))
.setPositiveButton("Launch",
new DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int id) {
// start the app by
invoking its launch intent
Intent i =
getPackageManager()
.getLaunchIntentForPackage(
app.getPackageName());
try {
if (i != null) {
startActivity(i);
} else {
i = new
Intent(app.getPackageName());
startActivity(i);
}
} catch
(ActivityNotFoundException err) {
Toast.makeText(Learning.this,
"Error launching app",
Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private List<App> loadInstalledApps(boolean includeSysApps) {
List<App> apps = new ArrayList<App>();
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo applicationInfo = p.applicationInfo;
if ( ((p.packageName).equals("com.qurankarim.urdu"))
|| ((p.packageName)
.equals("appinventor.ai_hamada_yousef_o.MP3Quruan_2"))
||
((p.packageName).equals("biz.binzrysolution.qibla"))
|| ((p.packageName).equals("com.EaseApps.duasof"))
|| ((p.packageName).equals("com.EaseApps.hajj"))
|| ((p.packageName)
.equals("com.HatafSoft.sahihmuslim_eng_free"))
||
((p.packageName).equals("com.aliftek.quran_urdu"))
||
((p.packageName).equals("com.amirmemon.quizgame.quran"))
||
((p.packageName).equals("com.baqa.quran.fehmulquran"))
||
((p.packageName).equals("com.baqa.quran.tafheemulquran"))
|| ((p.packageName)
.equals("com.bitsmedia.android.muslimpro"))
|| ((p.packageName).equals("com.chaks.duaas"))
|| ((p.packageName).equals("com.chaks.quran"))
||
((p.packageName).equals("com.guidedways.iQuran"))
||
((p.packageName).equals("com.guidedways.iQuranPro"))
||
((p.packageName).equals("com.hajjguide.se.android"))
||
((p.packageName).equals("com.iroshni.asanquran"))
|| ((p.packageName).equals("com.osama.maher"))
|| ((p.packageName).equals("com.osama.sodesEn"))
||
((p.packageName).equals("com.perfection.WWWislan"))
||
((p.packageName).equals("com.quran.labs.androidquran"))
||
((p.packageName).equals("com.triosLabs.SahihMuslim"))
||
((p.packageName).equals("com.triosLabs.hadithreader"))
|| ((p.packageName).equals("com.wAllahsWord"))
|| ((p.packageName).equals("com.yallasoft.quran"))
||
((p.packageName).equals("com.yaz.sahihMuslimOne"))
||
((p.packageName).equals("com.zx.AlQuran_karim_Free"))
||
((p.packageName).equals("com.zx.Alquran_uthmanic_Free"))
||
((p.packageName).equals("excelarz.apps.dhqReader"))
||
((p.packageName).equals("io.vov.android.vitamio.demo"))
|| ((p.packageName).equals("islam3d.liveWPcube"))
|| ((p.packageName).equals("mydotdev.quranurdu"))
|| ((p.packageName).equals("net.xbound.zakirnaik"))
|| ((p.packageName).equals("q.and.u"))
|| ((p.packageName).equals("sadaqa.app.deentv"))
||
((p.packageName).equals("com.verypositive.Quran"))
) {
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager)
.toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description = p.applicationInfo
.loadDescription(packageManager);
app.setDescription(description != null ?
description.toString()
: "");
apps.add(app);
// }
}
}
return apps;
}
/**
* An asynchronous task to load the icons of the installed applications.
*/
private class LoadIconsTask extends AsyncTask<App, Void, Void> {
@Override
protected Void doInBackground(App... apps) {
Map<String, Drawable> icons = new HashMap<String, Drawable>();
PackageManager manager = getApplicationContext()
.getPackageManager();
for (App app : apps) {
String pkgName = app.getPackageName();
Drawable ico = null;
try {
Intent i =
manager.getLaunchIntentForPackage(pkgName);
if (i != null) {
ico = manager.getActivityIcon(i);
}
} catch (NameNotFoundException e) {
Log.e("ERROR", "Unable to find icon for package '"
+ pkgName + "': " +
e.getMessage());
}
icons.put(app.getPackageName(), ico);
}
mAdapter.setIcons(icons);
return null;
}
@Override
protected void onPostExecute(Void result) {
mAdapter.notifyDataSetChanged();
}
}
}
public class AppListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<App> mApps;
/** a map which maps the package name of an app to its icon drawable */
private Map<String, Drawable> mIcons;
private Drawable mStdImg;
/**
* Constructor.
*
* @param context the application context which is needed for the layout inflater
*/
public AppListAdapter(Context context) {
// cache the LayoutInflater to avoid asking for a new one each time
mInflater = LayoutInflater.from(context);
// set the default icon until the actual icon is loaded for an app
mStdImg = context.getResources().getDrawable(R.drawable.icon);
}
@Override
public int getCount() {
return mApps.size();
}
@Override
public Object getItem(int position) {
return mApps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
// creates a ViewHolder and stores a reference to the children view we want to
bind data to
holder = new AppViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.apptitle);
holder.mIcon = (ImageView) convertView.findViewById(R.id.appicon);
convertView.setTag(holder);
} else {
// reuse/overwrite the view passed assuming(!) that it is castable!
holder = (AppViewHolder) convertView.getTag();
}
App app = mApps.get(position);
holder.setTitle(app.getTitle());
if (mIcons == null || mIcons.get(app.getPackageName()) == null) {
holder.setIcon(mStdImg);
} else {
holder.setIcon(mIcons.get(app.getPackageName()));
}
return convertView;
}
public void setListItems(List<App> list) {
mApps = list;
}
public void setIcons(Map<String, Drawable> icons) {
this.mIcons = icons;
}
/**
* Returns the map containing the icons for each displayed app.
*
* @return a map which contains a mapping of package names to icon drawable for all
displayed apps
*/
public Map<String, Drawable> getIcons() {
return mIcons;
}
/**
* A view holder which is used to re/use views inside a list.
*/
public class AppViewHolder {
private TextView mTitle;
private ImageView mIcon;
/**
* Sets the text to be shown as the app's title
*
* @param title the text to be shown inside the list row
*/
public void setTitle(String title) {
mTitle.setText(title);
}
/**
* Sets the icon to be shown next to the app's title
*
* @param img the icon drawable to be displayed
*/
public void setIcon(Drawable img) {
if (img != null) {
mIcon.setImageDrawable(img);
}
}
}
}
public class App {
private String title;
private String packageName;
private String versionName;
private int versionCode;
private String description;
// ordinary getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public int getVersionCode() {
return versionCode;
}
public void setVersionCode(int versionCode) {
this.versionCode = versionCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
我该怎么办?请帮帮我
{{1}}
答案 0 :(得分:0)
我认为最简单的解决方案是重新创建另一个List然后添加其余的应用程序。 你可以这样做:
List<App> apps = new ArrayList<App>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(PackageInfo package : packs) {
String name = package.packageName;
if("com.baqa.quran.tafheemulquran".equals(name)) { // Add other "equals" here
App app = new App(); // Create your App […]
apps.add(app);
packs.remove(package);
}
}
for(PackageInfo package : packs) {
App app = new App(); // Create your App […]
apps.add(app);
}