in my project I need to refresh the listview after some changes.
I use the following code:
#Override
protected void onResume(){
super.onResume();
this.onCreate(null);
}
When I run the project, the logcat returns this error:
java.lang.RuntimeException: Performing pause of activity that is not resumed: {it.xxx.yyy/it.xxx.yyy.TabsActivity}
Any idea?
You can not call onCreate method in onPause. Its affect to Android lifecycle.
You must to do:
adapter.notifyDataSetChanged();
To force an update (where adapter must be tour adapter variable)
If the requirement is just to refresh the List View, you can just call the below adapter method in. This would refresh the list view and of course you need to update the List with the changed values before calling this.
#Override
protected void onResume(){
super.onResume();
cursorAdapter.notifyDataSetChanged()
}
Related
I am developing an android application in which I am using one base adapter and displaying data in list form.Now what happened If I remove any object from list and if I call notifyDataSetChanged, its not working as expected. I tried this in following manner.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
shareDataList = getData();
shareListAdapter = new SharedHistoryListAdapter(this, shareDataList);
sharedList.setAdapter(shareListAdapter);
}
#Override
protected void onResume()
{
super.onResume();
// after deleting data it is coming inside onresume ...
shareDataList = getData();
shareListAdapter.notifyDataSetChanged();
}
I checked data size inside my activity and inside adapter as well. So in activity it is showing count as expected but inside adapter it is not showing updated count. Am I missing something? Need some help.
You cannot change the reference of the list after setting the list to the adapter. so remove the line
shareDataList = getData();
and replace with
shareDataList.clear();
shareDataList.addAll(getData());
and now invoke the shareListAdapter.notifyDataSetChanged();
I think, in getData() method gets every time new ArrayList object.
You should simply call
shareListAdapter.notifyDataSetChanged();
it will work perfectly
I want to perform one data base Operation once. I want to do this when My Activity is Visible. Where shall I puty my LoadDatabase() function
LoadDatabase();
this is my oncreate of activity
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.retrospectscan);
}
this is my onStart
#Override
protected void onStart()
{
super.onStart();
}
Where Shall I put my LoadDatabase Code ? So that It will operated only if activity is fully Visible.
If Any other Approach is there please help me.
The complete activity lifecycle is here:
Though loading from database may be lengthy task , you can try doing it in AsyncTask or in onStart.
You can also use it on onResume. This depends on your application use.
user2737044
use Application context and load your database in application context create().
2nd thing is that, In activity onCreate() call first then it will call onstart().
Here is the code:
#Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
Log.d("onResumeMethod", "It is called! :)");
}
}
I am using this to refresh list when anything is edited in the database. Unfortunately it is not working. Somewhere I read I should use requery but it is already deprecated. Any advice?
EDIT:
Tried already this, but did not help(unfortunately, because I was convinced it is gonna work):
#Override
public void onResume() {
super.onResume();
adapter = new ArrayAdapter<Friend>(this,
android.R.layout.simple_list_item_1, list);
this.getListView().setAdapter(adapter);
this.getListView().invalidate();
}
I found another problem: If I use back button (on emulator) to go back to desktop and try to turn on an application by clicking on a menu item it shows me this:
03-19 15:43:50.153: E/AndroidRuntime(7374): FATAL EXCEPTION: main
03-19 15:43:50.153: E/AndroidRuntime(7374): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.birthdayreminder/com.example.birthdayreminder.MainActivity}:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is
'android.R.id.list'
I faced this problem before so they adviced me to insert this to my xml:
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ListView>
You need to get your data from database again. Then you can set the adapter again in your listview and call invalidate(), like the following method:
#Override
public void onResume() {
super.onResume();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, db.getMyData());
myListView.setAdapter(adapter);
myListView.invalidate();
}
Where db.getMyData() is your method to get your data from the database, and myListView is your ListView.
Hope it helps!
Override notifyDataSetChanged :
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
myArray.clear();
/* load the data again */
myArray = yourMethodToLoadData();
}
I want to call a listview adapter from another activies after insert record to sqllite db.
I cant call the adapter.notifyDataSetChanged() method. What is the best way to do it.
public void onClick(View v) {
TextView item_name;
item_name=(TextView) findViewById(R.id.eItemName);
Log.d("Insert: ", "Inserting ..");
db.add_item(new Item(item_name.getText().toString()), getApplicationContext());
// I want to call the method HERE!!!!
finish();
}
Just
After finish() Current Activity, Call adapter.notifyDataSetChanged() from onActivityResult()in previous Activity (As I assumes your Listview in this previous Activity).
Also start your Current Activity from Previous Activity using startActivityForResult();
Or if you have reference of List Adapter in this Activity, then using that reference call adapter.notifyDataSetChanged(). (But I think its not a good practice)
The better option would be re-loading your ListView with new data inside onResume() of your Activity where you have ListView. There is no need to call notifyDataSetChanged() from another class as you don't have your List visible in your another Activity.
#Override
protected void onResume() {
super.onResume();
//fill your Collection with new data
// setAdapter or notify your adapter
}
I've got a ListView, an ArrayList. From activity1, I pass some data, e.g a price and a name of a company. This data will be stored in an ArrayList of Hashmaps. When I first passes some data to the second activity which holds the ListView the data will be found in this list, but when I go back to the first activity and pass some information again, the old data will still be in the list. Is there any possible way I can prevent this to happend? For instance, when I return from activity2 to activity1, the ListView will be wiped.
Thanks!
what you need to do:
on the activity which holds the listView - override the onResume() method:
#Override
protected void onResume() {
super.onResume();
mArrayList.clear();
mAdapter.notifyDataSetChanged();
}
if it's not good for you to clear the array list, so you can do the following:
#Override
protected void onResume() {
super.onResume();
mListView.removeAllViews();
}