Reputation: 13
I have a Runnable
class that calculates model values for my composite list views, in that runnable there's a UI thread inside of a custom thread. There I have adapter's notifyDataSetChanged()
call, but after notifyDataSetChanged()
I try updating some TextView value in the main layout. The problem is when running TextView gets updated first and only then ListViews and getting updated. That means notifyDataSetChanged()
of the Adapter
custom class gets updated last which is not suitable for me. Is there any possibility to synchronize those screen updates?
Here's the sample code:
public class TestRunnable implements Runnable {
private TestAdapter adapter;
@Override
public void run() {
Context.runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
MainActivity.setTextViewValue("Something...");
}
});
}
}
public class TestAdapter extends ArrayAdapter<TestModel> {
@Override
public View getView(final int position, View view, ViewGroup parent) {
View row = view;
TestHolder holder;
Boolean rowIsNew = false;
if (row == null) {
rowIsNew = true;
LayoutInflater layoutInflater = ((Activity) context)
.getLayoutInflater();
row = layoutInflater.inflate(layoutResourceId, parent, false);
holder = new TestHolder();
...
row.setTag(holder);
} else {
holder = (TestHolder) row.getTag();
}
TestModel testModel = data.get(position);
holder.property = testModel.property;
...
if (rowIsNew) {
holder.....setTypeface(...);
holder.....setTypeface(...);
}
return row;
}
}
Upvotes: 0
Views: 83
Reputation: 8180
I presume your MainActivity.setTextViewValue("Something...");
line is trying to print some data from the adapter and you're getting the old value, is that so?
I'm not 100% sure of this, perhaps someone else can help confirm this, but I think notifyDataSetChanged()
only marks the current data on the adapter as dirty, so the adapter will know that it has to refresh the data, but it doesn't do it immediately when you do the call.
EDIT: If my first paragraph is correct, you should try to update the text view with data from the data source instead of the adapter, this would be a nicer way to solve the problem.
Upvotes: 0
Reputation: 3392
I have revised the source code of ArrayAdapter and I see no way of executing code after it has called the onChanged()
on it's observer so my answer would be:
onChanged()
being calledListView.setAdapter
with a brand new adapter with the new datasetP.S. Number 1 is the optimum solution but number 2 is the easy solution, depending on your time and performance requirement use what you need, but I recommend taking some time and implementing Number 1.
Upvotes: 1