大家好,我很新,可以使用Firebase Firestore在Android Studio上开发完整的应用程序。我正在YouTube上使用firestores文档跟踪视频,而我想做的就是简单地从“调查”集合的“ Anket”集合中获取所有文档,并在弹出屏幕上列出它们。谢谢您的时间和友好的回答
MainFragmentActivity.java:
public class MainFragmentActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private SectionPageAdapter mSectionPageAdapter;
private ViewPager mViewPager;
FirebaseAuth mAuth = FirebaseAuth.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment2);
Log.d(TAG,"onCreate:Starting");
mViewPager = findViewById(R.id.container);
setupViewPager(mViewPager);
mViewPager.setOffscreenPageLimit(4);
mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager){
SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
adapter.addFragment(new ProfileFragment(), "Profile");
adapter.addFragment(new MessageFragment(), "Messages");
adapter.addFragment(new MatchFragment(), "Matches");
adapter.addFragment(new ShopFragment(), "Shop");
viewPager.setAdapter(adapter);
}
public void logoutUser(View view) {
mAuth.signOut();
Intent intent = new Intent (MainFragmentActivity.this, ChooseLoginRegistration.class);
startActivity(intent);
finish();
return;
}
public void openUpAnket (View view){
Intent intent = new Intent(MainFragmentActivity.this,Pop.class);
startActivity(intent);
}
}
Pop.java
public class Pop extends Activity implements Serializable {
private FirebaseFirestore surveyDb = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anket_pop_up);
surveyDb.collection("Surveys").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<Anket> mAnketList = new ArrayList<>();
if(task.isSuccessful()){
for(QueryDocumentSnapshot document : task.getResult()){
Log.e("zaa",document.getId() + "=>"+ document.getData());
Anket anket = document.toObject(Anket.class);
mAnketList.add(anket);
}
ListView mAnketListView = findViewById(R.id.anket_list);
AnketItemAdapter mAnketItemAdapter = new AnketItemAdapter(this ,mAnketList);
mAnketListView.setAdapter(mAnketItemAdapter);
}else{
Log.e("ZZAAAA","erör",task.getException());
}
}
});
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.85),(int)(height*.9));
}
AnketItemAdapter.java
public class AnketItemAdapter extends ArrayAdapter<Anket> {
public AnketItemAdapter(OnCompleteListener<QuerySnapshot> context, List<Anket> object){
super((Context) context,0, object);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.anket_custom_item,parent,false);
}
TextView mAnketSorusu = (TextView) convertView.findViewById(R.id.anket_soru);
Button cvp1 = convertView.findViewById(R.id.cvp1);
Button cvp2 = convertView.findViewById(R.id.cvp2);
Anket anket = getItem(position);
mAnketSorusu.setText(anket.getQuestion());
cvp1.setText(anket.getAnswer1());
cvp2.setText(anket.getAnswer2());
return convertView;
}
}
Logcat
Process: com.example.anyzonev01, PID: 678
java.lang.ClassCastException: com.example.anyzonev01.Pop$1 cannot be cast to android.content.Context
at com.example.anyzonev01.AnketItemAdapter.<init>(AnketItemAdapter.java:21)
at com.example.anyzonev01.Pop$1.onComplete(Pop.java:50)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:0)
AnketItemAdapter mAnketItemAdapter = new AnketItemAdapter(this ,mAnketList);
此此处是指您创建的OnCompleteListener
的实例,而不是Activity
(它是上下文)。尝试使用Pop.this
,它将引用外部Activity实例。