gpt4 book ai didi

android - AsyncTask 和 Firebase

转载 作者:行者123 更新时间:2023-11-29 19:32:18 26 4
gpt4 key购买 nike

我正在尝试使用 RecyclerView 加载 ListView,但加载的只有进度条,列表未显示。我在 AsyncTask 类中使用 Firebase。我哪里错了?下面是我的代码。

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);
}
}















}

最佳答案

Firebase 已经有了它的异步 litsener ,所以你可以把它放在 onCreate 方法中。但是你说进度条是唯一可见的,只是在onChildEventLitsener()方法pb.setVisibility(View.INVISIBLE);中也加入了这行代码,所以当数据到来时,进度条消失。

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) {

}
});

希望这一切顺利。

关于android - AsyncTask 和 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39744213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com