What design concept to use to update the UI async - android

I'm working on an app that displays a working schedule on a time line.
This is a rough layout of how the app is designed at the moment:
The data is stored in an SQLite DB. When the Timeline (a singleton object) requests the data from the database helper class, it gets an ArrayList of Events (e.g. an Event could be a duty starting at the 1st of May 2016 at 03:00 and ending at the 3rd of May 2016 at 16:00). The Timeline then transforms these Events to TimelineItems, a class representing (part of) an Event for a particular day.
The loading of Events and the transformation of Events to TimelineItems both are done in AsyncTasks. So far so good.
Now comes the part I'm struggling with: updating the UI after a new DB fetch.
My first approach was to pass the updated ArrayList of TimelineItems to the RecyclerView adapter and let the the adapter know the data has changed with notifyDatasetChanged(). The problem with this approach is that
1) a lot of unnecessary work is being done (cause we're recalculating all Events/TimelineItems, not only the ones changed) and
2) the scroll position on the RecyclerView is reset after every DB fetch
In my 2nd approach, I've implemented some methods to check which Events/TimelineItems have changed since the last display with the idea of only changing those TimelineItems, with notifyItemChanged(). Less work is being done and no need to worry about scroll positions at all. The tricky bit is that checking which items have changed does take some time, so it needs to be done async as well:
I tried to do the code manipulations in doInBackground() and the UI updating by posting otto bus events in onProgressUpdate().
private class InsertEventsTask extends AsyncTask<Void, Integer, Void> {
#Override
protected Void doInBackground(Void... params) {
ArrayList<Event> events = mCachedEvents;
// if mChangedEvents is not null and not empty
if (events != null && !events.isEmpty()) {
// get the list of pairs for the events
ArrayList<TimelineItemForDateTimePair> listOfPairs = convertEventsToPairs(events);
// insert the TimelineItems from the pairs into the Timeline
for (int i = 0; i < listOfPairs.size(); i++) {
// get the last position for the DateTime associated with the pair
int position = findLastPositionForDate(listOfPairs.get(i).dateTime);
// if position is -1, the events started on a day before the timeline starts
// so keep skipping pairs until position > -1
if (position > -1) {
// if the item is a PlaceholderItem
if (mTimelineItems.get(position).isPlaceholderItem) {
// remove the PlaceholderItem
mTimelineItems.remove(position);
// and add the TimelineItem from the pair at the position the PlaceholderItem was at
mTimelineItems.add(position, listOfPairs.get(i).timelineItem);
// update the UI on the UI thread
publishProgress(position, TYPE_CHANGED);
} else { // if the item is not a PlaceholderItem, there's already an normal TimelineItem in place
// place the new item at the next position on the Timeline
mTimelineItems.add(position + 1, listOfPairs.get(i).timelineItem);
publishProgress(position, TYPE_ADDED);
}
}
}
}
return null;
}
/**
* onProgressUpdate handles the UI changes on the UI thread for us. Type int available:
* - TYPE_CHANGED
* - TYPE_ADDED
* - TYPE_DELETED
*
* #param values value[0] is the position as <code>int</code>,
* value[1] is the type of manipulation as <code>int</code>
*/
#Override
protected void onProgressUpdate(Integer... values) {
int position = values[0];
int type = values[1];
// update the UI for each changed/added/deleted TimelineItem
if (type == TYPE_CHANGED) {
BusProvider.getInstance().post(new TimelineItemChangedNotification(position));
} else if (type == TYPE_ADDED) {
BusProvider.getInstance().post((new TimelineItemAddedNotification(position)));
} else if (type == TYPE_DELETED) {
// TODO: make delete work bro!
}
}
}
The problem is, that somehow, scrolling while this progress is being posted messes up the UI completely.
My main problem is: when I update a specific item in the data set (TimelineItems) of the adapter, notifyItemChanged() does change the item but doesn't put the item at the correct position.
Here's my adapter:
/**
* A custom RecyclerView Adapter to display a Timeline in a TimelineFragment.
*/
public class TimelineAdapter extends RecyclerView.Adapter<TimelineAdapter.TimelineItemViewHolder> {
/*************
* VARIABLES *
*************/
private ArrayList<TimelineItem> mTimelineItems;
/****************
* CONSTRUCTORS *
****************/
/**
* Constructor with <code>ArrayList<TimelineItem></code> as data set argument.
*
* #param timelineItems ArrayList with TimelineItems to display
*/
public TimelineAdapter(ArrayList<TimelineItem> timelineItems) {
this.mTimelineItems = timelineItems;
}
// Create new views (invoked by the layout manager)
#Override
public TimelineItemViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_timeline, parent, false);
// set the view's size, margins, paddings and layout parameters
// ...
return new TimelineItemViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(TimelineItemViewHolder holder, int position) {
// - get element from your data set at this position
// - replace the contents of the view with that element
// if the item is a ShowPreviousMonthsItem, set the showPreviousMonthsText accordingly
if (mTimelineItems.get(position).isShowPreviousMonthsItem) {
holder.showPreviousMonthsText.setText(mTimelineItems.get(position).showPreviousMonthsText);
} else { // otherwise set the showPreviousMonthsText blank
holder.showPreviousMonthsText.setText("");
}
// day of month & day of week of the TimelineItem
if (mTimelineItems.get(position).isFirstItemOfDay) {
holder.dayOfWeek.setText(mTimelineItems.get(position).dayOfWeek);
holder.dayOfMonth.setText(mTimelineItems.get(position).dayOfMonth);
} else {
holder.dayOfWeek.setText("");
holder.dayOfMonth.setText("");
}
// Event name for the TimelineItem
holder.name.setText(mTimelineItems.get(position).name);
// place and goingTo of the TimelineItem
// if combinedPlace == ""
if(mTimelineItems.get(position).combinedPlace.equals("")) {
if (mTimelineItems.get(position).isFirstDayOfEvent) {
holder.place.setText(mTimelineItems.get(position).place);
} else {
holder.place.setText("");
}
if (mTimelineItems.get(position).isLastDayOfEvent) {
holder.goingTo.setText(mTimelineItems.get(position).goingTo);
} else {
holder.goingTo.setText("");
}
holder.combinedPlace.setText("");
} else {
holder.place.setText("");
holder.goingTo.setText("");
holder.combinedPlace.setText(mTimelineItems.get(position).combinedPlace);
}
if(mTimelineItems.get(position).startDateTime != null) {
holder.startTime.setText(mTimelineItems.get(position).startDateTime.toString("HH:mm"));
} else {
holder.startTime.setText("");
}
if(mTimelineItems.get(position).endDateTime != null) {
holder.endTime.setText(mTimelineItems.get(position).endDateTime.toString("HH:mm"));
} else {
holder.endTime.setText("");
}
if (!mTimelineItems.get(position).isShowPreviousMonthsItem) {
if (mTimelineItems.get(position).date.getDayOfWeek() == DateTimeConstants.SUNDAY) {
holder.dayOfWeek.setTextColor(Color.RED);
holder.dayOfMonth.setTextColor(Color.RED);
} else {
holder.dayOfWeek.setTypeface(null, Typeface.NORMAL);
holder.dayOfMonth.setTypeface(null, Typeface.NORMAL);
holder.dayOfWeek.setTextColor(Color.GRAY);
holder.dayOfMonth.setTextColor(Color.GRAY);
}
} else {
((RelativeLayout) holder.dayOfWeek.getParent()).setBackgroundColor(Color.WHITE);
}
holder.bindTimelineItem(mTimelineItems.get(position));
}
// Return the size of the data set (invoked by the layout manager)
#Override
public int getItemCount() {
return mTimelineItems.size();
}
// replace the data set
public void setTimelineItems(ArrayList<TimelineItem> timelineItems) {
this.mTimelineItems = timelineItems;
}
// replace an item in the data set
public void swapTimelineItemAtPosition(TimelineItem item, int position) {
mTimelineItems.remove(position);
mTimelineItems.add(position, item);
notifyItemChanged(position);
}
// the ViewHolder class containing the relevant views,
// also binds the Timeline item itself to handle onClick events
public class TimelineItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView dayOfWeek;
protected TextView dayOfMonth;
protected TextView showPreviousMonthsText;
protected TextView name;
protected TextView place;
protected TextView combinedPlace;
protected TextView goingTo;
protected TextView startTime;
protected TextView endTime;
protected TimelineItem timelineItem;
public TimelineItemViewHolder(View view) {
super(view);
view.setOnClickListener(this);
this.dayOfWeek = (TextView) view.findViewById(R.id.day_of_week);
this.dayOfMonth = (TextView) view.findViewById(R.id.day_of_month);
this.showPreviousMonthsText = (TextView) view.findViewById(R.id.load_previous_data);
this.name = (TextView) view.findViewById(R.id.name);
this.place = (TextView) view.findViewById(R.id.place);
this.combinedPlace = (TextView) view.findViewById(R.id.combined_place);
this.goingTo = (TextView) view.findViewById(R.id.going_to);
this.startTime = (TextView) view.findViewById(R.id.start_time);
this.endTime = (TextView) view.findViewById(R.id.end_time);
}
public void bindTimelineItem(TimelineItem item) {
timelineItem = item;
}
// handles the onClick of a TimelineItem
#Override
public void onClick(View v) {
// if the TimelineItem is a ShowPreviousMonthsItem
if (timelineItem.isShowPreviousMonthsItem) {
BusProvider.getInstance().post(new ShowPreviousMonthsRequest());
}
// if the TimelineItem is a PlaceholderItem
else if (timelineItem.isPlaceholderItem) {
Toast.makeText(v.getContext(), "(no details)", Toast.LENGTH_SHORT).show();
}
// else the TimelineItem is an actual event
else {
Toast.makeText(v.getContext(), "eventId = " + timelineItem.eventId, Toast.LENGTH_SHORT).show();
}
}
}
And this is the method that is triggered in the TimelineFragment when a change is posted on the event bus:
#Subscribe
public void onTimelineItemChanged(TimelineItemChangedNotification notification) {
int position = notification.position;
Log.d(TAG, "TimelineItemChanged detected for position " + position);
mAdapter.swapTimelineItemAtPosition(mTimeline.mTimelineItems.get(position), position);
mAdapter.notifyItemChanged(position);
Log.d(TAG, "Item for position " + position + " swapped");
}
A thing to note is that the data set of the adapter seems to display correctly after I scrolled away from the changed data far enough and return to the position after that. Initially the UI is totally messed up though.
EDIT:
I found that adding
mAdapter.notifyItemRangeChanged(position, mAdapter.getItemCount());
resolves the issue but - unfortunately - sets the scroll position to the one being changed :(
Here's my TimelineFragment:
/**
* Fragment displaying a Timeline using a RecyclerView
*/
public class TimelineFragment extends BackHandledFragment {
// DEBUG flag and TAG
private static final boolean DEBUG = false;
private static final String TAG = TimelineFragment.class.getSimpleName();
// variables
protected RecyclerView mRecyclerView;
protected TimelineAdapter mAdapter;
protected LinearLayoutManager mLinearLayoutManager;
protected Timeline mTimeline;
protected MenuItem mMenuItemScroll2Today;
protected MenuItem mMenuItemReload;
protected String mToolbarTitle;
// TODO: get the value of this boolean from the shared preferences
private boolean mUseTimelineItemDividers = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get a handle to the app's Timeline singleton
mTimeline = Timeline.getInstance();
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
rootView.setTag(TAG);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.timeline_list);
mRecyclerView.hasFixedSize();
// LinearLayoutManager constructor
mLinearLayoutManager = new LinearLayoutManager(getActivity());
// set the layout manager
setRecyclerViewLayoutManager();
// adapter constructor
mAdapter = new TimelineAdapter(mTimeline.mTimelineItems);
// set the adapter for the RecyclerView.
mRecyclerView.setAdapter(mAdapter);
// add lines between the different items if using them
if (mUseTimelineItemDividers) {
RecyclerView.ItemDecoration itemDecoration =
new TimelineItemDivider(this.getContext());
mRecyclerView.addItemDecoration(itemDecoration);
}
// add the onScrollListener
mRecyclerView.addOnScrollListener(new TimelineOnScrollListener(mLinearLayoutManager) {
// when the first visible item on the Timeline changes,
// adjust the Toolbar title accordingly
#Override
public void onFirstVisibleItemChanged(int position) {
mTimeline.mCurrentScrollPosition = position;
try {
String title = mTimeline.mTimelineItems
.get(position).date
.toString(TimelineConfig.TOOLBAR_DATE_FORMAT);
// if mToolbarTitle is null, set it to the new title and post on bus
if (mToolbarTitle == null) {
if (DEBUG)
Log.d(TAG, "mToolbarTitle is null - posting new title request on bus: " + title);
mToolbarTitle = title;
BusProvider.getInstance().post(new ChangeToolbarTitleRequest(mToolbarTitle));
} else { // if mToolbarTitle is not null
// only post on the bus if the new title is different from the previous one
if (!title.equals(mToolbarTitle)) {
if (DEBUG)
Log.d(TAG, "mToolbarTitle is NOT null, but new title detected - posting new title request on bus: " + title);
mToolbarTitle = title;
BusProvider.getInstance().post(new ChangeToolbarTitleRequest(mToolbarTitle));
}
}
} catch (NullPointerException e) {
// if the onFirstVisibleItemChanged is called on a "ShowPreviousMonthsItem",
// leave the title as it is
}
}
});
return rootView;
}
/**
* Set RecyclerView's LayoutManager to the one given.
*/
public void setRecyclerViewLayoutManager() {
int scrollPosition;
// If a layout manager has already been set, get current scroll position.
if (mRecyclerView.getLayoutManager() != null) {
scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
} else {
scrollPosition = mTimeline.mFirstPositionForToday;
}
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mLinearLayoutManager.scrollToPositionWithOffset(scrollPosition, 0);
}
// set additional menu items for the Timeline fragment
#Override
public void onPrepareOptionsMenu(Menu menu) {
// scroll to today
mMenuItemScroll2Today = menu.findItem(R.id.action_scroll2today);
mMenuItemScroll2Today.setVisible(true);
mMenuItemScroll2Today.setIcon(Timeline.getIconForDateTime(new DateTime()));
mMenuItemScroll2Today.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// stop scrolling
mRecyclerView.stopScroll();
// get today's position
int todaysPosition = mTimeline.mFirstPositionForToday;
// scroll to today's position
mLinearLayoutManager.scrollToPositionWithOffset(todaysPosition, 0);
return false;
}
});
// reload data from Hacklberry
mMenuItemReload = menu.findItem(R.id.action_reload_from_hacklberry);
mMenuItemReload.setVisible(true);
mMenuItemReload.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// stop scrolling
mRecyclerView.stopScroll();
//
mTimeline.reloadDBForCurrentMonth();
mTimeline.loadEventsFromUninfinityDBAsync(mTimeline.mTimelineStart, mTimeline.mTimelineEnd);
return false;
}
});
super.onPrepareOptionsMenu(menu);
}
#Override
public void onResume() {
super.onResume();
// if the Timeline has been invalidated, let AllInOneActivity know it needs to replace
// this Fragment with a new one
if (mTimeline.isInvalidated()) {
Log.d(TAG, "posting TimelineInvalidatedNotification on the bus ...");
BusProvider.getInstance().post(
new TimelineInvalidatedNotification());
}
// fetch today's menu icon
if (mMenuItemScroll2Today != null) {
if (DEBUG) Log.d(TAG, "fetching scroll2today menu icon");
mMenuItemScroll2Today.setIcon(Timeline.getIconForDateTime(new DateTime()));
}
}
// from BackHandledFragment
#Override
public String getTagText() {
return TAG;
}
// from BackHandledFragment
#Override
public boolean onBackPressed() {
return false;
}
#Subscribe
public void onHacklberryReloaded(HacklberryLoadedNotification notification) {
resetReloading();
}
// handles ShowPreviousMonthsRequests posted on the bus by the TimelineAdapter's ShowPreviousMonthsItem onClick()
#Subscribe
public void onShowPreviousMonthsRequest(ShowPreviousMonthsRequest request) {
// create an empty OnItemTouchListener to prevent the user from manipulating
// the RecyclerView while it loads more data (would mess up the scroll position)
EmptyOnItemTouchListener listener = new EmptyOnItemTouchListener();
// add it to the RecyclerView
mRecyclerView.addOnItemTouchListener(listener);
// load the previous months (= add the required TimelineItems)
int newScrollToPosition = mTimeline.showPreviousMonths();
// pass the new data set to the TimelineAdapter
mAdapter.setTimelineItems(mTimeline.mTimelineItems);
// notify the adapter the data set has changed
mAdapter.notifyDataSetChanged();
// scroll to the last scroll (updated) position
mLinearLayoutManager.scrollToPositionWithOffset(newScrollToPosition, 0);
}
#Subscribe
public void onTimelineItemChanged(TimelineItemChangeNotification notification) {
int position = notification.position;
Log.d(TAG, "TimelineItemChanged detected for position " + position);
mAdapter.swapTimelineItemAtPosition(mTimeline.mTimelineItems.get(position), position);
//mAdapter.notifyItemRangeChanged(position, position);
Log.d(TAG, "Item for position " + position + " swapped");
}
I've taken a screenshot of the app after it first loads. I'll explain real quick what happens on initialisation:
the Timeline is built by populating all days with PlaceholderItems (a TimelineItem with just a Date).
Events are loaded from the DB and transformed to TimelineItems
Whenever a new TimelineItem has changed and is ready, the Timeline pokes the TimelineFragment via the otto bus to update the data set of the adapter for that particular position with the new TimelineItem.
Here's a screenshot of what happens after the initial load:
the Timeline is loaded but certain items are inserted at the wrong position.
When scrolling away and returning to the range of days that was displayed incorrectly before, all is good:

