I have a main list in which I am showing some options. In this I have one option of events which is added at 2nd index in main list. I have events arrayList and I get this list from asyncTask. So I get the Size of eventsArrayList in onPostExecute method.
But I need to show this count in item of main List. I tried to call prepareData method in onPstExceute after I get the size of an array. But it then takes much time to load show the main UI as it is connected with the server.
So I thought to first show the eventArrayList size as 0 and the update the main list Item in onPostexecute method with the new size.
But this dose not change the count shown in the main list. I tried invalidate() on recyclerview still I could not refresh the recycler view.
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,GetEventsAsyncTask.GetEvents {
private RecyclerView recyclerView;
private List<MainList> mainLists = new ArrayList<>();
private String userUsername;
public int PERMISSIONS_WRITE_EXTERNAL_STORAGE =10;
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
ArrayList<Event> eventArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedpreferences = getSharedPreferences("username",Context.MODE_PRIVATE);
userUsername = sharedpreferences.getString("UserUsername","No name defined");
GetEventsAsyncTask getEventsAsyncTask = new GetEventsAsyncTask(this,MainActivity.this,eventArrayList);
getEventsAsyncTask.execute(userUsername);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
eventArrayList = new ArrayList<>();
prepareData();
recyclerView.setAdapter(new MainListAdapter(mainLists, new MainListAdapter.OnItemClickListener() {
@Override public void onItemClick(MainList item) {
if(item.getTitle().equals("Plan Event")) {
startActivity(new Intent(MainActivity.this, PlanEventActivity.class));
}
if(item.getTitle().equals("Events")) {
startActivity(new Intent(MainActivity.this, EventsListActivity.class));
}
if(item.getTitle().equals("Photos")) {
photos = true;
checkFilePermissions();
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
permissionsAccepted = sharedpreferences.getBoolean("permission",false);
if(dir.exists()) {
listFile = dir.listFiles();
if (permissionsAccepted) {
if (listFile.length == 0) {
showAlert("No Images");
} else {
new MainActivity.SingleMediaScanner(MainActivity.this, listFile[0]);
}
}
else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
showAlert("Please accept permissions to get access to photos.");
}
}
else {
showAlert("No Folder");
}
}
if(item.getTitle().equals("Reminders")) {
startActivity(new Intent(MainActivity.this, RemindersActivity.class));
}
if(item.getTitle().equals("Chat")) {
}
if(item.getTitle().equals("Notes")) {
startActivity(new Intent(MainActivity.this, NotesActivity.class));
}
if(item.getTitle().equals("Notifications")) {
startActivity(new Intent(MainActivity.this, NotificationsActivity.class));
}
if(item.getTitle().equals("Profile")) {
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
}
}
}));
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
private void prepareData() {
MainList mainList = new MainList("Plan Event","", R.drawable.plan);
mainLists.add(mainList);
if(eventArrayList != null && eventArrayList.size() >= 0)
{
mainList = new MainList("Events",String.valueOf(eventArrayList.size()), R.drawable.events);
}
else
{
mainList = new MainList("Events", "0", R.drawable.events);
}
mainLists.add(mainList);
listFile = dir.listFiles();
if(listFile != null && listFile.length >= 0)
{
mainList = new MainList("Photos", String.valueOf(listFile.length), R.drawable.phone);
}
else
{
mainList = new MainList("Photos", "0", R.drawable.phone);
}
mainLists.add(mainList);
mainList = new MainList("Reminders","20", R.drawable.remind);
mainLists.add(mainList);
mainList = new MainList("Chat","2", R.drawable.chat);
mainLists.add(mainList);
mainList = new MainList("Notes","2", R.drawable.notes);
mainLists.add(mainList);
mainList = new MainList("Notifications","20", R.drawable.remind);
mainLists.add(mainList);
mainList = new MainList("Profile","50%", R.drawable.user);
mainLists.add(mainList);
}
@Override
public void doPostExecute(ArrayList<Event> response) {
eventArrayList = response;
MainList mainList = new MainList("Events","eventArrayList.size()", R.drawable.events);
mainLists.set(2,mainList);
recyclerView.invalidate();
}
}
What to do? Please help. Thank you.