Android ListView not updating after a call to notifyDataSetChanged - java

I have an Activity which I start like this:
public class MyProblemsActivity extends ListActivity
{
String[] PROBLEMS = new String[] {"One", "Two", "Three" };
ArrayAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
adapter = new ArrayAdapter<String>(this,R.layout.my_problems, PROBLEMS);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
and that works totally fine.
The problem I run into is after a call to a remote server via a Asynch call, I do this:
#Override
protected void onPostExecute(String result)
{
PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };
adapter.notifyDataSetChanged();
}
But the screen does not update. What am I doing wrong here and how can I get the screen to update with the new values?
Thanks!!

what's happening is at
PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };
the PROBLEMS is getting the reference to new string array object...Thus the old reference is remaining as it is(unchanged)..
to correct it, use following :
PROBLEMS.clear();
List<String> newlist = new ArrayList<String>();
newlist.add(..);
..
PROBLEMS.addAll(newlist);
adapter.notifyDataSetChanged();
this way new string(s) will be added only to existing array reference pointed out by PROBLEMS
NOTE: i have mentioned by referencing the use of arraylist of string instead of string[] , to use clear(),addAll() functionalites of it, you can modify it for String[] as per your use..

If you want a simpler answer, you can just change your code to look like this:
#Override
protected void onPostExecute(String result)
{
PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };
adapter.clear();
adapter.addAll(PROBLEMS);
adapter.notifyDataSetChanged();
}
This changes the ArrayAdapter's contents to have the Strings in your new String[], then notifies the ListView that the ArrayAdapter has new contents. This will cause the ListView to update and show your new Strings.

I read that you updated the list from an asynchronous callback. Same was my case. I went to run that in UI Thread and suddenly it worked as expected! (Usually you get an exception if you don't do that. Here it just doesn't do refresh the list.) For my opinion, the other answers are just work arounds.
So, alter your code like this:
#Override
protected void onPostExecute(String result)
{
runOnUiThread(new Runnable() {
#Override
public void run() {
PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };
adapter.notifyDataSetChanged();
}
});
};

Related

Issue with items of listview

i don't know why but when i add items to listview i get only the last item. E.G. if i write apple and than pear i get only pear and not apple and pear.
Why?
CODE:
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
//holder.lw.setA
String commento= (holder.tw.getText().toString());
// holder.lw.setAdapter(adapter);
ArrayList<String> arrayList= new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, arrayList);
arrayList.add(commento);
// next thing you have to do is check if your adapter has changed
holder.lw.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println("LISTVIEW:"+arrayList);
}
});
in every click you decalre new list ArrayList<String> arrayList= new ArrayList<String>(); and it's false
you need to declate it out of the listener and in your listener just add the new items and use adapter.notifyDataSetChanged();
You are creating new list and adapter during every click so move the declaration and initialization of both, outside that function
Declare them outside getView(if Base or ArrayAdaoter) or createViewHolder (RecyclerAdapter)
initialize them inside constructor
add data to list and notify adapter
Better use view holder pattern in here where you use Existing instance of listview rather than recreating the new one all the time.
You don't need to do anything special, inside of onClick you need to get a reference to your ArrayAdapter and call the method add(T...) on it.
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
String commento= (holder.tw.getText().toString());
ArrayAdapter adapter = (ArrayAdapter) holder.lw.getAdapter();
adapter.add(commento);
}
});
No need to even call notifyDataSetChanged because add already does that internally.
if you use this you can solve your problem
yourAdapter.(getCount()-1-position);
and this if you want to see the last items in front
public yourPoJo getItem(int position) {
return super.getItem(getCount()-1-position);
}

The recyclerview crashes when I update it