About your second approach. Probably your code is not workind because you have Data Race on mTimelineItems and mCachedEvents. I can't see all of your code, but it seems that you using mTimelineItems inside doInBackground() simultaneously with the UI thread without any synchronization.
I propose you to make a mix of your first and second approaches:
Make a copy of the original data (mTimelineItems) and send it to the AsyncTask.
Change the copy asynchronously in doInBackground() and log all changes.
Return the changed data and logs to the UI thread.
Apply the new data to the RecyclerView by using logs.
Let me illustrate this approach in code.
Data management:
public class AsyncDataUpdater
{
/**
* Example data entity. We will use it
* in our RecyclerView.
*/
public static class TimelineItem
{
public final String name;
public final float value;
public TimelineItem(String name, float value)
{
this.name = name;
this.value = value;
}
}
/**
* That's how we will apply our data changes
* on the RecyclerView.
*/
public static class Diff
{
// 0 - ADD; 1 - CHANGE; 2 - REMOVE;
final int command;
final int position;
Diff(int command, int position)
{
this.command = command;
this.position = position;
}
}
/**
* And that's how we will notify the RecyclerView
* about changes.
*/
public interface DataChangeListener
{
void onDataChanged(ArrayList<Diff> diffs);
}
private static class TaskResult
{
final ArrayList<Diff> diffs;
final ArrayList<TimelineItem> items;
TaskResult(ArrayList<TimelineItem> items, ArrayList<Diff> diffs)
{
this.diffs = diffs;
this.items = items;
}
}
private class InsertEventsTask extends AsyncTask<Void, Void, TaskResult>
{
//NOTE: this is copy of the original data.
private ArrayList<TimelineItem> _old_items;
InsertEventsTask(ArrayList<TimelineItem> items)
{
_old_items = items;
}
#Override
protected TaskResult doInBackground(Void... params)
{
ArrayList<Diff> diffs = new ArrayList<>();
try
{
//TODO: long operation(Database, network, ...).
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
//Some crazy manipulation with data...
//NOTE: we change the copy of the original data!
Random rand = new Random();
for(int i = 0; i < 10; i ++)
{
float rnd = rand.nextFloat() * 100.0f;
for(int j = 0; j < _old_items.size(); j++)
{
if(_old_items.get(j).value > rnd)
{
TimelineItem item = new TimelineItem("Item " + rnd, rnd);
//Change data.
_old_items.add(j, item);
//Log the changes.
diffs.add(new Diff(0, j));
break;
}
}
}
for(int i = 0; i < 5; i ++)
{
int rnd_index = rand.nextInt(_old_items.size());
//Change data.
_old_items.remove(rnd_index);
//Log the changes.
diffs.add(new Diff(2, rnd_index));
}
//...
return new TaskResult(_old_items, diffs);
}
#Override
protected void onPostExecute(TaskResult result)
{
super.onPostExecute(result);
//Apply the new data in the UI thread.
_items = result.items;
if(_listener != null)
_listener.onDataChanged(result.diffs);
}
}
private DataChangeListener _listener;
private InsertEventsTask _task = null;
/** Managed data. */
private ArrayList<TimelineItem> _items = new ArrayList<>();
public AsyncDataUpdater()
{
// Some test data.
for(float i = 10.0f; i <= 100.0f; i += 10.0f)
_items.add(new TimelineItem("Item " + i, i));
}
public void setDataChangeListener(DataChangeListener listener)
{
_listener = listener;
}
public void updateDataAsync()
{
if(_task != null)
_task.cancel(true);
// NOTE: we should to make the new copy of the _items array.
_task = new InsertEventsTask(new ArrayList<>(_items));
_task.execute();
}
public int getItemsCount()
{
return _items.size();
}
public TimelineItem getItem(int index)
{
return _items.get(index);
}
}
Using in UI:
public class MainActivity extends AppCompatActivity
{
private static class ViewHolder extends RecyclerView.ViewHolder
{
private final TextView name;
private final ProgressBar value;
ViewHolder(View itemView)
{
super(itemView);
name = (TextView)itemView.findViewById(R.id.tv_name);
value = (ProgressBar)itemView.findViewById(R.id.pb_value);
}
void bind(AsyncDataUpdater.TimelineItem item)
{
name.setText(item.name);
value.setProgress((int)item.value);
}
}
private static class Adapter extends RecyclerView.Adapter<ViewHolder>
implements AsyncDataUpdater.DataChangeListener
{
private final AsyncDataUpdater _data;
Adapter(AsyncDataUpdater data)
{
_data = data;
_data.setDataChangeListener(this);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
holder.bind(_data.getItem(position));
}
#Override
public int getItemCount()
{
return _data.getItemsCount();
}
#Override
public void onDataChanged(ArrayList<AsyncDataUpdater.Diff> diffs)
{
//Apply changes.
for(AsyncDataUpdater.Diff d : diffs)
{
if(d.command == 0)
notifyItemInserted(d.position);
else if(d.command == 1)
notifyItemChanged(d.position);
else if(d.command == 2)
notifyItemRemoved(d.position);
}
}
}
private AsyncDataUpdater _data = new AsyncDataUpdater();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv_content = (RecyclerView)findViewById(R.id.rv_content);
rv_content.setLayoutManager(new LinearLayoutManager(this));
rv_content.setAdapter(new Adapter(_data));
Button btn_add = (Button)findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
_data.updateDataAsync();
}
});
}
}
I put Example application on GH, so you can test it if you want.
Update 1
About Data Race.
this.mTimelineItems = timelineItems; inside TimelineAdapter() constructor makes a copy of the reference to the ArrayList, but not the copy of the ArrayList itself. So you have two references: TimelineAdapter.mTimelineItems and Timeline.mTimelineItems, that both refer to the same ArrayList object. Please, look at this.
The data race occurs when doInBackground() called from Worker Thread and onProgressUpdate() called from UI Thread simultaneously. The main reason is that publishProgress() does not call onProgressUpdate() synchronously. Instead, publishProgress() plans the call of onProgressUpdate() on UI Thread in the future. Here is a good description of the problem.
Off topic.
This:
mTimelineItems.set(position, item);
should be faster than this:
mTimelineItems.remove(position);
mTimelineItems.add(position, item);

