如何在未指定final的情况下使用变量持有人。实际上我很困惑,因为我在Activity中已经读过,不鼓励使用构造函数。有人可以向我解释吗?
在下面,我指定了最终的持有人,结果是我的价值在我的recyclerview中的每个字段上都是相同的。
public class LocationActivity extends AppCompatActivity {
private static final String TAG = "LocationActivitynull";
private RecyclerView.LayoutManager layoutManager;
private Query query;
private RecyclerView recyclerView;
public FirebaseRecyclerAdapter<LocationContent,LocationViewHolder> firebaseRecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// Write a message to the database
query = FirebaseDatabase.getInstance()
.getReference()
.child("Locations")
.limitToLast(50);
query.keepSynced(true);
// set up the RecyclerView
recyclerView = findViewById(R.id.myrecyclerview);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FirebaseRecyclerOptions<LocationContent> options =
new FirebaseRecyclerOptions.Builder<LocationContent>()
.setQuery(query, LocationContent.class)
.build();
firebaseRecyclerAdapter= new FirebaseRecyclerAdapter<LocationContent, LocationViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull LocationViewHolder holder, int position, @NonNull LocationContent model) {
// double doubleLatitude = model.getLatitude();
// double doubleLongitude = model.getLongitude();
readData(new MyCallback() {
@Override
public void onCallback(Double value, Double value2) {
holder.post_name.setText(model.getName());
holder.post_latitude.setText(String.valueOf (value));
holder.post_longitude.setText(String.valueOf (value2));
Log.d(TAG, value + ", " + value2);
}
});
// Log.d(TAG, "onCreate: " + model.getLongitude());
}
@NonNull
@Override
public LocationViewHolder onCreateViewHolder(@NonNull ViewGroup viewgroup, int i) {
View view = LayoutInflater.from(viewgroup.getContext()).inflate(R.layout.location_row,viewgroup,false);
return new LocationViewHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public interface MyCallback {
void onCallback(Double value,Double value2);
}
public void readData(MyCallback myCallback){
Log.d("TAG", "Before attaching the listener!");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
LocationContent lc = ds.child("South Bound").getValue(LocationContent.class);
// Log.d(TAG, lc.getLatitude() + ", " + lc.getLongitude());
myCallback.onCallback(lc.getLatitude(),lc.getLongitude());
Log.d("TAG", "Inside onDataChange() method!");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
Log.d("TAG", "After attaching the listener!");
}
@Override
protected void onStart(){
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
firebaseRecyclerAdapter.stopListening();
}
}
这是我的LocationViewHolder。
public class LocationViewHolder extends RecyclerView.ViewHolder {
public TextView post_name;
public TextView post_latitude;
public TextView post_longitude;
public LocationViewHolder(View itemView){
super(itemView);
post_name = itemView.findViewById(R.id.post_name);
post_latitude = itemView.findViewById(R.id.post_latitude);
post_longitude = itemView.findViewById(R.id.post_longitude);
}
}
这是我的LocationContect类
public class LocationContent {
private String Name;
private double Latitude;
private double Longitude;
public LocationContent(String name, double latitude, double longitude) {
Name = name;
Latitude = latitude;
Longitude = longitude;
}
public LocationContent(){
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double latitude) {
Latitude = latitude;
}
public double getLongitude() {
return Longitude;
}
public void setLongitude(Double longitude) {
Longitude = longitude;
}
}
答案 0 :(得分:0)
我只是声明了全局变量,它的工作就像我想要的