- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为 RecyclerView 创建了一个 FirebaseRecylerAdapter
,我的问题是 FirebaseRecyclerAdapter
没有调用 onCreateViewHolder
(因此 recyclerView 什么都不显示)。我在一些帖子上发现这是因为 getItemCount = 0
(在我的项目开始时我没有 getItemCount
方法)。所以我做到了,但它并没有解决我的问题,因为 getItemCount
返回 0,我不明白为什么。我可以正确地写入我的数据库,所以我知道我的数据库已成功连接,我运行到 Debug模式,我们可以看到。
主要 Activity .java:
public class MainActivity extends AppCompatActivity {
private RecyclerView mTaskList;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mTaskList = findViewById(R.id.task_list);
//mTaskList.setHasFixedSize(true);
mTaskList.setLayoutManager(new LinearLayoutManager(this));
mDatabase = FirebaseDatabase.getInstance().getReference().child("Tasks");
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddTaskActivity.class);
startActivity(intent);
}
});
}
public static class TaskViewHolder extends RecyclerView.ViewHolder{
public TaskViewHolder(View itemView) {
super(itemView);
View mView = itemView;
}
public void setName(String name)
{
TextView task_name = itemView.findViewById(R.id.task_name);
task_name.setText(name);
}
/*public void setTime(String time)
{
TextView task_time = itemView.findViewById(R.id.task_time);
task_time.setText(time);
}*/
}
@Override
protected void onStart() {
super.onStart();
Log.d("POS", "onStart: ");
FirebaseRecyclerOptions<Task> options =
new FirebaseRecyclerOptions.Builder<Task>()
.setQuery(mDatabase, Task.class)
.build();
FirebaseRecyclerAdapter<Task, TaskViewHolder> array = new FirebaseRecyclerAdapter<Task, TaskViewHolder>(options) {
@Override
public int getItemCount() {
Log.d("NB", "getItemCount: " + super.getItemCount());
return super.getItemCount();
}
@Override
protected void onBindViewHolder(TaskViewHolder holder, int position, Task model) {
holder.setName(model.getName());
Log.d("BIND", "onBindViewHolder: pass");
//holder.setTime(model.getTime());
}
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,
parent, false);
Log.d("CREATE", "onCreate: pass");
return new TaskViewHolder(view);
}
};
array.notifyDataSetChanged();
mTaskList.setAdapter(array);
}
任务.java :
public class Task {
private String name;
//private String time;
public Task(String name){
this.name = name;
//this.time = time;
}
public Task(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
添加任务 Activity .java :
public class AddTaskActivity extends AppCompatActivity {
private FirebaseDatabase db;
DatabaseReference myRef;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_task, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
editText = findViewById(R.id.desc);
String desc = editText.getText().toString();
//noinspection SimplifiableIfStatement
if (id == R.id.action_save) {
myRef = db.getInstance().getReference().child("Tasks");
DatabaseReference newTask = myRef.push();
newTask.child("name").setValue(desc);
AddTaskActivity.this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
在此先感谢您,如果您需要更多信息,请告诉我。
和我启动 mainActivity 时的 logcat:
01-15 10:57:57.830 18882-18882/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2], Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0009, [-1 -1 -1 -1 0 1]
01-15 10:57:57.832 18882-18882/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.thauvi_a.todolist
01-15 10:57:57.834 18882-18882/? I/art: Late-enabling -Xcheck:jni
01-15 10:57:57.854 18882-18882/? D/TimaKeyStoreProvider: TimaKeyStore is enabled: try add TimaSignature Service and generateKeyPair Service
01-15 10:57:57.883 18882-18882/? I/ResourcesManager: updateResourcesForOpenThemeChange for Desktop mode
01-15 10:57:57.994 18882-18882/com.thauvi_a.todolist W/System: ClassLoader referenced unknown path: /data/app/com.thauvi_a.todolist-2/lib/arm64
01-15 10:57:58.013 18882-18882/com.thauvi_a.todolist D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
01-15 10:57:58.019 18882-18882/com.thauvi_a.todolist D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
01-15 10:57:58.063 18882-18882/com.thauvi_a.todolist I/FirebaseInitProvider: FirebaseApp initialization successful
01-15 10:57:58.066 18882-18882/com.thauvi_a.todolist I/InstantRun: starting instant run server: is main process
01-15 10:57:58.071 18882-18898/com.thauvi_a.todolist I/FA: App measurement is starting up, version: 11910
01-15 10:57:58.071 18882-18898/com.thauvi_a.todolist I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
01-15 10:57:58.072 18882-18898/com.thauvi_a.todolist I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.thauvi_a.todolist
01-15 10:57:58.072 18882-18898/com.thauvi_a.todolist D/FA: Debug-level message logging enabled
01-15 10:57:58.110 18882-18882/com.thauvi_a.todolist W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-15 10:57:58.190 18882-18882/com.thauvi_a.todolist D/TextView: setTypeface with style : 0
01-15 10:57:58.190 18882-18882/com.thauvi_a.todolist D/TextView: setTypeface with style : 0
01-15 10:57:58.210 18882-18882/com.thauvi_a.todolist D/POS: onStart:
01-15 10:57:58.221 18882-18898/com.thauvi_a.todolist I/FA: Tag Manager is not found and thus will not be used
01-15 10:57:58.221 18882-18898/com.thauvi_a.todolist D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=8779723142852401023}]
01-15 10:57:58.222 18882-18882/com.thauvi_a.todolist D/Choreographer: init sf_choreo_doframe debug_Level : 0x4f4cdebug_game_running : false
01-15 10:57:58.224 18882-18882/com.thauvi_a.todolist I/SemDesktopModeManager: registerListener: android.view.ViewRootImpl$3@3835360
01-15 10:57:58.224 18882-18901/com.thauvi_a.todolist I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
01-15 10:57:58.224 18882-18901/com.thauvi_a.todolist I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
01-15 10:57:58.229 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: ThreadedRenderer.create() translucent=false
01-15 10:57:58.229 18882-18901/com.thauvi_a.todolist W/System: ClassLoader referenced unknown path:
01-15 10:57:58.232 18882-18882/com.thauvi_a.todolist D/InputTransport: Input channel constructed: fd=74
01-15 10:57:58.232 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: setView = DecorView@4b0d4ea[MainActivity] touchMode=true
01-15 10:57:58.241 18882-18901/com.thauvi_a.todolist W/System: ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/00000035/n/arm64-v8a
01-15 10:57:58.245 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: dispatchAttachedToWindow
01-15 10:57:58.263 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][1440,2960] result=0x27 surface={isValid=true 532786037248} surfaceGenerationChanged=true
01-15 10:57:58.263 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: mHardwareRenderer.initialize() mSurface={isValid=true 532786037248} hwInitialized=true
01-15 10:57:58.272 18882-18904/com.thauvi_a.todolist D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-15 10:57:58.272 18882-18882/com.thauvi_a.todolist D/NB: getItemCount: 0
01-15 10:57:58.272 18882-18882/com.thauvi_a.todolist D/NB: getItemCount: 0
01-15 10:57:58.289 18882-18902/com.thauvi_a.todolist D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
01-15 10:57:58.295 18882-18882/com.thauvi_a.todolist W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
01-15 10:57:58.306 18882-18902/com.thauvi_a.todolist I/OpenGLRenderer: Initialized EGL, version 1.4
01-15 10:57:58.307 18882-18902/com.thauvi_a.todolist D/OpenGLRenderer: Swap behavior 1
01-15 10:57:58.310 18882-18902/com.thauvi_a.todolist D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1440x2960]-format:1
01-15 10:57:58.364 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 96 - 0, 192) vi=Rect(0, 96 - 0, 192) or=1
01-15 10:57:58.364 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
01-15 10:57:58.364 18882-18882/com.thauvi_a.todolist D/ViewRootImpl@e91f019[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 532786037248}
01-15 10:57:58.364 18882-18882/com.thauvi_a.todolist I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
01-15 10:57:58.367 18882-18904/com.thauvi_a.todolist D/TcpOptimizer: TcpOptimizer-ON
01-15 10:57:58.370 18882-18898/com.thauvi_a.todolist D/FA: Connected to remote service
最佳答案
您需要调用FirebaseRecyclerAdapter#startListening()
方法来启动数据监听器。最好在 Activity 的 onStart()
方法中启动监听器,然后在 onStop()
方法中移除监听器。
顺便说一下,您可以在 onCreate()
方法中移动适配器声明以获得更好的应用程序性能。
private RecyclerView mTaskList;
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<Task, TaskViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTaskList = findViewById(R.id.task_list);
mTaskList.setLayoutManager(new LinearLayoutManager(this));
mDatabase = FirebaseDatabase.getInstance().getReference().child("Tasks");
FirebaseRecyclerOptions<Task> options = new FirebaseRecyclerOptions.Builder<Task>()
.setQuery(mDatabase, Task.class)
.build();
adapter = new FirebaseRecyclerAdapter<Task, TaskViewHolder>(options) {
@Override
protected void onBindViewHolder(TaskViewHolder holder, int position, Task model) { }
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { }
};
mTaskList.setAdapter(adapter);
}
下面是启动和停止数据监听器的代码
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
希望这有帮助:)
关于java - FirebaseRecyclerAdapter getItemCount = 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48254655/
我遇到以下问题,不幸的是,我的应用程序在进入下一个 Activity 时停止了。罪魁祸首是: public class RecyclerAdapter extends RecyclerView.
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我调查了关于这个问题的几个 SO 答案(here、here、and here),但没有一个提议的解决方案有效。我的问题是没有显示我的 RecyclerView 列表项。我在 MessengerRecy
我有一个 recyclerView,它让我崩溃: 这是我的开始 Activity : public class StartActivity extends AppCompatActivity {
在您将问题标记为重复之前请阅读问题... 所以我正在尝试使用 Intent 从图库中选择多张图片。当我选择多于 1 张图片时,它工作正常,当我只选择 1 张图片时,它会生成此错误。我寻找了每个 Nul
我为 RecyclerView 创建了一个 FirebaseRecylerAdapter,我的问题是 FirebaseRecyclerAdapter 没有调用 onCreateViewHolder(因
我想在任何给定时间获取“Activity ”键的计数。现在根据 API 文档 getItemCount() 应该返回此值。 然而事实并非如此。过期的键不会减少 getItemCount() 的值。为什
我查看了 SO 上关于此的其他问题,并逐字逐句匹配,但我一定错过了一些东西,因为没有任何效果。 getItemCount() 返回零,但我调试了我的数据列表,它似乎正确地添加了我的所有数据,所以与适配
我创建了一个工作正常的适配器。现在,我需要计算适配器中数据项的数量。我正在使用函数 getItemcount() ,但我每次都得到 0。另外,我正在使用 Firebase 来处理我的数据库。 fi
我刚刚制作了一个回收器 View ,但它没有工作,所以我在 getItemCount 上设置了一个断点,但该方法没有被调用。我从未见过 SO 上的任何其他人有这个特定问题,我相信这是非常明显的事情。这
如果 RecyclerView 为空,我会尝试显示 TextView。但是 ListAdapter 的 getItemCount() 总是返回 0,即使 RecyclerView 显示项目也是如此。
RecyclerView 有一个 InfiniteScrollListener 并且在该监听器内部,在 OnScrolled() 方法中我计算可见项和总项数以检查是否应加载新项。当totalItemC
请调查一下,让我知道哪里出了问题。 public class MyAdapter extends RecyclerView.Adapter { private ItemData[] items
本文整理了Java中com.tc.admin.common.XComboBox.getItemCount()方法的一些代码示例,展示了XComboBox.getItemCount()的具体用法。这些代
本文整理了Java中org.jfree.data.xy.XYIntervalSeries.getItemCount()方法的一些代码示例,展示了XYIntervalSeries.getItemCoun
本文整理了Java中org.jfree.data.xy.YIntervalSeries.getItemCount()方法的一些代码示例,展示了YIntervalSeries.getItemCount(
我遇到了一个奇怪的错误,如果 getItemCount 返回 1(数据结构的大小),适配器不会显示任何卡片。 我的适配器代码 - public class SingleAdapter extends
我知道我的问题已被问过好几次,但我已经搜索了一段时间,但没有一个答案能解决我的问题。问题是,当我在我的“HomeFragment”中创建 recyclerview 适配器和方法“onViewCreat
我从使用 FlatList 切换到 VirtualizedList,并收到错误 this.props.getItemCount 不是函数。当我运行调试器时,VirtualizedList.js 抛出异
文档对 getItemCount() 的描述如下: Returns the number of items in the adapter bound to the parent RecyclerVie
我是一名优秀的程序员,十分优秀!