ListView wont refresh when calling requery() or notifyDataSetChanged - android

So I have a database being displayed within a listView. When I delete an item, I'm trying to get the list to update and remove the item. I have all the code and am receiving no errors. I just doesn't refresh the list.
Here's what I have:
public void Requery() {
db.open();
final Cursor c1 = DBAdapter.getAllRecords();
startManagingCursor(c1);
String[] from = new String[] { DBAdapter.KEY_ITEM };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row,
c1, from, to);
setListAdapter(notes);
c1.getCursor().requery(); //trying to refresh list
db.close();
}
I have also tried:
public void Requery() {
db.open();
final Cursor c1 = DBAdapter.getAllRecords();
startManagingCursor(c1);
String[] from = new String[] { DBAdapter.KEY_ITEM };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row,
c1, from, to);
setListAdapter(notes);
notes.notifyDataSetChanged(); //another way that still didn't work
db.close();
}
Am I missing something here?

Related

Empty ListView when EditText is empty

I have code which takes data from SQLite cursor and puts into listview.
private void displayListView() {
cursor = myDatabase.getJoinedInfo(etSearch.getText().toString().trim());
String[] columns = new String[] { "re_value", "g_value", "ke_value" };
int[] to = new int[] { R.id.tvHiragana, R.id.tvMeaning, R.id.tvKanji };
dataAdapter = new SimpleCursorAdapter(this, R.layout.wordonlist,
cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lvWordlist);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
When EditText is empty, it is taking first 10 lines. How to show empty ListView when EditText is empty?
Do something like this :
If(yourEditText.length() == 0)
yourListView.setAdapter(null);
Or you can use :
If(yourEditText.length() == 0)
yourAdapter.clear();
do not set the adapter until EditText is empty. You can set the adapter after there's some data in EditText.
Try this
private void displayListView() {
if (etSearch.getText().toString().trim().equals("")) {
listView.setAdapter(null);
} else {
cursor = myDatabase.getJoinedInfo(etSearch.getText().toString()
.trim());
String[] columns = new String[] { "re_value", "g_value", "ke_value" };
int[] to = new int[] { R.id.tvHiragana, R.id.tvMeaning,
R.id.tvKanji };
dataAdapter = new SimpleCursorAdapter(this, R.layout.wordonlist,
cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lvWordlist);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
}

Listview and SimpleCursorAdapter

I'm trying to display a ListView of all contacts and enable the user to select multiple records. I want to use ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) but I'm not sure how to apply it here. The code below pulls up a list of contacts but I can't select from it.
Any tips very much appreciated
Cheers
public class addContacts extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_contacts);
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID };
int[] views = new int[] { R.id.contactName, R.id.contactID };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.add_contacts, c, columns, views);
this.setListAdapter(adapter);
}
}
Since you are using a ListActivity you can fetch the ListView with getListView(), use:
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
But I noticed that you are using the layouts incorrectly:
setContentView(R.layout.add_contacts);
...
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.add_contacts, c, columns, views);
Understand that:
The layout passed to setContentView() should have a ListView with the id android:id="#android:id/list".
The row layout passed to SimpleCursorAdapter should never have a ListView... You are trying to create a ListView with ListViews on every row.
Try using built in layouts like this:
public class addContacts extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID };
int[] views = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c, columns, views);
this.setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}

ListView not working after i changed its table. Android

i have created a listview to show all the contents of a database on a screen. the code i am using is this
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = datasource.getAllActs();
startManagingCursor(c);
String[] from = new String[] {DataBaseHelper.KEY_DATE,
DataBaseHelper.KEY_STEPS,DataBaseHelper.KEY_CALs };
int[] to = { R.id.code, R.id.Days, R.id.BMI };
SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
which worked beofre with the code below.
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = datasource.getAllGoals();
startManagingCursor(c);
String[] from = new String[] {DataBaseHelper.KEY_GOAL, DataBaseHelper.KEY_Current,DataBaseHelper.KEY_Target };
int[] to = { R.id.code, R.id.Days, R.id.BMI };
SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
i'm just wondering if there is something wrong with this code that i may be missing. i have the logcat if you need it
The only thing I can think of is that your new Cursor doesn't have a column named _id. Make sure that the cursor,
Cursor c = datasource.getAllActs();
has a column named _id, as the SimpleCursorAdapter class requires it.

list view from database

I am using database in my application,in that i have fetched data from database,but i dont know how to display it in the textview.
Here is my code:
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
db.open();
Cursor c = db.getAllTitles();
startManagingCursor(c);
String[] from = new String[] { db.KEY_INCOME };
int[] to = new int[] { R.id.textView1 };
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.report, c, from, to);
Here the notes have the database value.But don't know how to proceed how to display it in listview.
do like this
String[] titles= new String[] {c.TITLE };
int[] view = new int[] { R.id.textview };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, c, titles, view );
listview.setAdapter(notes);
You need to find your listview and set your adapter. (Assuming you are not using a ListActivity)
ListView list = (ListView) this.findViewById(R.id.yourlistview);
list.setAdapter(notes);
If you are using a ListActivity (or ListFragment), it's even simpler as the framework assumes you are using a certain id and you can just set the adapter like so:
setListAdapter(notes);
if you want to set set Tesxt on list item click you can do iy like
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) arg0.getAdapter().getItem(arg2);
tv.setTextt(c.getString(1));
}

SimpleCursorAdapter and ListView problem

I have a problem displaying an records from my db in listview. This is my code
public class FirstTab extends ListActivity {
private DatabaseHelper dbhelper;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.first);
dbhelper = new DatabaseHelper(getApplicationContext());
Cursor cursor = dbhelper.getAllNotes();
startManagingCursor(cursor);
String[] columns = new String[] {DatabaseHelper.colTitle , DatabaseHelper.colDate};
int[] to = new int[] { android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
setListAdapter(mAdapter);
}
}
and
...
public Cursor getAllNotes()
{
SQLiteDatabase db=this.getReadableDatabase();
return db.query(noteTable, new String [] {colTitle, colDesc, colDate}, null, null, null, null, null);
}
...
if you need more , here is repo https://github.com/grzegorz-l/myHomeworks
When i start my app it crash at the beggining (RuntimeException). If I comment 2 last lines in onCreate method it run but ofc doesn't show anything.
Thanks in advance
Greg
Don't pass getApplicationContext() when creating SimpleCursorAdapter. Use this instead, i.e., the context of your ListActivity.
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
Your FirstTab.java file is extending a ListActivity. This means that it's layout file has to contain a ListView with android:id set to:
android:id="#android:id/list"
Also, your FirstTab.java has it's layout pointing to note_entry.xml. It should either point to a layout which has a ListView of above description in it or not be extended by a ListActivity.

Categories

Resources