Please don't mark it as duplicate, as the solutions provided yesterday did not work.
I have populated a recyclerview, and it is working fine, albeit it loads a bit slowly.
However, say when I add a new name and update the recyclerview it crashes with the NullPointerException.
Below are the classes and code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainOnes);
recyclerView = (RecyclerView) findViewById(R.id.recycleOnes);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
adapter = new MusicRecyclerAdapter(list);
recyclerView.setAdapter(adapter);
populateList();
}
public void populateList(){
DisplayUsersList displayUsersList = new DisplayUsersList();
displayUsersList.execute();
}
public class DisplayUsersList extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
...
}
#Override
protected void onPostExecute(String postData) {
try {
list.clear();
JSONArray jsonArray = new JSONArray(postData);
for(int i=0; i < jsonArray.length(); i++){
String forename = jsonArray.getJSONObject(i).getString("forename");
String surname = jsonArray.getJSONObject(i).getString("surname");
UserDetails userDetails = new UserDetails(forename, musicName, surname);
list.add(userDetails);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public class AddUsers extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
...
}
#Override
protected void onPostExecute(String postData) {
populateList();
}
}
I think it is crashing because of the populateList() method call because I have already initialised everything
My logcat:
java.lang.NullPointerException
at georgezs.userdas.UserDBGUI$DisplayUsersList.onPostExecute(UserDBGUI.java:218)
at android.os.AsyncTask.finish(AsyncTask.java:741)
at android.os.AsyncTask.access$600(AsyncTask.java:197)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:654)
at android.os.Handler.dispatchMessage(Handler.java:100)
Line 218 is adapter.notifyDataSetChanged();
I tried instantiating the recyclerview in onPostExecute but it keeps crashing. My code works in the sense that it actually loads the data and displays it in a recyclerview. It just doesn't redisplay or reload when I call the populateList() method again.
This bug has been bugging me in my sleep literally - so any help and solutions would mean a lot to me
EDIT: I THINK I KNOW EXACTLY WHERE THE ERROR IS - IT IS IN THE POSTEXECUTE METHOD IN ADDUSERS CLASS.
Thanks
Just check if the adapter is not null before calling notifyDataSetChanged():
if (adapter != null) {
adapter.notifyDataSetChanged();
}
This happens because the AsyncTask executes asynchronously and by the time it finishes, the adapter might not be available anymore.
One option you could try is, instead of using
adapter.notifyDataSetChanged();
replace with
recyclerView.getAdapter().notifyDataSetChanged();
This is assuming for whatever reason, you have got your adapter set to null, but not shown in the code clearly above, while your recyclerview is still fully intact.
p/s: I also didn't see you update your latest retrieved data and insert into the adapter. You might want to check that as well.
You're handling variables outside the scope of your main class in a Threaded class which can always be sketchy. Granted AsyncTask is supposed to synchronize operations for you, I'm always cautious
Try passing your list and adapter to your DisplayUsersList class e.g.
DisplayUsersList displayUsersList = new DisplayUsersList(list, adapter);
displayUsersList.execute();
And in your DisplayUsersList make sure the reference of these is explicit e.g.
public class DisplayUsersList extends AsyncTask<String, String, String> {
private final List<UserDetails> list;
private final Adapter adapter;
public DisplayUsersList(List<UserDetails> list, Adapter adapter) {
this.list = list;
this.adapter = adapter;
}
...
Also, try moving the populateList(); to OnStart rather than onCreate
As although the variables are assigned they may still be initialising.
Edit
Could you try moving the call to the top level class e.g.
public void dataSetChangedCallback() {
adapter.notifyDataSetChanged();
}
And in your DisplayUsersList change adapter.notifyDataSetChanged(); to
...
dataSetChangedCallback();
...

How to get Notified when Method is Called on Object that does not Implement Observable?

I need to somehow know when the adapter of a ListView instance changes.
I was thinking of adding an observer to the listView object, but it does not implement the Observable interface.
ListView listView;
listView.addObserver(...); // method does not exist!
Is there any other way I can know when the adapter of a list view changes? e.g. the setAdapter() method is called... ?
--- UPDATE ---
Here's an code example:
final ListAdapter firstAdapter = new SimpleAdapter(
this,
new ArrayList<Map<String, Object>>(),
android.R.layout.simple_list_item_1,
new String[] {"AAA"},
new int[] {android.R.id.text1}
);
final ListAdapter secondAdapter = new SimpleAdapter(
this,
new ArrayList<Map<String, Object>>(),
android.R.layout.simple_list_item_1,
new String[] {"AAA"},
new int[] {android.R.id.text1}
);
setListAdapter(firstAdapter);
(new Timer()).schedule(new TimerTask() {
#Override
public void run() {
Runnable r = new Runnable() {
#Override
public void run() {
setListAdapter(secondAdapter);
}
};
new Handler(Looper.getMainLooper()).post(r);
}
}, 1000);
My question is: How can the firstAdapter know when it is detached from being the listView's actual adapter (because secondAdapter replaces it)?
Is this what you are looking for? You can set an observer on the adapter.
registerDataSetObserver
Turns out the Adapter gets notified when it's deregistered from the ListView, so I am using this:
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
super.unregisterDataSetObserver(observer);
Log.d("Adapter just got unregistered from the listView!");
[...]
}

