Stack Overflow на русском Asked by SMD 70 on February 18, 2021
При переходе на новое активити recyclerview вообще не отображается, если выйти назад и зайти снова, то список без проблем подгружается из firebase. В чем может быть проблема?
public class Comments extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {
private String str_title = "Отзывы";
private EditText edt_comment_name, edt_comment;
private TextView showComment, hideComment;
private RelativeLayout layout_comment, layout_comment1;
List<User> userList;
private RecyclerView recyclerView;
private RecyclerAdapterUser adapter;
private DatabaseReference mDataBase;
private String USER_KEY = "User";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(str_title);
}
recyclerView = findViewById(R.id.rv_comment);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
userList = new ArrayList<>();
adapter = new RecyclerAdapterUser(userList);
recyclerView.setAdapter(adapter);
mDataBase = FirebaseDatabase.getInstance().getReference(USER_KEY);
mDataBase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// очищаем список if (userList.size() > 0) userList.clear();
for (DataSnapshot ds : snapshot.getChildren()) {
User user = ds.getValue(User.class);
userList.add(user);
}
// добавляем адаптеру метод adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
init();
onClickShowAndHide();
}
private void init() {
showComment = findViewById(R.id.btn_show_comment);
hideComment = findViewById(R.id.btn_hide_comment);
layout_comment = findViewById(R.id.layout_comment);
layout_comment1 = findViewById(R.id.layout_comment1);
edt_comment_name = findViewById(R.id.edt_comment_name);
edt_comment = findViewById(R.id.comment);
}
public void onClickShowAndHide() {
showComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout_comment.setVisibility(View.VISIBLE);
showComment.setVisibility(View.INVISIBLE);
hideComment.setVisibility(View.VISIBLE);
layout_comment1.setVisibility(View.GONE);
}
});
hideComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout_comment.setVisibility(View.GONE);
showComment.setVisibility(View.VISIBLE);
hideComment.setVisibility(View.INVISIBLE);
layout_comment1.setVisibility(View.VISIBLE);
}
});
}
public void onClickSave(View view) {
String id = mDataBase.getKey();
String name = edt_comment_name.getText().toString();
String comment = edt_comment.getText().toString();
User user = new User(id, name, comment);
if (name.equalsIgnoreCase("") || comment.equalsIgnoreCase("")) {
Snackbar.make(view, R.string.checkout_fill_form, Snackbar.LENGTH_SHORT).show();
} else {
mDataBase.push().setValue(user);
Toast.makeText(this, "Комментарий успешно добавлен", Toast.LENGTH_SHORT).show();
layout_comment.setVisibility(View.GONE);
layout_comment1.setVisibility(View.VISIBLE);
showComment.setVisibility(View.VISIBLE);
hideComment.setVisibility(View.INVISIBLE);
edt_comment_name.setText("");
edt_comment.setText("");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
}
решение прокомментировал в коде
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP