如何将值与数据库值进行比较

时间:2013-06-24 06:35:30

标签: android

使用下面的代码在数据库中插入用户安装的应用程序包名称,并且现在插入成功地在日志中显示我想比较系统安装apped与此数据库值并仅显示数据库中的那个值,那么我如何比较?我支持的是(((p.packageName).equals(“db.list ???”))

    DatabaseHandler db;

                public boolean shouldOverrideUrlLoading(WebView view, String url) {

        String Standard = "https://play.google.com/store/apps/details?id=";
         String id= url.substring(Standard .length(), url.indexOf("&"));
         db.addContact(new Contact(id));


  public class FunActivity extends Activity implements OnItemClickListener {

     DatabaseHandler db;


                          private List<App> loadInstalledApps(boolean includeSysApps) {
  List<App> apps = new ArrayList<App>();

  // the package manager contains the information about all installed apps
  PackageManager packageManager = getPackageManager();

  List<PackageInfo> packs = packageManager.getInstalledPackages(0);    






     for(int i=0; i < packs.size(); i++) {
     PackageInfo p = packs.get(i);
     if ( ((p.packageName).equals("db.list???"))  //what i write here
     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);

1 个答案:

答案 0 :(得分:0)

从数据库中获取列表

在数据库处理程序中添加此方法

public ArrayList<String> getAllApps() {

    String selectQuery = "SELECT  * FROM table";
     ArrayList<String> apps = new ArrayList<String>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            apps.add(cursor.getString("your column name"));
        } while (cursor.moveToNext());
    }

    // return list
    return apps;
}

首先从数据库中获取列表并添加到Arraylist
然后将您的应用名称与它们进行比较

arraylist = dbHelper.getAllApps();

if ( ((p.packageName).equals("db.list???"))中 替换为

 if (arraylist.contains(p.packageName))
    {
       add();
    }
    else
    {
     donothing();
    }