下面是我的代码我按照这个链接如何将值与数据库值进行比较并自定义我的代码我只想在数据库中显示用户在数据库中插入的那个名称但是在getAllApps方法的帮助器类中显示我的错误方法getString(int ) 类型的游标不适用于参数 (String)
import android.database.sqlite.SQLiteOpenHelper;
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);
}
// 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";
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( KEY_NAME));
} while (cursor.moveToNext());
}
// return list
return apps;
}
}
还有我的活动课
public class PlayStoreApps extends Activity implements
OnItemClickListener {
/* whether or not to include system apps */
private static final boolean INCLUDE_SYSTEM_APPS = false;
DatabaseHandler db;
private ListView mAppsList;
private AppListAdapter mAdapter;
private List<App> mApps;
String APKFilePath = "mnt/sdcard/foldername/";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.games);
db = new DatabaseHandler(this);
Button AddMore = (Button) findViewById(R.id.AddMore);
AddMore.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Intent intent = new Intent(FunActivity.this,
// SdcardAPkMgr.class);
// startActivity(intent);
}
});
mAppsList = (ListView) findViewById(R.id.appslist);
mAppsList.setOnItemClickListener(this);
mApps = loadInstalledApps(INCLUDE_SYSTEM_APPS);
mAdapter = new AppListAdapter(getApplicationContext());
mAdapter.setListItems(mApps);
mAppsList.setAdapter(mAdapter);
new LoadIconsTask().execute(mApps.toArray(new App[] {}));
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final App app = (App) parent.getItemAtPosition(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String msg = app.getTitle()
+ "\n\n"
+ "Version "
+ app.getVersionName()
+ " ("
+ app.getVersionCode()
+ ")"
+ (app.getDescription() != null ? ("\n\n" + app
.getDescription()) : "");
builder.setMessage(msg)
.setCancelable(true)
.setTitle(app.getTitle())
.setIcon(mAdapter.getIcons().get(app.getPackageName()))
.setPositiveButton("Launch",
new DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int id) {
// start the app by
invoking its launch intent
Intent i =
getPackageManager()
.getLaunchIntentForPackage(
app.getPackageName());
try {
if (i != null) {
startActivity(i);
} else {
i = new
Intent(app.getPackageName());
startActivity(i);
}
} catch
(ActivityNotFoundException err) {
Toast.makeText(PlayStoreApps.this,
"Error launching app",
Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
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); //
PackageManager.GET_META_DATA
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo applicationInfo = p.applicationInfo;
String name = packageManager.getApplicationLabel(applicationInfo)
.toString();
List arraylist = db.getAllApps();
if (arraylist.contains(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;
}
/**
* An asynchronous task to load the icons of the installed applications.
*/
private class LoadIconsTask extends AsyncTask<App, Void, Void> {
@Override
protected Void doInBackground(App... apps) {
Map<String, Drawable> icons = new HashMap<String, Drawable>();
PackageManager manager = getApplicationContext()
.getPackageManager();
for (App app : apps) {
String pkgName = app.getPackageName();
Drawable ico = null;
try {
Intent i =
manager.getLaunchIntentForPackage(pkgName);
if (i != null) {
ico = manager.getActivityIcon(i);
}
} catch (NameNotFoundException e) {
Log.e("ERROR", "Unable to find icon for package '"
+ pkgName + "': " +
e.getMessage());
}
icons.put(app.getPackageName(), ico);
}
mAdapter.setIcons(icons);
return null;
}
@Override
protected void onPostExecute(Void result) {
mAdapter.notifyDataSetChanged();
}
}
}