0

在我的应用程序中,我从 Google Play 插入用户访问应用程序包名称并将其保存在数据库中。我只显示来自安装应用程序包名称在数据库中的应用程序。

这些值成功插入数据库,但是当我PlayStoreApps在这一行的类中 进行比较时if (arraylist.equals(p.packageName))

public class PlayStoreApps extends Activity implements OnItemClickListener {

    private static final boolean INCLUDE_SYSTEM_APPS = false;
    DatabaseHandler db;
    private ListView mAppsList;
    private AppListAdapter mAdapter;
    private List<App> mApps;

    db = new DatabaseHandler(this);

    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)) {
                // if ( (p.packageName).equals(arraylist))
                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 Search extends Activity {
        /** Called when the activity is first created. */
        DatabaseHandler db;
        WebView web;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search);
            db = new DatabaseHandler(this);

            String id= url.substring(Standard .length(), url.indexOf("&"));
            db.addContact(new Contact(id));

            Log.d("Reading: ", "Reading all contacts..");
            List<Contact> contacts = db.getAllContacts();       

            for (Contact cn : contacts) {
                String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() ;

                //  String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
                // Writing Contacts to log
                Log.d("Name: ", log);
            }   

            view.loadUrl(url);

            return true;
        }

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "contactsManager";

    private static final String TABLE_CONTACTS = "contacts";

    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";

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

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact
    void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
    //  values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

    // Getting single contact
    Contact getContact(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_NAME }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1));
        // return contact
        return contact;
    }

    // Getting All Contacts
    public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setName(cursor.getString(1));
                //contact.setPhoneNumber(cursor.getString(2));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Updating single contact
    public int updateContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName());
//  values.put(KEY_PH_NO, contact.getPhoneNumber());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", new String[] { String.valueOf(contact.getID()) });
    }

    // Deleting single contact
    public void deleteContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { String.valueOf(contact.getID()) });
        db.close();
    }

    public int deleteContact() {
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            return db.delete(TABLE_CONTACTS, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    // Getting contacts Count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

    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

首先:不要使用非泛型Lists

在您的loadInstalledApps方法中,您使用以下行:

List arraylist = db.getAllApps();

作为getAllApps回报,ArrayList<String>List应该是 type List<String>

第二:您不能像您尝试做的那样比较列表中的项目:

if(arraylist.equals(p.packageName)

您实际上是在尝试查看ArrayList对象是否等于 a String。将苹果与橙子进行比较也不太顺利……

第三:在代码中比较而不是在 SQL 中比较在性能方面确实很慢,绝对应该避免,尤其是在移动操作系统上,因为这是对内存和 CPU 的浪费。

如果你真的想在代码中进行比较,你可以这样做:

if(arraylist.contains(p.packageName)) {
    // Do something
    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);
}

但我不会推荐这条路。

于 2013-06-24T20:00:20.000 回答