0

如何用arraylist比较值?下面的表单数据库是我的代码。我只想过滤数据库中存在的值。我只想在这一行过滤数组列表

if (arraylist.equals(p.packageName))

所以只显示与数据库匹配的包名我该怎么办?

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); 
    List arraylist = db.getAllApps();

    for (int i = 0; i < packs.size(); i++) {
      PackageInfo p = packs.get(i);
      ApplicationInfo applicationInfo = p.applicationInfo;
      if (arraylist.equals(p.packageName))
      {
        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;
}

public class DatabaseHandler extends SQLiteOpenHelper {
     // All Static variables    // Database Version
     private static final int DATABASE_VERSION = 1;

     // Database Name
     private static final String DATABASE_NAME = "contactsManager";

     // Contacts table name
     private static final String TABLE_CONTACTS = "contacts";

     // Contacts Table Columns names
     private static final String KEY_ID = "id";
     private static final String KEY_NAME = "name";
     //private static final String KEY_PH_NO = "phone_number";

     public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
     }

     // Creating Tables
     @Override
     public void onCreate(SQLiteDatabase db) {
         // String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("+ 
         //  KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"+ KEY_PH_NO + " TEXT" + ")";

         String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS +
             "("+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" +  " UNIQUE " + ")";
         db.execSQL(CREATE_CONTACTS_TABLE);
     }

     public ArrayList<String> getAllApps() {
         String selectQuery = "SELECT  * FROM "+TABLE_CONTACTS;
         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(cursor.getColumnIndex(KEY_NAME)));
             } while (cursor.moveToNext());
         }

         // return list
         return apps;
     }
}
4

1 回答 1

0

我只是在您的数组列表中更改了一些位。

for (int i = 0; i < packs.size(); i++) {
  PackageInfo p = packs.get(i);
  ApplicationInfo applicationInfo = p.applicationInfo;
  String arrayval= arraylist.get(i).toString();
  if (arrayval.equals(p.packageName))
  {
    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);
  }
}

您试图将 completearrayList与 a进行比较,String因此这是不可能的

于 2013-06-24T20:13:09.517 回答