Related

How to change the data of an item at once using DiffUtil?

When I press the toggle button, I want to change the units of the list of the currently displayed recycler views at once.
I used ListAdapter + DiffUtil to display the recycler view.
The way I tried to implement this feature is to load the current list when the toggle button is pressed.
Then, after resetting the new toggle unit values ​​for the current lists, I used submitList() to update the list.
But this was the wrong way.
My guess is because the variable created for the value of the list loaded to be updated has the same reference value, so the value changed at the same time.
In other words, there is no change because the values ​​of the update list and the existing list are the same.
What can I do to solve this problem?
RoutineDetailModel.java
public class RoutineDetailModel {
public int id;
private int set = 1;
public static String unit = "kg";
public RoutineDetailModel() {
Random random = new Random();
this.id = random.nextInt();
}
public RoutineDetailModel(int set) {
Random random = new Random();
this.id = random.nextInt();
this.set = set+1;
}
public int getSet() {
return set;
}
public int getId() {
return id;
}
public String getWeight() {
return weight;
}
public String getUnit() {
return unit;
}
#Override
public int hashCode() {
return Objects.hash(set, weight); // getWeight를 호출하면 더 다양하게 되나?
}
#Override
public boolean equals(#Nullable Object obj) {
if(obj != null && obj instanceof RoutineDetailModel) {
RoutineDetailModel model = (RoutineDetailModel) obj;
if(this.id == model.getId()) {
return true;
}
}
return false;
}
}
MainActivity.java
public class WriteRoutineActivity extends AppCompatActivity implements WritingCommentDialogFragment.OnDialogClosedListener {
List<RoutineModel> items;
RoutineListAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_routine);
listAdapter.setOnRoutineClickListener(new RoutineListAdapter.OnRoutineItemClickListener() {
#Override
public void onUnitBtnClicked(int curRoutinePos, String unit) {
Object obj = listAdapter.getRoutineItem(curRoutinePos);
RoutineModel item = (RoutineModel) obj;
if(obj instanceof RoutineModel) {
for(RoutineDetailModel detailItem : item.getDetailItemList()) {
detailItem.setUnit(unit);
}
listAdapter.submitList(getUpdatedList());
}
}
});
}
}
Please tell me if you need more information
To change all the items you need to use notifyDataSetChanged() otherwise you cannot update ALL the items.
In order to do so, you must create a method inside your adapter which does the following ->
public void updateItems(String unit) {
for({YOUR ITEM TYPE} item: {YOUR LIST}) {
item.unit = unit;
}
notifyDataSetChanged();
}
And call this method when you want to change all units.
yourAdapter.updateItems("Kg");

