在 Firebase 回收器适配器中,OnBindViewHolder 和 onCreateViewHolder 从未调



我正在编写一个 android 应用程序,我需要在其中使用回收器视图显示数据列表。我正在使用带有读写规则的Firebase实时数据库true,但问题onBindViewHolder,并且从未像日志所说的那样调用onCreateViewHolder

模态类:连接列表项.java

public class ConnectedListItem {
String name;
Double lat , lng;
public ConnectedListItem() {
}
public ConnectedListItem(String name, Double lat, Double lng) {
this.name = name;
this.lat = lat;
this.lng = lng;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
}

连接活动.xml :

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ConnectionActivity">
<include
android:id="@+id/connection_toolbar"
layout="@layout/toolbar_layout" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="15dp"
android:fontFamily="@font/open_sans"
android:text="Connection"
android:textAllCaps="false"
android:textSize="35sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/emptyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="No Connection"
android:visibility="invisible" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/connected_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<Button
android:id="@+id/connection_child_add_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/login_screen_button_background"
android:text="Add Child"
android:textColor="@color/white" />
</LinearLayout>

单用户布局 :connected_single_layout.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/login_sign_up_edit_text_background"
android:orientation="vertical">
<TextView
android:id="@+id/connectedName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:text="Name" />
<TextView
android:id="@+id/connectedLastLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:text="Last Location" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/removeBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_weight="1"
android:text="Remove" />
<Button
android:id="@+id/trackB only oncetn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_weight="1"
android:text="Track" />
</LinearLayout>
</LinearLayout>

连接活动.java :

public class ConnectionActivity extends AppCompatActivity {
private String TAG = "ConnectionActivity";
private RecyclerView mChildConnectedListRecyclerView;
private androidx.appcompat.widget.Toolbar mToolbar;
private TextView mEmptyTextView;
private DatabaseReference mConnectedUserDatabase , mUserDatabase;
private FirebaseAuth mAuth;
private String mCurrentUser;
private Button mAddChildBtn;
private Geocoder geocoder;
FirebaseRecyclerAdapter<ConnectedListItem , ConnectedViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connection);
Log.d(TAG, "onCreate: called");
mAuth = FirebaseAuth.getInstance();
setUpToolbar();
if(mAuth.getCurrentUser() != null){
mCurrentUser = mAuth.getCurrentUser().getUid();
Log.d("CurrentUser", mCurrentUser);
}
mAddChildBtn = (Button) findViewById(R.id.connection_child_add_btn);
mConnectedUserDatabase = FirebaseDatabase.getInstance().getReference().child("Connected").child(mCurrentUser);
Log.d(TAG, "onCreate: " + mCurrentUser);
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mChildConnectedListRecyclerView = (RecyclerView) findViewById(R.id.connected_list_view);
mChildConnectedListRecyclerView.setHasFixedSize(true);
mChildConnectedListRecyclerView.setLayoutManager(new LinearLayoutManager(this));

mEmptyTextView = (TextView) findViewById(R.id.emptyTextView);
mAddChildBtn.setOnClickListener(v -> {
// Some code here
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: called");
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<ConnectedListItem>()
.setQuery(mConnectedUserDatabase , ConnectedListItem.class)
.build();
Log.d(TAG, "onStart: Firebase option done");
adapter = new FirebaseRecyclerAdapter<ConnectedListItem, ConnectedViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ConnectedViewHolder connectedViewHolder, int i, @NonNull ConnectedListItem connectedListItem) {

Log.d(TAG, "onBindViewHolder: called" + connectedListItem.getName());
connectedViewHolder.mChildName.setText(connectedListItem.getName());
Double mLat = connectedListItem.getLat();
Double mLng = connectedListItem.getLng();
// geocoding starts here
String errorMessage = "";
geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
mLat,
mLng,
// In this sample, get just a single address.
1);
} catch (IOException e) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, e);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + mLat +
", Longitude = " +
mLng, illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for (int j = 0; j <= address.getMaxAddressLineIndex(); j++) {
addressFragments.add(address.getAddressLine(j));
}
// Log.i(TAG, getString(R.string.address_found));
connectedViewHolder.mChildAddress.setText(TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
}
@NonNull
@Override
public ConnectedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.connected_single_layout , parent, false);
ConnectedViewHolder connectedViewHolder = new ConnectedViewHolder(view);

return connectedViewHolder;
}
@Override
public int getItemCount() {
return super.getItemCount();
}
};
mChildConnectedListRecyclerView.setAdapter(adapter);
Log.d(TAG, "onStart: Adapter setuped");
adapter.startListening();
Log.d(TAG, "onStart: Start listening called");
}
@Override
protected void onDestroy() {
super.onDestroy();
adapter.stopListening();
Log.d(TAG, "onDestroy: Called Stop");
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
protected void onResume() {
super.onResume();
adapter.startListening();
Log.d(TAG, "onResume: called resume");
}
public static class ConnectedViewHolder extends RecyclerView.ViewHolder {
TextView mChildName , mChildAddress;
Button mTrackBtn , mRemoveBtn;
private ConnectedViewHolder(@NonNull View itemView) {
super(itemView);
mChildAddress = itemView.findViewById(R.id.connectedLastLocation);
mChildName = itemView.findViewById(R.id.connectedName);
mTrackBtn = (Button) itemView.findViewById(R.id.trackBtn);
mRemoveBtn = (Button) itemView.findViewById(R.id.removeBtn);
}
}
}

应用程序级构建.gradle :

//Firebase dependencies here
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-auth:17.0.0'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'com.firebaseui:firebase-ui-database:5.0.0'

日志猫 :

ConnectionActivity: onCreate: called
ConnectionActivity: onCreate: 8zQPcYnUHnQBtRetg4vkylXJI1O2
ConnectionActivity: onStart: called
ConnectionActivity: onStart: Firebase option done
ConnectionActivity: onStart: Adapter setuped
ConnectionActivity: onStart: Start listening called
ConnectionActivity: onResume: called resume
Logging event (FE): screen_view(_vs), 
Bundle[{firebase_event_origin(_o)=auto, 
firebase_previous_class(_pc)=ParentHomeActivity, 
firebase_previous_id(_pi)=4706309106774484658, 
firebase_screen_class(_sc)=ConnectionActivity, 
firebase_screen_id(_si)=4706309106774484659}]
Logging event (FE): user_engagement(_e), 
Bundle[{firebase_event_origin(_o)=auto, 
engagement_time_msec(_et)=8849, 
firebase_screen_class(_sc)=ConnectionActivity, 
firebase_screen_id(_si)=4706309106774484659}]
Logging event (FE): screen_view(_vs), 
Bundle[{firebase_event_origin(_o)=auto, 
firebase_previous_class(_pc)=ConnectionActivity, 
firebase_previous_id(_pi)=4706309106774484659, 
firebase_screen_class(_sc)=ParentHomeActivity, 
firebase_screen_id(_si)=4706309106774484658}]
ConnectionActivity: onDestroy: Called Stop

我在stackOverflow上搜索了其他问题,但没有一个对我有用。请帮忙

我终于通过删除这一行来解决它mChildConnectedListRecyclerView.setHasFixedSize(true);

相关内容

  • 没有找到相关文章

最新更新