The Scenario: I have a ListView that, when a child is clicked on by the user, sets the value of the TextView inside of the ListView through the main activity.
I have a FloatingActionButton that adds and subtracts an object to count the amount of items in the ListView.
To add or subtract the amount of children the ListView shows, i call notifyDataSetChanged()
.
I'm trying to set the text entered back into the TextView after notifyDataSetChanged()
is called
The Problem: notifyDataSetChanged()
resets the TextView back to blank after
it is finished running.
What I've Tried: I'ved tried saving all the values within the ListView items into a separate String[] savedItems
and set the text within the TextView. I've run the code step by step within the debugger and see that the code is saving the text and actually setting the text but for some reason, notifyDataSetChanged()
has not completed its job.
How do I set the text after notifyDataSetChanged()
has finished?
Code in Question:
public void addNewThought(int numberOfThoughts) {
String[] savedDistortionsList = saveDistortions(); // This saves each childs text
selectedItems[numberOfThoughts] = new ArrayList<String>();
IntegerNumberOfThoughts.add(Integer.toString((numberOfThoughts++)) ); // Just the variable that counts the number of children needed in ListView
distortionsAdapter.notifyDataSetChanged(); // Refresh ListView
setBackDistortionsList(savedDistortionsList); // Put back text into ListView
}
saveDistortions()
works so I don't think it's necessary to show but here is the code to setBackDistortionsList()
which I've tested and it actually is setting the TextView in the ListView but it just is not showing for some reason.
setBackDistortionsList
private void setBackDistortionsList(String[] savedDistortionsList) {
View tempView;
TextView tempTextView;
for (int i = 0; i < savedDistortionsList.length; i++) {
tempView = LDistortions.getChildAt(i);
tempTextView = (TextView) tempView.findViewById(R.id.textEntry);
tempTextView.setText(savedDistortionsList[i]);
}
}
The answer was found here: How do I check when my ListView has finished redrawing?
Copy & Paste from the user @Petro
Hopefully this can help:
Perform code on update (getLastVisiblePosition()
)
mListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mListView.removeOnLayoutChangeListener(this);
Log.e(TAG, "updated");
}
});
mAdapter.notifyDataSetChanged();