Android Recyclerview, add new item keeping the rest unchanged and the scrolling in the same place

Gonna do some background so you can picture better what I need to accomplish.
Well the thing is, I'm reading data from a DataBase, this data changes relatively fast, let's say every 30 seconds, I need to scroll the recycler to check the new data and press some buttons, every time I press the button, it changes its color, but 2 things are happening.
1- I refresh the recycler with a timer every second so, when I'm scrolling it and the refresh comes, it will go all the way to the first item and that can't happen, so I need a way to prevent this.
2- every data that comes from the Database needs some adjustment and for that, I press a button, this button changes it´s color when I press it so I know the action for that button is done but, everytime the timer refreshes the recycler it not only goes to the first item but also turns the buttons to the original color because is re-creating the entire recycler with the data in the database, and that Can Not Happen, when the timer refreshes the recycler, the old items should remain the same and only add the NEW IF there's any new.
well here's my code for the adapter and for the Activity, thanks in advance for the help.
Here's the Adapter
public class ComandaAdapter extends
RecyclerView.Adapter<ComandaAdapter.ComandaAdapterViewHolder>
implements View.OnClickListener{
private ArrayList<Comanda> list_comandas;
private Context context;
private View.OnClickListener listener;
public ComandaAdapter(Context con, ArrayList<Comanda> list) {
this.context = con;
this.list_comandas = list;
}
#Override
public ComandaAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row, parent, false);
return new ComandaAdapterViewHolder(itemView);
}
#Override
public void onBindViewHolder(ComandaAdapterViewHolder holder, int position) {
Integer anchomanda = Math.round(context.getResources().getDimension(R.dimen.parents_size));
// Llenar la información de cada item
Comanda comanda = list_comandas.get(position);
comanda.setNro_comanda(position+"");
String cadena = comanda.getOrden();
Integer tope = cadena.length();
Boolean tijera_categoria=false;
Boolean tijera_articulo=true;
Boolean tijera_contorno=true;
Boolean tijera_cambio =true;
Integer indisup;
Integer indiin =0;
char apuntador;
String Buscado ="";
String Buscado_contorno="";
String Buscado_categoria="";
Integer id=0;
holder.txt_comanda.setText(position+"");
holder.txt_mesa.setText(comanda.getMesa());
for (int i = 0; i < tope ; i++) {
apuntador = cadena.charAt(i);
if (Buscado.equals("Bebidas")){
break;
}
else
{
if (apuntador == '$')
{
break;
}
else
{
//CUERPO PRINCIPAL DE EJECUCION
if (apuntador == '#' && !tijera_categoria)
{
if (i==0) {
indiin = i + 1;
}
}
if (apuntador == '!' && !tijera_categoria)
{
tijera_categoria=true;
tijera_articulo=false;
indisup=i;
id=i;
Buscado=cadena.substring(indiin,indisup);
indiin=indisup+1;
Buscado_categoria=Buscado;
holder.b[id].setId(id);
}
if (apuntador == '%' && !tijera_articulo)
{
indisup=i;
tijera_articulo=true;
tijera_contorno=false;
Buscado=cadena.substring(indiin,indisup);
indiin=indisup+1;
holder.b[id].setLayoutParams(new LinearLayout.LayoutParams(anchomanda, LinearLayout.LayoutParams.WRAP_CONTENT));
holder.b[id].setTextSize((context.getResources().getDimension(R.dimen.txt_size)) / 2);
if (Buscado_categoria.equals("Fondos")) {
holder.b[id].setBackgroundTintList(context.getResources().getColorStateList(R.color.fondos, null));
}
if (Buscado_categoria.equals("Entradas")) {
holder.b[id].setBackgroundTintList(context.getResources().getColorStateList(R.color.entradas, null));
}
if (Buscado_categoria.equals("Postres")) {
holder.b[id].setBackgroundTintList(context.getResources().getColorStateList(R.color.postres, null));
}
holder.b[id].setText(Buscado);
holder.lyocomanda.addView(holder.b[id]);
}
if (apuntador == '*' && !tijera_contorno)
{
indisup=i;
tijera_cambio=false;
Buscado=cadena.substring(indiin,indisup);
indiin=indisup+1;
if (!Buscado.equals("")) {
Buscado_contorno=Buscado;
holder.t[i].setText(Buscado);
holder.t[i].setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
holder.t[i].setTextSize((context.getResources().getDimension(R.dimen.txt_size)) / 2);
holder.l[id].addView(holder.t[i]);
}
}
if (apuntador == '#' && !tijera_cambio)
{
indisup=i;
tijera_contorno=true;
tijera_cambio=true;
tijera_categoria=false;
Buscado=cadena.substring(indiin,indisup);
indiin=indisup+1;
if (!Buscado_contorno.equals("")) {
holder.l[id].setLayoutParams(new LinearLayout.LayoutParams(anchomanda, ViewGroup.LayoutParams.WRAP_CONTENT));
holder.l[id].setOrientation(LinearLayout.VERTICAL);
holder.l[id].setBackground(context.getDrawable(customborder));
holder.lyocomanda.addView(holder.l[id]);
}
}
//FIN CUERPO PRINCIPAL DE EJECUCION
} //EJECUCION DE DESCARTE DE FINAL DE CADENA
} //EJECUCION DE DESCARTE DE BEBIDAS
}
}
#Override
public int getItemCount() {
return list_comandas.size();
}
public void removeItem(int position) {
list_comandas.remove(position);
notifyItemRemoved(position);
}
#Override
public void onClick(View view) {
if (listener != null)
{
listener.onClick(view);
}
}
public void setOnClickListener(View.OnClickListener listener)
{
this.listener = listener;
}
public class ComandaAdapterViewHolder extends RecyclerView.ViewHolder {
TextView txt_comanda, txt_mesa;
private LinearLayout lyocomanda;
private Integer cant_platos =500;
private TextView[] t = new TextView[(cant_platos*8)];
private LinearLayout[] l = new LinearLayout[cant_platos];
private Button[] b = new Button[cant_platos];
public ComandaAdapterViewHolder(View itemView) {
super(itemView);
// Inicializamos los controles
lyocomanda = (LinearLayout) itemView.findViewById(R.id.lyocomanda);
txt_comanda = (TextView) itemView.findViewById(R.id.txt_comanda);
txt_mesa = (TextView) itemView.findViewById(R.id.txt_mesa);
for (int i = 0; i <cant_platos; i++) {
/////////////////////////////CONFIGURACION DEL BOTON///////////////////////////
b[i] = new Button(itemView.getContext());
b[i].setOnClickListener(listener);
///////////////////////////CONFIGURACION DEL CONTORNO///////////////////////////
l[i] = new LinearLayout(itemView.getContext());
t[i] = new TextView(itemView.getContext());
}
}
}
}
Here´s the Activity
public class MainActivity extends AppCompatActivity {
private ComandaAdapter mComandaAdapter;
ArrayList<Comanda> lista_Comanda;
RecyclerView rec_Lista;
public int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rec_Lista = (RecyclerView) findViewById(R.id.rec_lista);
new CountDownTimer(10000, 100){
#Override
public void onTick(long millisUntilFinished) {
counter++;
}
#Override
public void onFinish() {
try{
loadRetrofitComanda();
counter = 0;
start();
}
catch (Exception e){
mostrarMensaje("Error: " + e.getMessage());
}
}
}.start();
}
#Override
protected void onResume() {
super.onResume();
loadRetrofitComanda();
}
private void loadRetrofitComanda()
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.20:3000")
.addConverterFactory(GsonConverterFactory.create())
.build();
IRequestComanda request = retrofit.create(IRequestComanda.class);
Call<ArrayList<Comanda>> call = request.getJSONComandas();
call.enqueue(new Callback<ArrayList<Comanda>>() {
#Override
public void onResponse(Call<ArrayList<Comanda>> call, Response<ArrayList<Comanda>> response) {
ArrayList<Comanda> lista = response.body();
lista_Comanda = lista;
// Refresh recyclerview
setAdapter();
configurarOrientacionLayout();
}
#Override
public void onFailure(Call<ArrayList<Comanda>> call, Throwable t) {
mostrarMensaje("Error: " + t.getMessage());
}
});
}
private void mostrarMensaje(String mensaje)
{
Toast.makeText(getApplicationContext(), mensaje, Toast.LENGTH_SHORT).show();
}
private void setAdapter()
{
mComandaAdapter = new ComandaAdapter(getApplicationContext(), lista_Comanda);
mComandaAdapter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mostrarMensaje("ejecutar accion");
}
});
rec_Lista.setAdapter(mComandaAdapter);
}
private void configurarOrientacionLayout()
{
rec_Lista.setLayoutManager(new L LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
}
}
a little update, I have managed to keep the recyclerview state when new data arrives with 2 lines of code :
recyclerViewState = rec_Lista.getLayoutManager().onSaveInstanceState();//save
setAdapter();
configurarOrientacionLayout();
rec_Lista.getLayoutManager().onRestoreInstanceState(recyclerViewState);//restore
by saving and restoring the layout manager befor and after calling the setAdapter method, so 1 more problem to go.. the scroll position for each recycler item.
Solved the final Issue with this.
mComandaAdapter.notifyItemInserted(lista_Comanda.size());
so i check if new items has arrived, if so, I just isolate the item and add it to the recycler and everything else remains unchanged.. Im not setting the adapter everytime new data arrives now.
hope this can help anyone.
RecyclerView does this out of the box when using a ListAdapter, which in turn uses DiffUtil. You then just have to inform the adapter of the new list via submitList(newList).
DiffUtil takes an old list and a new list and figures out what's different. It finds which items were added, removed, or changed, and RecyclerView can use that information to update those items, which is much more efficient than redoing the entire list.
The RecyclerView takes care of presenting it smoothly to the user.

