2013-06-24 79 views
0

如何comapre價值與陣列列表?下面的表單數據庫是我的代碼。我只想過濾僅存在於數據庫中的值。我只想在這一行過濾數組列表如何比較字符串與陣列列表

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; 
    } 
} 
+0

這裏是我的全部代碼http://stackoverflow.com/questions/17280519/android-how-to- match-values-with-database我只想顯示列表中與數據庫匹配的包名稱什麼是ido? –

+0

總結你的問題。你只需要那些存在於數據庫中的包? – MDMalik

+0

yesssssssssssssssss –

回答

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); 
    } 
} 

你正在嘗試完成arrayListString比較,因此是不可能的