Update ListView - android

I received cursor from Database and set andapter to ListView. Then I insert new row to Database, but my ListView didn't update.
Cursor c = getContentResolver().query(GEN_URI, null, null, null, null);
String[] from = { DB.column_name };
int[] to = { R.id.textViewItem };
myAdapter = new SimpleCursorAdapter(this, R.layout.shop_list_item, c, from, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
ShopList.setAdapter(myAdapter);
ContentValues newC = new ContentValues();
newC.put(DB.column_name, "Селёдочка");
Uri uri = getContentResolver().insert(SHOP_LIST_URI, newC);
getContentResolver().query(GEN_URI, null, null, null, null);
myAdapter.notifyDataSetChanged();
If I set flag SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER - ListView don't update.
If I set flag SimpleCursorAdapter.FLAG_AUTO_REQUERY- ListView update.
What do?

You can tryed myadapter.notifyDataSetChanged();

You must notify the adapter that data has changed and tell it to refresh.
adapter.notifyDataSetChanged();
When using the flag autorequery, the cursor automatically requeris and hence, your listview is getting updated, But as far as I know, this FLAG_AUTO_REQUEY is now deprecated.
UPDATE
After some googling, I found out that notifyDataSetChanged has nothing to do with SimpleCursorAdapters.
So the workaround is requers(). But as I stated it is deprecated and the new method is by changing the Cursor.
adapter.changeCursor(cursor) and send it to your adapter.
Here is the google's official documentation about requery().

Related

How to add an item to an already populated SimpleCursorAdapter?

I'm using CursorLoader to fetch categories from a content provider. The data is being populated in SimpleCursorAdapter and displayed in a dropdown menu (spinner).
The thing is, I need to add an additional item to the list that will be displayed first.
How can I do that?
Here is the code:
CursorLoader cl = new CursorLoader(Main2Activity.this,
ContentProvider.createUri(Category.class, null), null, null, null,
"name ASC");
Cursor cursor = cl.loadInBackground();
final SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(Main2Activity.this,
android.R.layout.simple_spinner_item,
cursor,
new String[]{"name"},
new int[]{android.R.id.text1},
0);
cursor.close();

I want to update my listview once i add data in SQLite

Hi every one I am working on SQLite.when i add the data listview doesnt get updated
String[] from = { helper_ob.FNAME, helper_ob.LNAME };
int[] to = { R.id.tv_fname, R.id.tv_lname };
cursor = adapter_ob.queryName();
#SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
here I am inserting the data to SQLite.
public long insertDetails(String fname, String lname) {
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.FNAME, fname);
contentValues.put(openHelper_ob.LNAME, lname);
opnToWrite();
long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
contentValues);
Close();
return val;
}
i am following
http://androidtuts4u.blogspot.in/2012/11/android-sqlite-and-listview-example.html this given tutorial .
Any suggestion is appreciated.
But when I run same code 2nd time it shows added data.
Its not clear, at what stage of the view creation you are loading the data. Its worth you try calling the notifyDataSetChanged after loading the data. Its basically notifies the attached list that the underlying data has been changed and any View reflecting the data set should refresh itself.
cursorAdapter.notifyDataSetChanged()
Hope this helps

How to: Populating views in a layout from a single record returned by a cursor without using list view

I have an activity page which has textview fields(name , phoneNo) and some buttons. I query the database to get the details of a single customer(1 record). A cursor is returned. I want to show the values(name , phoneNo) returned in the textviews of this activity. How can I do that without using a listView. I don't need a list because only a single record is returned. It's like a profile page. But to use a cursor I think I need somekind of listView. How to do this ???? Thankz in advance
String[] fromColumns = {TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_RequestID,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_Destination,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_CustomerName};
int[] toViews = {R.id.textView1, R.id.textView3,R.id.CustomerName};
TakenJobDataBaseOpenHelper jobDatabaseHelper = new TakenJobDataBaseOpenHelper(this);
Cursor cursor = jobDatabaseHelper.getSingleRecord(id);
SimpleCursorAdapter cursoradapter = new SimpleCursorAdapter(this,
R.layout.activity_job_details, cursor, fromColumns, toViews, 0);
you don't have to use a listview. simply use it like this:
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex('NAME'));
String phoneNo = cursor.getString(cursor.getColumnIndex('phoneNo'));
((TextView)findViewById(R.id.textView1)).setText(name);
((TextView)findViewById(R.id.textView2)).setText(phoneNo);
} else {
// nothing found!
}

Android query sqlite database

What am I doing wrong!?!??
I am trying to get a set of data to a listview.
First I Open the database and then I trying to get the set,
I get a response but only 1 row from the database and I am getting at least 10 when I trying it in sqlite browser.....
Anyway I don't know if this make any sense, here is the code: (i am new at this, please don't laugh to much)
And btw I use the same methods/functions in another listview but then I don't have any WHERE in my query and that works fine....
So I want to get all the rows from the database and i only get the first one :)
Thanks mates!
db.openDataBase();
Cursor c = db.getCoursesFromCountyID(countyID);
BindsimpleCursorAdapter(c);
db.close();
the getCoursesFromCountyID =
int id = Integer.parseInt(county);
return db.query("Courses", new String[]{KEY_course_ID, KEY_course}, KEY_county_ID + " = " + id, null, null, null, null);
And the BindsimpleCursorAdapter looks like this =
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.simpleadapter_courses, c, new String[]{DataBaseHelper.KEY_course_ID, DataBaseHelper.KEY_course}, new int[]{R.id.courseID, R.id.course});
ListView lv = new ListView(this);
lv = (ListView)findViewById(R.id.listViewCourses);
lv.setAdapter(adapter);
I don't see any problems on your code, try adding 'KEY_county_ID' to you projection argument and removing the selection argument to check if all the rows are really there.

GridView with images from DataBase

I have images in my DB and I put it to my GridView by this code:
public void setNotes()
{
String[] columns = {NotesDbAdapt.KEY_ID, NotesDbAdapt.KEY_IMG, NotesDbAdapt.KEY_NAME, NotesDbAdapt.KEY_DATE, NotesDbAdapt.KEY_TIME};
String table = NotesDbAdapt.NOTES_TABLE;
Cursor c = MainNote.mDB.getHandle().query(table, columns, null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.file_dialog_row,
c,
new String[] {NotesDbAdapt.KEY_IMG, NotesDbAdapt.KEY_NAME, NotesDbAdapt.KEY_DATE, NotesDbAdapt.KEY_TIME},
new int[] {R.id.img, R.id.txt, R.id.date, R.id.time});
adapter.setViewBinder(new NotesBinder());
gridview.setAdapter(adapter);
}
All ok, but but the scrolling is slow, jerky. Seems that information takes from DB every time. How to fix it?
This method was deprecated in API level 11. Use the new CursorLoader class with LoaderManager instead. It can perform the cursor query on a background thread so that it does not block the application's UI.

Categories

Resources