How to manipulate view inside ListView items?

I have a ListView in which there several rows containing two buttons and a ProgressBar (Visibility:GONE) each.
My purpose is to display the ProgressBar upon click on the buttons and after completing a certain set of background operations remove that row entirely.
The problem here is that after removing the item from the ArrayList which the ListView is created upon and calling notifyDataSetChanged the row is removed successfully but the ProgressBar remains visible.
Shouldn't it be removed along with it's parent view?
Checkout the following record to see the problem in action.
Here is the source of my entire fragment:
public class FriendRequestFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = "FriendRequestFragment";
ArrayList<FriendRequest> friendRequests;
#InjectView(R.id.friendRequestList)
ListView mListView;
#InjectView(R.id.noRequestsText)
TextView noRequestsText;
#InjectView(R.id.swipe)
SwipeRefreshLayout swipeRefreshLayout;
// NotificationHandler nh;
/**
* The Adapter which will be used to populate the ListView/GridView with
* Views.
*/
private FriendRequestAdapter mAdapter;
private Context c;
private boolean isProcessing = false;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public FriendRequestFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Util.trackFragment(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friendrequest_list, container, false);
ButterKnife.inject(this, view);
c = getActivity();
friendRequests = new ArrayList<>();
swipeRefreshLayout.setOnRefreshListener(this);
mAdapter = new FriendRequestAdapter(getActivity(), friendRequests);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int topRowVerticalPosition =
(view == null || view.getChildCount() == 0) ?
0 : view.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
Log.d(TAG, "SwipeRefresh: " + String.valueOf(firstVisibleItem == 0 && topRowVerticalPosition >= 0));
}
});
loadRequests();
return view;
}
private void loadRequests() {
// nh = new NotificationHandler(getActivity());
swipeRefreshLayout.setRefreshing(true);
Log.d(TAG, "loading requests init");
HashMap<String, Integer> params = new HashMap<>();
params.put("profile_id", Util.getCurrentProfileID(c));
final String uniqueID = Util.getCurrentProfileID(c) + String.valueOf(System.currentTimeMillis() / 1000 / 1200);
new ApiRequest(Util.URL_GET_FRIEND_REQUESTS, params, new AjaxCallback<String>() {
#Override
public void callback(String url, String result, AjaxStatus status) {
super.callback(url, result, status);
ApiResponse apiResponse = new ApiResponse(url, result, uniqueID);
Log.d(TAG, "Friend Requests Response: " + result);
if (apiResponse.isSuccessful()) {
JSONArray jsonArray = apiResponse.getDataJSONArray();
try {
for (int i = 0; i < jsonArray.length(); i++) {
friendRequests.add(new FriendRequest(jsonArray.getJSONObject(i)));
}
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
mListView.setVisibility(View.VISIBLE);
} else if (apiResponse.getErrorMessage().equals("request_not_found")) {
noRequestsText.setVisibility(View.VISIBLE);
}
swipeRefreshLayout.setRefreshing(true);
}
}).setUniqueID(uniqueID).execute();
}
#Override
public void onRefresh() {
loadRequests();
}
private void acceptRequest(final int position, final View rootView) {
if (isProcessing) {
CustomToast.makeToast(getActivity(), CustomToast.TYPE_ALERT, getString(R.string.please_wait), CustomToast.LENGTH_SHORT);
return;
}
rootView.findViewById(R.id.loading).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.acceptBtn).setVisibility(View.GONE);
rootView.findViewById(R.id.denyBtn).setVisibility(View.GONE);
isProcessing = true;
Log.d("FriendRequest", "accepting:" + position);
FriendRequest request = friendRequests.get(position);
HashMap<String, Integer> params = new HashMap<>();
params.put("request_id", request.getRequestID());
params.put("profile_id", ProfilesSingleton.getInstance().getCurrentProfile().getProfileID());
new ApiRequest(Util.URL_ACCEPT_REQUEST, params, new AjaxCallback<String>() {
#Override
public void callback(String url, String object, AjaxStatus status) {
super.callback(url, object, status);
ApiResponse apiResponse = new ApiResponse(object);
if (apiResponse.isSuccessful()) {
friendRequests.remove(position);
CustomToast.makeToast(getActivity(), CustomToast.TYPE_DEFAULT,
getString(R.string.you_are_now_friends_with) + " " + friendRequests.get(position).getFullName(),
CustomToast.LENGTH_SHORT);
mAdapter.notifyDataSetChanged();
}else {
rootView.findViewById(R.id.loading).setVisibility(View.GONE);
rootView.findViewById(R.id.acceptBtn).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.denyBtn).setVisibility(View.VISIBLE);
}
isProcessing = false;
}
}).execute();
}
private void denyRequest(final int position, final View rootView) {
if (isProcessing) {
CustomToast.makeToast(getActivity(), CustomToast.TYPE_ALERT, getString(R.string.please_wait), CustomToast.LENGTH_SHORT);
return;
}
rootView.findViewById(R.id.loading).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.acceptBtn).setVisibility(View.GONE);
rootView.findViewById(R.id.denyBtn).setVisibility(View.GONE);
Log.d("FriendRequest", "denying:" + position);
FriendRequest request = friendRequests.get(position);
HashMap<String, Integer> params = new HashMap<>();
params.put("request_id", request.getRequestID());
params.put("profile_id", ProfilesSingleton.getInstance().getCurrentProfile().getProfileID());
new ApiRequest(Util.URL_DENY_REQUEST, params, new AjaxCallback<String>() {
#Override
public void callback(String url, String object, AjaxStatus status) {
super.callback(url, object, status);
ApiResponse apiResponse = new ApiResponse(object);
if (apiResponse.isSuccessful()) {
friendRequests.remove(position);
mAdapter.notifyDataSetChanged();
}else {
rootView.findViewById(R.id.loading).setVisibility(View.GONE);
rootView.findViewById(R.id.acceptBtn).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.denyBtn).setVisibility(View.VISIBLE);
}
}
}).execute();
}
public class FriendRequestAdapter extends ArrayAdapter<FriendRequest> {
public FriendRequestAdapter(Context context, ArrayList<FriendRequest> objects) {
super(context, 0, objects);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rootView = convertView;
final ViewHolder holder;
final FriendRequest friendRequest = getItem(position);
if (rootView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rootView = inflater.inflate(R.layout.friend_request_item, parent, false);
holder = new ViewHolder();
holder.profilePhoto = (RoundedImageView) rootView.findViewById(R.id.profilePhoto);
holder.fullName = (TextView) rootView.findViewById(R.id.fullName);
holder.acceptBtn = (ImageView) rootView.findViewById(R.id.acceptBtn);
holder.denyBtn = (ImageView) rootView.findViewById(R.id.denyBtn);
holder.loading = (ProgressBar) rootView.findViewById(R.id.loading);
rootView.setTag(holder);
} else {
holder = (ViewHolder) rootView.getTag();
}
holder.fullName.setText(friendRequest.getFullName());
if (friendRequest.getFullPhotoPath().equals("")) {
ImageUtil.replaceWithInitialsView(getContext(), holder.profilePhoto, friendRequest.getInitials());
} else {
Util.aQuery.id(holder.profilePhoto).image(friendRequest.getFullPhotoPath(), false, true, 50, R.drawable.avatar_profile, null, AQuery.FADE_IN);
}
final View finalRootView = rootView;
holder.acceptBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
acceptRequest(position, finalRootView);
}
});
holder.denyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
denyRequest(position, finalRootView);
}
});
return rootView;
}
public class ViewHolder {
RoundedImageView profilePhoto;
TextView fullName;
ImageView acceptBtn, denyBtn;
ProgressBar loading;
}
}
}
Add a field in your FriendRequest class that saves the current state of the progress bar. based on it set the visibility of the progress bar.
The same view row has been sent to another row. in your getView method you must always set the progress bar visibility based on its status.
Code Sample:
final View finalRootView = rootView;
if (friendRequest.acceptingRequestInProgress())
holder.loading.setVisibility(View.Visibile);
else
holder.loading.setVisibility(View.Gone);
holder.acceptBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
friendRequest.setAcceptingInProgress(true);
acceptRequest(position, finalRootView);
}
});
holder.denyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
denyRequest(position, finalRootView);
}
});
Another place to modify:
if (apiResponse.isSuccessful()) {
friendRequest.setAcceptingInProgress(false);
friendRequests.remove(position);
mAdapter.notifyDataSetChanged();
}
Note: this is also handles the case when the user scrolls the list
view and the row view in progress is no longer visible. this will
hands the view to another row. But since we check the row state the
progress bar will be stopped. and when user scrolls back to the row
view in progress and hands it a reusable view the progress bar will be
visible again if accepting is still in progress.
Views are getting reused by the ListView and in the getView() method you are not cleaning up the reused view, that's why the progress bar will become visible for an item that shouldn't display it.
Similarly if an item would be removed some items with progress bars visible would loose their progress bar, handing them over to an item that didn't need it.
In getView(), after initializing the holder, you should check if progress bar is necessary.
Start with storing progress bar values at the beginning:
private ArrayList<Integer> progresses = new ArrayList<Integer>();
Update these values every time the list changes (when list changes in loadRequests and when value changes not sure where).
And in getView()
if (progresses.get(position) == 100) {
holder.loading.setVisibility(View.GONE);
} else {
holder.loading.setVisibility(View.VISIBLE);
holder.loading.setProgress(progresses.get(position));
}
The problem is due to visibility of progressbar is VISIBLE default so in getView() after you call notifyDataSetChanged(), the progressbar becomes visible to row position (i - 1).
holder.loading = (ProgressBar) rootView.findViewById(R.id.loading);
holder.loading.setVisibility(View.GONE);
Set progressbar visibility to GONE in getView() and this problem will not come

