kamanzi abubakar kamanzi abubakar - 2 months ago 39
Android Question

AsyncTask and Firebase

I am trying to load a ListView using a RecyclerView, but only the progress bar is the one loading and the list is not showing . I'm using Firebase in the AsyncTask class. Where am I going wrong? Below is my code.

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

public static String KEY_HEADLINES="headlines";
public static String KEY_DETAILS="details";

ProgressBar pb;

public List<NewsModel> newslist;

public NewsAdapter2 adapter;
private RecyclerView mRecyclerView;

private DatabaseReference mRef;
ImageView image_news;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// call the array list
newslist = new ArrayList<NewsModel>();

pb = (ProgressBar) findViewById(R.id.progressBarNews);
image_news =(ImageView) findViewById(R.id.image_news);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Enabling offline capabilities
FirebaseDatabase.getInstance().setPersistenceEnabled(true);

// firebase initialisation......
mRef = FirebaseDatabase.getInstance().getReference("News");
// keep my data synced
mRef.keepSynced(true);


//firebase

//initializing async task
new MyTask();

// load data

//declare the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

public void LoadData (DataSnapshot dataSnapshot){
System.out.println(dataSnapshot.getValue());

NewsModel news_model =dataSnapshot.getValue(NewsModel.class);

newslist.add(news_model);

adapter = new NewsAdapter2(MainActivity.this, newslist);
mRecyclerView.setAdapter(adapter);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Intent i;
if (id == R.id.hospitals) {
i = new Intent(MainActivity.this, HealthCentres.class);
startActivity(i);
} else if (id == R.id.doctors) {
i = new Intent(MainActivity.this, Doctors.class);
startActivity(i);

} else if (id == R.id.tips) {
i = new Intent(MainActivity.this, Tips.class);
startActivity(i);

}
else if (id == R.id.contacts) {
i= new Intent(MainActivity.this, ContactUs.class);
startActivity(i);

}
else if (id == R.id.about) {
i = new Intent(MainActivity.this, AboutUs.class);
startActivity(i);
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

private class MyTask extends AsyncTask<String,String,String>
{

@Override
protected void onPreExecute() {

pb.setVisibility(View.VISIBLE);
super.onPreExecute();
}


@Override
protected String doInBackground(String... params) {
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
LoadData(dataSnapshot);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
LoadData(dataSnapshot);
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) { }
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
@Override
public void onCancelled(DatabaseError databaseError) { }
});

return null;
}

@Override
protected void onPostExecute(String s) {
pb.setVisibility(View.INVISIBLE);
super.onPostExecute(s);
}
}















}

Answer

Firebase has already it's Asynchronous litsener , so what you can do put this in the onCreate Method. But you said that the progress bar is the only visible , just also add this code of line in the onChildEventLitsener() method pb.setVisibility(View.INVISIBLE);, so that when data comes the Progress bar dissapears.

mRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                pb.setVisibility(View.INVISIBLE);
                LoadData(dataSnapshot);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                LoadData(dataSnapshot);
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Hope this works well.