I've created a CursorLoader to work with my Contentprovider(click to view provider) but I am receiving a swapCursor
error
"The method swapCursor(Cursor) is undefined for the type CursorAdapter"
Changed SwapCursor to changeCursor to remove error.
new error is with loader or provider trying to pull sqlite data. please see logcat
any help will be greatly appreciated.
Logcat:
03-10 10:35:02.788: E/AndroidRuntime(1318): FATAL EXCEPTION: ModernAsyncTask #1
03-10 10:35:02.788: E/AndroidRuntime(1318): java.lang.RuntimeException: An error occured while executing doInBackground()
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.lang.Thread.run(Thread.java:856)
03-10 10:35:02.788: E/AndroidRuntime(1318): Caused by: android.database.sqlite.SQLiteException: no such table: _id: , while compiling: SELECT sName, wUrl, uName, pWord, lNotes FROM _id
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:354)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:291)
03-10 10:35:02.788: E/AndroidRuntime(1318): at com.loginplus.home.ListProvider.query(ListProvider.java:61)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.content.ContentProvider$Transport.query(ContentProvider.java:178)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.content.ContentResolver.query(ContentResolver.java:310)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
03-10 10:35:02.788: E/AndroidRuntime(1318): at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
03-10 10:35:02.788: E/AndroidRuntime(1318): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-10 10:35:02.788: E/AndroidRuntime(1318): ... 4 more
Loader activity:
public class LoginList extends FragmentActivity implements OnClickListener, OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private ListView loginList;
private Button webLogin;
private CursorAdapter adapter ;
private static final int LOADER_ID = 02;
private ArrayList<LoginDetails> loginArrayList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_listview);
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
String[] from = { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES};
int[] to = {R.id.rusName, R.id.ruwUrl, R.id.ruuName, R.id.rupWord, R.id.ruNotes};
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to);
loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);
webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES};
CursorLoader cursorloader = new CursorLoader(this, ListProvider.CONTENT_URI, projection, null , null, null);
return cursorloader;
}
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
adapter.changeCursor(cursor);
}
public void onLoaderReset(Loader<Cursor> cursorLoader) {
adapter.changeCursor(null);
}
@Override
public void onClick (View v) {
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}
public List<String> populateList (){
List<String> webNameList = new ArrayList<String>();
dataStore openHelperClass = new dataStore (this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
while (cursor.moveToNext()){
String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));
LoginDetails lpDetails = new LoginDetails();
lpDetails.setsName(sName);
lpDetails.setwUrl(wUrl);
lpDetails.setuName(uName);
lpDetails.setpWord(pWord);
lpDetails.setlNotes(lNotes);
loginArrayList.add(lpDetails);
webNameList.add(sName);
}
cursor.close();
return webNameList;
}
SQLite DB:
String sqlDataStore = "create table if not exists " +
TABLE_NAME_INFOTABLE + " ("+ BaseColumns._ID + " integer primary key autoincrement,"
+ COLUMN_NAME_SITE + " text not null,"
+ COLUMN_NAME_ADDRESS + " text not null,"
+ COLUMN_NAME_USERNAME + " text not null,"
+ COLUMN_NAME_PASSWORD + " text not null,"
+ COLUMN_NAME_NOTES + " text not null);";
db.execSQL(sqlDataStore);
}
In order to be usable in adapters, your SQLite tables must have an "_id" column. It looks like your table does not have one. The common practice is to have it declared as integer primary key autoincrement. Check this answer.