How to add native ads in a listview?

this is my activity
i want to insert a native ads into the list view.
I'm trying to follow this guide https://github.com/StartApp-SDK/Documentation/wiki/android-advanced-usage But I find it hard to understand.
can you give me a hand, maybe making examples of code? thank you
ACTIVITY
public class EpisodiActivity extends Activity {
private StartAppAd startAppAd = new StartAppAd(this);
public class ViewModel {
private String url;
private String name;
public ViewModel(String url, String name) {
this.url = url;
this.name = name;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// creazione fullscreen activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.episodi_activity);
String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");
ListView mylist = (ListView) findViewById(R.id.listView1);
// And in this loop we create the ViewModel instances from
// the name and url and add them all to a List
List<ViewModel> models = new ArrayList<ViewModel>();
for (int i = 0; i < episodi.length; i++) {
String name = episodi[i];
String url = "No value";
if (i < urls.length) {
url = urls[i];
}
ViewModel model = new ViewModel(url, name);
models.add(model);
}
// Here we create the ArrayAdapter and assign it to the ListView
// We pass the List of ViewModel instances into the ArrayAdapter
final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(
this, android.R.layout.simple_list_item_1, models);
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
// Here we get the ViewModel at the given position
ViewModel model = (ViewModel) arg0.getItemAtPosition(position);
// And the url from the ViewModel
String url = model.getUrl();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
}
#Override
public void onResume() {
super.onResume();
startAppAd.onResume();
startAppAd.showAd();
}
#Override
public void onPause() {
super.onPause();
startAppAd.onPause();
}
}
A listView BaseAdapter first calls the method getCount() to retrieve the number of rows to show and then for each row it calls the getView(int position, View convertView, ViewGroup parent) method to create and return a View object for the given position.
The approach is to extend BaseAdapter and create your own custom Adapter, instead of using the default ArrayAdapter. Then you need to return an “Ad” View instead of your usual normal View when you want to display an ad. This means that you need to override getCount() to return more rows (for example if you have 10 rows, that means you need to return 11 = 10 actual content + 1 ad)
Then you need to decide in which position to create this View, I think you can do it by simply checking the position variable:
if (position == VALUE) {
// Create and return Ad View
} else {
// Create and return a normal View
}
Anyway, this whole thing is really tricky as things can go easily out of hand (positions mismatching with Views etc). StartApp should be able to control your listView adapter to do all this for you. My guess is that your adapter is not communicating properly with StartApp (maybe you are not initialising correctly?).
Try to dig into the documentation or find an example by them. If you can’t figure it out, there are other alternatives you can use such as Avocarrot, Namomedia, Inmobi, etc
I have used Avocarrot which has an open source example in github for inserting ads in listViews.
You can run it and use it if it fits you: https://github.com/Avocarrot/android-demo-app
Recently I stucked with the same question but for a new Admob native ads. Then I decided to post my solution for that to admobadapter. Hope it will help you. I believe you could use the AdmobAdapterWrapper after some customizations for the StartApp-SDK...Kindly look at the AdmobFetcher.java. The result could appear like this
I've been working on this for a while now. I'm using a BaseAdapter and a LinearLayout holder for the ad. These are my global variables:
LinearLayout adHolderView;
private AdRequest adRequest;
NativeExpressAdView singleAdView;
I also have a few other global variables:
public static boolean adLoaded;
public static boolean allowShowAd = true;
public static int adLocation = 5;
Point adSanityCheck = new Point(0,0);
Here is my BaseAdapter, please read the comments
public BaseAdapter drawingsGridAdapter = new BaseAdapter() {
//viewHolder classes are very common with BaseAdapters to speed up loading
//It is not exactly needed but will be very usefull for different uses.
//For example if you are using a GridView instead of a ListView like I am in this case
//it would be good to have the orientation String which is used later on
//in the adapter
class ViewHolderItem {
ImageView imageView;
ImageView userImageView;
TextView userNameView;
TextView dateView;
TextView numCommentsView;
ImageView starView;
TextView starCount;
//below items are used for ads in a gridView
int orientation;
final int LANDSCAPE = 1;
final int PORTRAIT = 2;
}
#SuppressLint("SimpleDateFormat")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
//check if you can use your viewHolderItem and make sure that
//the view is not an Adview
if (convertView == null || !(convertView.getTag() instanceof ViewHolderItem)) {
viewHolder = new ViewHolderItem();
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.gallery_item, null);
viewHolder.dateView = (TextView) ll.findViewById(R.id.gal_item_date);
//...
//initialize your viewHolder items here
//...
convertView = ll;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
//...
Tools.addDateToTextView(viewHolder.dateView, drawings.get(position).time);
//do your view modifications here
//...
//now we start doing the ad stuff
//this is optional. add it in to make it work on a gridview
boolean landscape = false; if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
landscape = true;
//in my case I wanted the ad to be the same size as all my other Items so I made sure the values are accesable.
//since you are only using a ListView, you only really need to check for getMeasuredHeight() or you could define your own
//AdSize
if (convertView.getMeasuredHeight()>0 && convertView.getMeasuredWidth()>0 && allowShowAd) {
int adH = convertView.getMeasuredHeight();
int adW = convertView.getMeasuredWidth();
//if the adview needs to be set up then it will set it up, else it will make sure that the ad is the correct size
//and fix it if it needs to
if (singleAdView == null){
if ((landscape && viewHolder.orientation == viewHolder.LANDSCAPE)
||(!landscape && viewHolder.orientation == viewHolder.PORTRAIT))
setUpAd(adW,adH, landscape);
}else{
if ((landscape && viewHolder.orientation == viewHolder.LANDSCAPE)
||(!landscape && viewHolder.orientation == viewHolder.PORTRAIT)) {
//Log.d("ads","Adsize"+singleAdView.getAdSize());
if (singleAdView.getAdSize().getHeight() !=Tools.convertPixelsToDp(adH-1,MainMenuActivity.this)
|| singleAdView.getAdSize().getWidth() !=Tools.convertPixelsToDp(adW-1,MainMenuActivity.this)) {
Log.d("Ads","ad sizesW:"+singleAdView.getAdSize().getWidth() + "vs" + Tools.convertPixelsToDp(adW-1,MainMenuActivity.this));
Log.d("Ads","ad sizesH:"+singleAdView.getAdSize().getHeight() + "vs" + Tools.convertPixelsToDp(adH-1,MainMenuActivity.this));
//sometimes the correct size is not reported so this will ensure ads are not loaded twice. This only really needs to be checked with a gridview
if (adSanityCheck.x == adW && adSanityCheck.y == adH) {
setUpAd(adW, adH, landscape);
}else{
adSanityCheck.x = adW;
adSanityCheck.y = adH;
}
}
}
}
}//Log.d("Ads","tag ="+av.getTag());
if (position == adLocation && singleAdView!=null && allowShowAd) {
if (adLoaded) {
adHolderView.postDelayed(new Runnable() {
#Override
public void run() {
//this seemed to help avoid ad flicker. may want to test to make sure
if (singleAdView!=null)
singleAdView.requestLayout();
}
}, 100);
return adHolderView;
}
}
if (landscape)
viewHolder.orientation = viewHolder.LANDSCAPE;
else
viewHolder.orientation = viewHolder.PORTRAIT;
return convertView;
}#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if (position == adLocation && singleAdView!=null && adLoaded && allowShowAd){
return 1;
}
return 0;
}
#Override
public int getCount() {
return drawings.size();
}
};
You will notice in my BaseAdapter, that I have a method called setUpAd:
private void setUpAd(int widthPX, int heightPX, boolean isLandscape){
if (allowShowAd) {
Log.d("draw", "setupAD");
destroyAdView();
adHolderView = new LinearLayout(MainMenuActivity.this);
singleAdView = new NativeExpressAdView(this);
adLoaded = false;
drawingsGridAdapter.notifyDataSetChanged();
singleAdView.setId(R.id.googleIdentify);
singleAdView.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxx/xxxxxxxxxxxxxx");
int wh = Tools.convertPixelsToDp(widthPX - 1, MainMenuActivity.this);
singleAdView.setAdSize(new AdSize(Tools.convertPixelsToDp(widthPX - 1, MainMenuActivity.this),
Tools.convertPixelsToDp(heightPX - 1, MainMenuActivity.this)));
adRequest = new AdRequest.Builder()
.addTestDevice("TEST DEV ID")
.build();
singleAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
adLoaded = false;
drawingsGridAdapter.notifyDataSetChanged();
}
#Override
public void onAdLoaded() {
super.onAdLoaded();
adLoaded = true;
drawingsGridAdapter.notifyDataSetChanged();
}
});
singleAdView.loadAd(adRequest);
adHolderView.setLayoutParams(new LinearLayout.LayoutParams(widthPX, heightPX));
adHolderView.setBackgroundColor(Color.WHITE);
adHolderView.addView(singleAdView);
}
}
From that you need a few other methods:
private void destroyAdView()
{
if (singleAdView != null)
{
adRequest = null;
singleAdView.removeAllViews();
singleAdView.setAdListener(null);
singleAdView.destroy();
singleAdView.setEnabled(false);
adHolderView.removeView(singleAdView);
singleAdView = null;
adHolderView = null;
}
}
public static int convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return (int) dp;
}
And Finally, If you end up using a GridView, here is one last thing to add in order to load the right size ad after orientation changes:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
allowShowAd = false;
gridView.postDelayed(new Runnable() {
#Override
public void run() {
drawingsGridAdapter.notifyDataSetChanged();
gridView.invalidateViews();
gridView.requestLayout();
gridView.postDelayed(new Runnable() {
#Override
public void run() {
drawingsGridAdapter.notifyDataSetChanged();
}
},500);
gridView.postDelayed(new Runnable() {
#Override
public void run() {
allowShowAd = true;
Log.d("Ads","allow ads now");
drawingsGridAdapter.notifyDataSetChanged();
}
},1500);
}
}, 100);
}
I do not know the StartApp framework, but basically you have to do the following:
Write your own adapter and do not use ArrayAdapter. Here is a basis class you can use to simplify the adapters view recycling: https://github.com/sockeqwe/appkit/blob/master/adapter/src/main/java/com/hannesdorfmann/appkit/adapter/SimpleAdapter.java
Specify own View cells by overriding getItemViewType(int position) and getViewTypeCount()
Specify a own view type and view holder for your banners that will appear in the ListView.
From what I have seen, the banner needs to save something in onSaveInstanceState() and to restore something in onRestoreInstanceState() . Im not sure if this is needed or makes sence in a ListView. Simply try it with or without this calls. If this is needed, than you have to keep a List of Banner Items in you adapter and you
have to do something like this in your Activities code:
public class MyActivity extends Activity
{
public void onSaveInstanceState(Bundle b){
super.onSaveInstanceState(b);
adapter.saveInstancState(b);
}
public void restoreInstanceState(Bundle b){
super.restoreInstanceState(b);
adapter.restoreInstanceState(b);
}
}

