我正在尝试使用asink任务从后台firebase下载数据并使用它们来创建非专有的firebase recyclerview。然而,postexecute之后的滞后仍然相同。 我不知道我做的是对的。
我做错了什么?
公共类FriendFragmentFollow扩展了Fragment {
FriendHoldAdapter3 adapter;
RecyclerView recyclerView;
ArrayList<FriendItem> username;
ArrayList<String> uid, url;
DatabaseReference mDatabase;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.friend_fragment_follow, container, false);
}
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SearchView search = (SearchView) view.findViewById(R.id.ciao);
mDatabase = FirebaseDatabase.getInstance().getReference();
recyclerView = (RecyclerView) view.findViewById(R.id.friend_list2);
//recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setLayoutManager(new GridLayoutManager(view.getContext(), 2));
new LongOperation().execute("");
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.equals("")){
newText = " ";
}
if (newText.equals("View All")){
newText = "";
}
final List<FriendItem> filteredModelList = filter(username, newText);
adapter.setFilter(filteredModelList);
return true;
}
});
}
public String getUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
final DatabaseReference mPostReference = FirebaseDatabase.getInstance().getReference().child("user-profile");
mPostReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
username = new ArrayList<>();
uid = new ArrayList<String>();
url = new ArrayList<String>();
for (DataSnapshot data : dataSnapshot.getChildren()){
final FriendItem friendItem = data.getValue(FriendItem.class);
//final String friendItem2 = data.getKey();
username.add(friendItem);
//uid.add(friendItem2);
adapter = new FriendHoldAdapter3(getContext(), username, uid, url);
//adapter.setClickListener(FriendActivity3.this);
recyclerView.setAdapter(adapter);
/**
adapter = new FriendHoldAdapter2(FriendActivity2.this, username, uid);
adapter.setClickListener(FriendActivity2.this);
recyclerView.setAdapter(adapter);
**/
final List<FriendItem> filteredModelList = filter(username, " ");
adapter.setFilter(filteredModelList);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}});
return "Executed";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getContext(), "Post execute", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
答案 0 :(得分:1)
您应该在onDataChange()方法中使用AyncTask。从onDataChanged获取数据后,您只需要在后台线程中执行处理部分。尝试
final DatabaseReference mPostReference = FirebaseDatabase.getInstance()
.getReference().child("user-profile");
mPostReference.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
new LongOperation().execute(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
private class LongOperation extends AsyncTask<DataSnapshot, Void, String> {
@Override
protected String doInBackground(DataSnapshot... params) {
for (DataSnapshot data : params[0].getChildren()){
final FriendItem friendItem = data.getValue(FriendItem.class);
//final String friendItem2 = data.getKey();
username.add(friendItem);
//uid.add(friendItem2);
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
adapter = new FriendHoldAdapter3(getContext(), username, uid, url);
//adapter.setClickListener(FriendActivity3.this);
recyclerView.setAdapter(adapter);
/**
adapter = new FriendHoldAdapter2(FriendActivity2.this, username, uid);
adapter.setClickListener(FriendActivity2.this);
recyclerView.setAdapter(adapter);
**/
final List<FriendItem> filteredModelList = filter(username, " ");
adapter.setFilter(filteredModelList);
Toast.makeText(getContext(), "Post execute", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
username = new ArrayList<>();
uid = new ArrayList<String>();
url = new ArrayList<String>();
}
@Override
protected void onProgressUpdate(Void... values) {}
}