I had a ListView that loads data from the SQLite database and onClickListener for each list that opens up to a new activity. My problem now is that when I press "Back" to go back to the previous activity with the ListView, it doesn't show.
How do I reload the list from database again? How do I go about it?
Does it have anything to do with notifyDataSetChanged()?
Please help.
Thank you.
EDITED:
Below are my codes to load the ListView under onCreate():
ListView listContent = (ListView) findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
int[] to = new int[] { R.id.text };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
//Onclick ListView setlistener
listContent.setTextFilterEnabled(true);
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent summaryIntent = new Intent(DocumentListActivity.this, ViewDocumentActivity.class);
listTopic = ((TextView) view).getText().toString();
summaryIntent.putExtra("SummTopic", listTopic);
startActivity(summaryIntent);
}
});
So what to add in the Resume()?
You can put your code to fetch data from database and adding it to ListView inside onResume(). So when you jump back to your previous Activity its onResume() will be called and your ListView will be loaded with the data from database.
Related
I use SimpleCursorAdapter build a list. And I want to start another activity when I click one of the rows. code as following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);
listView = (ListView) findViewById(R.id.user_list);
db.open();
fillData();
.......
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor selectedItem = (Cursor) listView.getAdapter().getItem(position);
Log.d("#####", selectedItem.getCount() +"");
// return total number of the cursor using to build the list.
selectedItem.moveToFirst();
.......
}
});
}
private void fillData() {
Cursor c = db.fetchAllSenders();
startManagingCursor(c);
String[] from = new String[] {PeerContract.KEY_NAME};
int[] to = new int[] {R.id.user_list_row_name};
adapter = new SimpleCursorAdapter(this, R.layout.user_list_row, c, from, to, 0);
listView.setAdapter(adapter);
}
But int the LogCat Log.d("#####", selectedItem.getCount() +""); always return the total number of rows in cursor used to build the list. For example, there are 4 rows in the list, after I click one row in the list, the LogCat will display #####:4. But it supposes to be 1 when I click just one item.
Thanks in advance.
It is coming 4 because there is only one cursor reference with the adapter. the method getAdapter().getItem(position) will return you the same cursor object but it will move the cursor to the position for which you requested getItem().
So whenever you call getItem(position), it will not give you a new cursor object, it will give you the same cursor object, moving it to the current position
Hello everyone and thanks in advance!
I have a listview that displays a list of name from the sqlite database. What I want to know is how can I make it so that when I click on an Item in the list, it takes me to a new activity which displays the rest of the columns in the Sqlite Table?
This is the method I used to bind the listview with the cursor:
public void displayListView() {
Cursor cursor = dataSource.findAll();
String[] columns = new String[]{
DBHelper.CHARACTERATTRIBUTES_NAME,
DBHelper.CHARACTERATTRIBUTES_NICKNAME,
DBHelper.CHARACTERATTRIBUTES_NOTES};
int[] to = new int[]{
R.id.name,
R.id.nickname,
R.id.notes};
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, R.layout.character_info, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
i have a list view filled with data coming from database, i want when i click on an item to
disappear, i used --list.remove(position)-- but i have an error add cast to list.
HERE the list view :
private void populateListViewFromDB() {
Cursor cursor = db.getAllRecords();
startManagingCursor(cursor);
String[] databaseColumnNames = new String[] { DBAdapter.col_Region };
int[] toViewIDs = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_1, cursor,
databaseColumnNames, toViewIDs, FLAG_REGISTER_CONTENT_OBSERVER);
final ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(myCursordapter);
HERE my code when i want to delete an item :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> arg0, View arg1,
final int position, long arg3) {
list.remove(position);
adapter.notifyDataSetChanged();}
There is no ListView.remove() method. You should remove correspondent item from the data model and then refresh your adapter.
In your OnItemClickListener listener get the bound data item:
Object itemObj = adapter.getItem(position);
Then, delete this item from your DB.
Then get new instance of your cursor and update your cursor adapter:
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD){
adapter.swapCursor(newCurosr);
} else {
adapter.changeCursor(newCursor);
}
Then, notify your list view about data changes:
adapter.notifyDataSetChanged();
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));
}
This is my ListView structure and element :
// Get a cursor with all phones
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
startManagingCursor(c);
/** ----Display the Contacts on the device----- */
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);
and my implementation of the setOnItemClickListener is as follows:
/** ----Defining the ItemOnClickListener for the displayed List---- */
final ListView contactlistview = getListView();
contactlistview.setTextFilterEnabled(true);
contactlistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG, String.valueOf(position));
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
contactlistview.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
Log.d(TAG, contactlistview.getItemAtPosition(position).toString());
}
});
and finally got the ListView as :
and when i clicked on any of the item i get the Toast Displaying the Following:
but i want my toast to display the contact name of the selected item
can u pls help me with the code :)
sorry for the inconvenience :)
THANKS :) :)
You are printing the value of the object here.
Now try like this:
Cursor c = ((Cursor) parent.getAdapter().getItem(position));
Toast.makeText(getApplicationContext(),
c.getString(Phone.NAME),Toast.LENGTH_SHORT).show();