Image Loading and caching android

I have a ListFragment with an adapter and a AsyncTask to load data, currently it builds up a listview with text, and I have put a non-dynamic image as a place holder for now. I was wondering if someone could shed light on the best way to implement downloading images from the net, I have url's to test from google, and caching and showing those images through the adapter, I don't have a scrolllistener yet for when the page reaches bottom, but I am kinda stuck on what the best solution to pass over and load my dynamic images would be, if someone could tell me the best way to load images and preferably cache them, so I don't not have to download them to sd card and everything every time.
Here are my classes
TestListFragment
public class TestListFragment extends ListFragment
implements
android.support.v4.app.LoaderManager.LoaderCallbacks<List<TestItemModel>> {
TestCustomArrayAdapter _adapter;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("TestListFragment.onActivityCreated");
// Initially there is no data
setEmptyText("Refresh: No Data Here");
// Create an empty adapter we will use to display the loaded data.
_adapter = new TestCustomArrayAdapter(getActivity());
setListAdapter(_adapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("TestListFragment", "Item clicked: " + id);
TestItemModel ti = this._adapter.getItem(position);
System.out.println(ti.getId());
}
#Override
public Loader<List<TestItemModel>> onCreateLoader(int arg0, Bundle arg1) {
System.out.println("TestListFragment.onCreateLoader");
return new TestListLoaderAsync(getActivity());
}
#Override
public void onLoadFinished(Loader<List<TestItemModel>> arg0,
List<TestItemModel> data) {
_adapter.setData(data);
System.out.println("TestListFragment.onLoadFinished");
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
#Override
public void onLoaderReset(Loader<List<TestItemModel>> arg0) {
_adapter.setData(null);
}
}
TestCustomArrayAdapter
public class TestCustomArrayAdapter extends ArrayAdapter<TestItemModel> {
private final LayoutInflater _inflater;
public OnItemClickListener ol;
public TestCustomArrayAdapter(Context context) {
super(context, R.layout.test_list_fragment);
_inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setData(List<TestItemModel> data) {
clear();
if (data != null) {
for (TestItemModel appEntry : data) {
add(appEntry);
}
}
}
/**
* Populate new items in the list.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = _inflater.inflate(R.layout.test_single_item, parent, false);
} else {
view = convertView;
}
TestItemModel item = getItem(position);
((TextView) view.findViewById(R.id.item_label)).setText(item.getName());
((TextView) view.findViewById(R.id.item_id)).setText(item.getId());
ImageView image = (ImageView) view.findViewById(R.id.image_id);
Resources resources = this.getContext().getResources();
image.setImageDrawable(resources.getDrawable(R.drawable.ic_launcher));
Button btn = (Button) view.findViewById(R.id.button_id);
Button btn2 = (Button) view.findViewById(R.id.button_id_2);
Button btn3 = (Button) view.findViewById(R.id.button_id_3);
ol = new OnItemClickListener(position, item);
btn.setOnClickListener(ol);
btn.setTag(1);
btn2.setOnClickListener(ol);
btn2.setTag(2);
btn3.setOnClickListener(ol);
btn3.setTag(3);
return view;
}
private class OnItemClickListener implements OnClickListener {
private int _position;
private TestItemModel _ti;
public OnItemClickListener(int position, TestItemModel ti) {
_position = position;
_ti = ti;
}
// TODO
// provide functionality for which button was clicked then pass the item
// to which it was clicked in.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_id:
// btn clicked
Toast.makeText(getContext(),
" Button1 clicked at positon" + v.getTag(),
Toast.LENGTH_SHORT).show();
break;
case R.id.button_id_2:
// btn2 clicked
Toast.makeText(getContext(),
" Button2 clicked at positon" + v.getTag(),
Toast.LENGTH_SHORT).show();
break;
case R.id.button_id_3:
Toast.makeText(getContext(),
" Button3 clicked at positon" + v.getTag(),
Toast.LENGTH_SHORT).show();
// btn 3 clciked
break;
}
// the view is the button, so you get get the tag it has set with
// v.getTag() to know what button is pressed.
Log.v("YOOO",
"Button Click at position " + _position + " " + v.getTag()
+ " Item ID = " + _ti.getId());
}
}
}
TestListLoaderAsync
public class TestListLoaderAsync extends AsyncTaskLoader<List<TestItemModel>> {
List<TestItemModel> _models;
public TestListLoaderAsync(Context context) {
super(context);
}
#Override
public List<TestItemModel> loadInBackground() {
System.out.println("TestListLoader.loadInBackground");
// You should perform the heavy task of getting data from
// Internet or database or other source
// Here, we are generating some Sample data
// Create corresponding array of entries and load with data.
List<TestItemModel> entries = new ArrayList<TestItemModel>(5);
entries.add(new TestItemModel("Java", "1"));
entries.add(new TestItemModel("C++", "2"));
entries.add(new TestItemModel("Python", "3"));
entries.add(new TestItemModel("JavaScript", "4"));
entries.add(new TestItemModel("D", "5"));
entries.add(new TestItemModel("C", "6"));
entries.add(new TestItemModel("Perl", "7"));
entries.add(new TestItemModel("Fortran", "8"));
entries.add(new TestItemModel("Cobalt", "9"));
entries.add(new TestItemModel("Ruby", "10"));
entries.add(new TestItemModel("Pascal", "11"));
entries.add(new TestItemModel("HTML", "12"));
entries.add(new TestItemModel("CSS", "13"));
entries.add(new TestItemModel("PHP", "14"));
entries.add(new TestItemModel("MYSQL", "15"));
TestItemModel lastItem = new TestItemModel("C#", "16");
lastItem.setId("TestingId");
entries.add(lastItem);
return entries;
}
/**
* Called when there is new data to deliver to the client. The super class
* will take care of delivering it; the implementation here just adds a
* little more logic.
*/
#Override
public void deliverResult(List<TestItemModel> listOfData) {
if (isReset()) {
// An async query came in while the loader is stopped. We
// don't need the result.
if (listOfData != null) {
onReleaseResources(listOfData);
}
}
List<TestItemModel> oldApps = listOfData;
_models = listOfData;
if (isStarted()) {
// If the Loader is currently started, we can immediately
// deliver its results.
super.deliverResult(listOfData);
}
// At this point we can release the resources associated with
// 'oldApps' if needed; now that the new result is delivered we
// know that it is no longer in use.
if (oldApps != null) {
onReleaseResources(oldApps);
}
}
/**
* Handles a request to start the Loader.
*/
#Override
protected void onStartLoading() {
if (_models != null) {
// If we currently have a result available, deliver it
// immediately.
deliverResult(_models);
}
if (takeContentChanged() || _models == null) {
// If the data has changed since the last time it was loaded
// or is not currently available, start a load.
forceLoad();
}
}
/**
* Handles a request to stop the Loader.
*/
#Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
/**
* Handles a request to cancel a load.
*/
#Override
public void onCanceled(List<TestItemModel> apps) {
super.onCanceled(apps);
// At this point we can release the resources associated with 'apps'
// if needed.
onReleaseResources(apps);
}
/**
* Handles a request to completely reset the Loader.
*/
#Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
// At this point we can release the resources associated with 'apps'
// if needed.
if (_models != null) {
onReleaseResources(_models);
_models = null;
}
}
/**
* Helper function to take care of releasing resources associated with an
* actively loaded data set.
*/
protected void onReleaseResources(List<TestItemModel> apps) {
}
}
TestItemModel
public class TestItemModel {
private String name;
private String id;
public TestItemModel(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Keep in mind this is all in testing phase, dummy state, so I know some things should be cleaned up, etc.. that was all part of the plan when I got this to work. Thanks.
There are many libraries that can help you out with this, as an alternative to rolling your own. Check out Picasso by Square, for one possible solution.
Use Universal Image Loader for that.. It is a effective library to download images from URL Asynchronously.
https://github.com/nostra13/Android-Universal-Image-Loader

Categories

Resources