正确实现了来自PackageInfo
列表数据的appName和appVersion的值,但我希望将Package Info数据与数据库数据相匹配。
面临的问题:
如果我将db数据与for循环中的Package Info数据匹配,那么循环无限运行,如果我在循环外得到Package info数据,那么Package info只显示一个值。
那么如何在for循环之外获取所有的PackageInfo数据,以便它可以在循环外匹配?
代码段:
Cursor cursor = dbHandler.getDeviceApkData();
String dbName = null;
String dbVersion = null;
if (cursor.getCount() !=0) {
if (cursor.moveToFirst()) {
do {
dbName = cursor.getString(1);
dbVersion = cursor.getString(2);
} while (cursor.moveToNext());
}
}
List<PackageInfo> pInfo=getPackageManager().getInstalledPackages(0);
String appNameInfo = null;
String versionInfo = null;
for (PackageInfo packageInfo : pInfo) {
appNameInfo = (String) getPackageManager().getApplicationLabel(packageInfo.applicationInfo);
versionInfo = packageInfo.versionName;
}
if (dbName.equals(appNameInfo) && dbVersion.equals(versionInfo)) {
// do something here
}
else{
// do something here
}
答案 0 :(得分:1)
您需要将代码划分为多种方法,其中每种方法只执行一项特定作业。
首先,移动以下内容以获取appNameInfo
和versionInfo
方法:
List<PackageInfo> pInfo=getPackageManager().getInstalledPackages(0);
String appNameInfo = null;
String versionInfo = null;
for (PackageInfo packageInfo : pInfo) {
appNameInfo = (String) getPackageManager().getApplicationLabel(packageInfo.applicationInfo);
versionInfo = packageInfo.versionName;
}
我们可以看到您的代码产生了两个值:appNameInfo
和versionInfo
这两个值。因此,我们可以使用配对。我们还需要为每个packageInfo创建一个Pair列表。我们可以通过以下方法实现它:
private List<Pair<String, String>> getPackageInfos() {
List<Pair<String, String>> pairs = new ArrayList<>();
List<PackageInfo> pInfo=getPackageManager().getInstalledPackages(0);
for (PackageInfo packageInfo : pInfo) {
String appNameInfo = (String) getPackageManager().getApplicationLabel(packageInfo.applicationInfo);
String versionInfo = packageInfo.versionName;
pairs.add(new Pair<>(appNameInfo, versionInfo));
}
return pairs;
}
第二,您需要使数据库查询代码创建数据列表。因此,您需要更改以下代码:
Cursor cursor = dbHandler.getDeviceApkData();
String dbName = null;
String dbVersion = null;
if (cursor.getCount() !=0) {
if (cursor.moveToFirst()) {
do {
dbName = cursor.getString(1);
dbVersion = cursor.getString(2);
} while (cursor.moveToNext());
}
}
这样的事情:
List<Pair<String, String>> dbNameVersions = new ArrayList<>();
Cursor cursor = dbHandler.getDeviceApkData();
String dbName = null;
String dbVersion = null;
if (cursor.getCount() !=0) {
if (cursor.moveToFirst()) {
do {
dbName = cursor.getString(1);
dbVersion = cursor.getString(2);
dbNameVersions.add(new Pair<>(dbName, dbVersion));
} while (cursor.moveToNext());
}
}
我们也使用配对:
List<Pair<String, String>> dbNameVersions = new ArrayList<>();
所以我们可以将数据与包信息数据进行比较。
第三次,我们可以根据dbNameVersions
和getPackageInfos
执行某些操作,方法是检查包信息中是否存在匹配的dbName和dbVersion,如下所示:
packageInfos.contains(dbNameVersion)
这是完整的代码(我没有这个):
private List<Pair<String, String>> getPackageInfos() {
List<Pair<String, String>> pairs = new ArrayList<>();
List<PackageInfo> pInfo=getPackageManager().getInstalledPackages(0);
for (PackageInfo packageInfo : pInfo) {
String appNameInfo = (String) getPackageManager().getApplicationLabel(packageInfo.applicationInfo);
String versionInfo = packageInfo.versionName;
pairs.add(new Pair<>(appNameInfo, versionInfo));
}
return pairs;
}
private void checkData() {
List<Pair<String, String>> dbNameVersions = new ArrayList<>();
Cursor cursor = dbHandler.getDeviceApkData();
String dbName = null;
String dbVersion = null;
if (cursor.getCount() !=0) {
if (cursor.moveToFirst()) {
do {
dbName = cursor.getString(1);
dbVersion = cursor.getString(2);
dbNameVersions.add(new Pair<>(dbName, dbVersion));
} while (cursor.moveToNext());
}
}
List<Pair<String, String>> packageInfos = getPackageInfos();
for(Pair<String,String> dbNameVersion: dbNameVersions) {
if(packageInfos.contains(dbNameVersion)) {
// found matching dbName and dbVersion in packageInfo.
// do something
} else {
// do something when not found.
}
}
}
答案 1 :(得分:-1)
是否可以将for循环移入while循环?
Cursor cursor = dbHandler.getDeviceApkData();
String dbName = null;
String dbVersion = null;
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
dbName = cursor.getString(1);
dbVersion = cursor.getString(2);
List<PackageInfo> pInfo = getPackageManager().getInstalledPackages(0);
String appNameInfo = null;
String versionInfo = null;
for (PackageInfo packageInfo : pInfo) {
appNameInfo = (String) getPackageManager().getApplicationLabel(packageInfo.applicationInfo);
versionInfo = packageInfo.versionName;
if (dbName.equals(appNameInfo) && dbVersion.equals(versionInfo)) {
// do something here
} else {
// do something here
}
}
} while (cursor.moveToNext());
}
}