Keeping list entries when I move activity

I have a ListView.
I populate this list from 2 editTexts
When I move activity and go back to it the entries are gone again.
I kind of understand why this is but dont know how to correct it.
ListView lv2 = (ListView) findViewById(R.id.listView2);
final SimpleAdapter simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String[]{"planet"}, new int[]{android.R.id.text1});
planetsList.add(createPlanet("planet", "testme"));
lv2.setAdapter(simpleAdpt);
button21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iinitList();
simpleAdpt.notifyDataSetChanged();
editText5.setText("");
editText6.setText("");
}
});
}
private void iinitList() {
String st,str;
Double db;
if (editText5.getText().toString()!= "" && editText6.getText().toString()!="") {
st = editText5.getText().toString();
str = editText6.getText().toString();
db = Double.parseDouble(str);
planetsList.add(createPlanet("planet", ""+st+
": \n" +db+""));
}
}
HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.put(key, name);
return planet;
}
As you can see I have added a value to the list manually called test also, when I move activity this stays in the list, I would love if the editText entries were to stay in there also when I move activities.
Activities can be destroyed when you navigate to a new one or rotate. This will clear anything that is only referenced by the activity, like your EditTexts. However, Android provides a nice utility for saving things you want to remain in a method called, which you can override in your activity:
#Override
protected void onSaveInstanceState(Bundle state) {
// Put your values in the state bundle here
}
#Override
protected void onCreate(Bundle savedState) {
// Load your UI elements as usual
if (savedState != null) {
// Load your state from the bundle
}
}
That same bundle will be given back to you in onCreate, where you create your UI to begin with so you can reload the state from it.
This is a really good description of how activities work:
http://developer.android.com/reference/android/app/Activity.html

String[] urls won't update correctly

For my class we have to create an application where we catch an SMS message in a broadcast receiver, get the string (assumed to be a URL), add it dynamically to a string-array which is displayed in a fragmentlist. When the list item is clicked we then have to load it into a webview in a fragment.
Everything works until here.
The problem is that the list doesn't update when I try to add the url_act string to it.
Here's my code:
public class UpdateString extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url_act = "";
Bundle b = getIntent().getExtras();
url_act = b.getString("url");
UrlListFragment uf = (UrlListFragment) getFragmentManager()
.findFragmentById(R.id.listFragment);
String[] urls = getResources().getStringArray(R.array.urls_array);
List<String> list = new ArrayList<String>();
list = Arrays.asList(urls);
ArrayList<String> arrayList = new ArrayList<String>(list);
arrayList.add(url_act);
urls = arrayList.toArray(new String[list.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdateString.this,
android.R.layout.simple_list_item_1, urls);
uf.setListAdapter(adapter);
}
If you need more code for context let me know. Updated
You really should post your logcat output just to be sure, as otherwise we don't know what kind of exception was thrown.
That being said, I bet it is a NullPointerException; findFragmentById() is most likely returning null. This is probably because you never inflate your XML resource to begin with.

Categories

Resources