i'am getting my Listview data from an sqlite database like this
ArrayAdapter<String> aa;
ArrayList<String> arrayList = new ArrayList<String>();
aa = new ArrayAdapter<String>(getActivity(),
R.layout.row_listview, arrayList);
setListAdapter(aa);
Cursor cursor8 = db.rawQuery("SELECT * FROM test21", null);
if(cursor8.getCount() == 0)
{
textView5.setVisibility(View.VISIBLE);
}cursor8.close();
Cursor cursor = db.rawQuery("SELECT * FROM test21", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if(cursor.moveToFirst())
{
do {
arrayList.add(cursor.getString(3));
}
while (cursor.moveToNext());
}
When i click another button it remove all the data of the sqlitedatabase.
How can i refresh the content of the listview because there is now nothing. I always need to go back in the activity each time :(
Get rid of the notifyDataSetChanged() and call requery() on your Cursor. Here is a sample project demonstrating this.
Hope it will works for you.
Related
I'm able to populate my SQLite DB and to do all CRUD operations.
Now I would populate a listview starting from:
List<Starred> data = DBAdapter.getAllUserData();
By log I can read my list:
for (Starred st : data)
Log.d("Title: ", "Title: " + st.getName());
I'm pretty sure I'm missing the Adapter but don't well know how start.
This is my getAllUserData() function:
public static List<Starred> getAllUserData() {
List<Starred> starredList = new ArrayList<Starred>();
// Select All Query
String selectQuery = "SELECT * FROM " + USER_TABLE;
final SQLiteDatabase db = open();
Cursor cursor = db.rawQuery ( selectQuery, null );
if (cursor.moveToFirst()) {
do {
Starred data = new Starred();
data.setID(Integer.parseInt(cursor.getString(0)));
data.setName(cursor.getString(1));
data.setLabel(cursor.getString(2));
// Adding contact to list
starredList.add(data);
} while (cursor.moveToNext());
}
return starredList;
I tried with
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<Starred> arrayAdapter = new ArrayAdapter<Starred>(
this, android.R.layout.simple_list_item_1, data);
lv.setAdapter(arrayAdapter);
but I get nullpointer exception.
Could anyone give me suggestion? Any help would be much appreciated.
try to use the cursor adapter pal
https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter
I am retrieving data from SQLite and trying to display it in the ListView. The problem is I have two columns in the database and the listview only shows data from one column. I am using following code for this purpose
public List<entry> getAllEntries() {
List<Entry> entries = new ArrayList<Entry>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_ENTRIES,
allEntries, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Entry entry = cursorToEntry(cursor);
Entry entry1 = cursorToEntry(cursor);
Log.d(TAG, "get entry = " + cursorToEntry(cursor).toString());
entries.add(entry);
cursor.moveToNext();
Log.d(TAG, "get amount = "+ cursorToEntries(cursor).toString1());
entries.add(entry1);
cursor.moveToNext();
}
cursor.close();
return entries;
}
And I have also been trying following code
while(cursor.moveToNext())
{
entries.add("Entry"+cursor.getString(1)+"Amount"+cursor.getString(2));
}
And thats how I am displaying it in the ListView
List<Entry> values = datasource.getAllEntries();
ArrayAdapter<Entry> adapter = new ArrayAdapter<Entry>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
You can either use a CursorAdapter, which lets you specify a row layout as well as how to map from columns in the cursor to views in the layout; or you can write your own adapter implementation that extends from BaseAdapter and is internally backed by a Cursor. I suggest you watch The World of ListView.
I am displaying data pulled from the Android OS sqlite database. I am successfully getting the items to delete when I click on them. However I am having an issue refreshing, or updating the listview after the operation.
Below is the code where I delete the contact.
deleteBtn = (Button)v.findViewById(R.id.deleteBtn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Deleted: " + c.getId() + " " + c.getName(), Toast.LENGTH_SHORT).show();
adapter.deleteContact(c.getId());
updateList();
}
});
Below is the updateList() method:
public void updateList(){
myList.refreshDrawableState();
myList.invalidateViews();
this.notifyDataSetChanged();
}
I included in this method all three ways I tried to refresh but none worked for me. Any idea how I might achieve this?
EDIT: I changed my code thinking this would be the solution but it did not work either:
DbAdapter class delete method():
public boolean deleteContact(int rowId){
getAllContactsList();
return db.delete(DB_TABLE, COLUMN_ID + "=" + rowId, null) > 0;
}
getAllContactsList():
public List<Contact> getAllContactsList(){
List<Contact> contactList = new ArrayList();
Cursor c = db.query(DB_TABLE, new String [] {COLUMN_ID, COLUMN_FNAME, COLUMN_LNAME}, null, null, null, null, null);
//loop through cursor rows and add to list
if(c.moveToFirst()){
do{
Contact contact = new Contact();
contact.setId(Integer.parseInt(c.getString(0)));
contact.setfName(c.getString(1));
contact.setlName(c.getString(2));
contactList.add(contact);
}while(c.moveToNext());
}
return contactList;
}public List<Contact> getAllContactsList(){
List<Contact> contactList = new ArrayList();
Cursor c = db.query(DB_TABLE, new String [] {COLUMN_ID, COLUMN_FNAME, COLUMN_LNAME}, null, null, null, null, null);
//loop through cursor rows and add to list
if(c.moveToFirst()){
do{
Contact contact = new Contact();
contact.setId(Integer.parseInt(c.getString(0)));
contact.setfName(c.getString(1));
contact.setlName(c.getString(2));
contactList.add(contact);
}while(c.moveToNext());
}
return contactList;
}
I thought by getting a new cursor before deleting the contact It would update the list accordingly. Unfortunately It made no difference. Any ideas ?
You have to notify the list's adapter that you have modified the underlying data.
Try using adapter.notifyDataSetChanged();
It seems that you have the Database Adapter class, but you're missing the ArrayAdapter class, which is intended to manage the list of items, displayed in your ListView. Take a look at this example, specifically at the WeatherAdapter.java class.
If I am wrong in my assumptions and the adapter object in your code is not a database adapter, but an ArrayAdapter class, try putting adapter.notifyDataSetChanged() instead of this.notifyDataSetChanged().
Let me know if this helped.
You must requery and and generate new list:
public void updateList(){
clear();
addAll(contactsDbHelper.getAllContactsList()); //addAll works since 11 API version.
notifyDataSetChanged(); //need this is you dissabled auto notify
}
P.S. You should done this job with using Content providers and CursorAdapter, but you need manually notify content provider about changes, because Cursor.requery() is deprecated since 11 version.
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.
I am trying to populate a spinner with data from a database. The code is in a subroutine. Everything executes but the spinner is empty. If I copy the code to the "onCreate(Bundle saveInstanceState) it works fine. I am thinking I have context issue but can not figure it out.
Here is the subroutine:
public void showMatchNames (){
Cursor c;
String sMatchName = "MatchTable";
c = db.query(sMatchName, new String[]{KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
if(c.moveToFirst()){
SimpleCursorAdapter MatchAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, c, new String[]{KEY_TITLE}, new int[]{android.R.id.text1});
MatchAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin2 = (Spinner)this.findViewById(R.id.spinner2);
spin2.setAdapter(MatchAdapter);
}
scrMatchView.setOnItemSelectedListener(new mySpinnerListener());
c.close(); // Done with Cursor so close it
}
There is data in the database, confirmed by exporting to SQliteMan and doing a query.
I can also pull the names from the database and populate the spinner with a simple ArrayAdapter from the "showMatchNames" subroutine.
I am pretty sure it is something simple but I have been staring at this for hours now and can't get it to work.
Any help would be very welcome.