java - Android studio RecyclerView 错误未显示任何内容(22 小时前提交,工作正常)
问题描述
我做了一些破坏我的项目的事情。它适用于我 22 小时前的最新提交。我正在使用 recylerview 从登录用户那里获取数据。我什至看不到 recylerview 在我的页面上。我怎样才能恢复到我在 22 小时前所做的 android studio 中的最新提交。我已经尝试了所有我不明白的事情。我今天什至没有碰过那个活动,但 2 小时前它停止了工作。
我所做的唯一一件事就是重构用于编辑笔记的名为 custom_layout 的视图,但我恢复了这一点,之后可能有些东西被保存在某处并导致错误这里是我的视图图片:
enter code here (MODEL)
package com.example.examapplikation.Models;
public class NotesList {
private String title;
private String text;
public NotesList(){
}
public NotesList(String title, String text){
this.title=title;
this.text= text;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
观众!enter code here
包 com.example.examapplikation.ViewHolder;
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
public TextView text_title, text_content;
public NoteViewHolder(@NonNull View itemView){
super (itemView);
text_title = itemView.findViewById(R.id.text_title);
text_content = itemView.findViewById(R.id.text_content);
itemView.setOnCreateContextMenuListener(this); // context to this view
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { // on clik update delete on every object
// menu.setHeaderTitle("Select desired option");
menu.add(0,0,getAdapterPosition(),"Edit Note");
menu.add(0,0,getAdapterPosition(),"Delete Note");
}
}
MAINACTIVITY 在此处输入代码
包 com.example.examapplikation;
public class HomeActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private FloatingActionButton addNotePageButton;
private FirebaseAuth firebaseAuth;
FirebaseDatabase database;
DatabaseReference notesDb;
FirebaseRecyclerOptions<NotesList> options;
FirebaseRecyclerAdapter<NotesList, NoteViewHolder> adapter; // adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//iniate
viewSetUp();
// refrense
notesDb = database.getReference("NoteList").child(firebaseAuth.getCurrentUser().getUid());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager( new LinearLayoutManager(this) );
showEachRow(); // call function
// buton to add note
addNotePageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomeActivity.this, NoteInputActivity.class);
startActivity(intent);
}
});
}
//Region -used for edit and delete this section start reads whats in the post and open ups a window on the same window based on the Viewholder and a xml file called custom_layout
// recycler view in this section and only the verifed logged in user can See his own data!
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) { // viewholder
if (item.getTitle().equals("Edit Note")){
showUpdateDialog(adapter.getRef(item.getOrder()).getKey(),adapter.getItem(item.getOrder()));
} else if (item.getTitle().equals("Delete Note")){
deleteNote(adapter.getRef(item.getOrder()).getKey());
}
return super.onContextItemSelected(item);
}
private void deleteNote(String adapter) { // delete
notesDb.child(adapter).removeValue();
}
private void showUpdateDialog(final String key, NotesList item) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("[ Note Edit Mode]");
builder.setMessage("Please update the desired fields");
View update_layout = LayoutInflater.from(this).inflate(R.layout.custom_layout,null); // view model layou
final EditText changed_title = update_layout.findViewById(R.id.edit_update_title);
final EditText changed_content = update_layout.findViewById(R.id.edit_update_text);
changed_title.setText(item.getTitle()); // get value to new
changed_content.setText(item.getText());
builder.setView(update_layout);
builder.setPositiveButton("Save changes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String title = changed_title.getText().toString();
String content = changed_content.getText().toString();
NotesList notesList = new NotesList(title,content);
notesDb.child(key).setValue(notesList);
Toast.makeText(HomeActivity.this,"Note Updated", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
//# EndRegion
// Region to read data from dataBase using Viewholder and model Notes and a xml file called note_each_row + the home xml file.
private void showEachRow(){ // recycler view
options = new FirebaseRecyclerOptions.Builder<NotesList>()
.setQuery(notesDb,NotesList.class)
.build();
adapter = new FirebaseRecyclerAdapter<NotesList, NoteViewHolder>(options) { // noteViewModel to model to get
@Override
protected void onBindViewHolder(@NonNull NoteViewHolder holder, int position, @NonNull NotesList model) {
holder.text_title.setText(model.getTitle());
holder.text_content.setText(model.getText());
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.notes_each_row,viewGroup,false); // inflate the rows
return new NoteViewHolder(itemView);
}
};
recyclerView.setAdapter(adapter);
}
//# end Region
//#start region initate to xml
private void viewSetUp(){
recyclerView = findViewById(R.id.recyclerView);
addNotePageButton = findViewById(R.id.fab_button_addPage);
firebaseAuth = FirebaseAuth.getInstance(); // get inSTACE
database = FirebaseDatabase.getInstance();
}
// end region
//# Menu
private void Logout(){ // sign out method called in switchcase
firebaseAuth.signOut();
finish();
startActivity(new Intent(HomeActivity.this,MainActivity.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { //create menu on toolbar
getMenuInflater().inflate(R.menu.menu,menu); //inflated inside
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { // handle on click events on items on menu
switch(item.getItemId()){
case R.id.logoutMenu:{
Logout();
finish();
break;
}
case R.id.ProfileMenu:{
startActivity(new Intent(HomeActivity.this,ProfileActivity.class));
finish();
break;
}
case R.id.HomeMenu:{
startActivity(new Intent(HomeActivity.this,HomeActivity.class));
finish();
break;
}
}
return super.onOptionsItemSelected(item);
}
}
// Endregion
recyclerVIEW 中每一行的 XML
enter code here
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:id="@+id/random"
android:layout_width="45dp"
android:layout_height="37dp"
android:src="@drawable/note" />
<TextView
android:id="@+id/text_title"
android:background="@color/LightPink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
style="@style/TextAppearance.AppCompat.Headline"/>
<View
android:layout_marginTop="5dp"
android:id="@+id/fillView"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#c0c0c0"/>
<TextView
android:id="@+id/text_content"
android:text="content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
即使我不再获得数据,我也应该能够在我的页面上看到卡片视图..?
解决方案
别担心,这些可能会对您有所帮助:这可能与内存不足和索引有关。
- 重启你的电脑
- 开始你的 ide(android 工作室)
- 构建-->清理
- 构建->重建项目
- File-->Invalidate caches/Restart-->Invalidate and restart
- 重启android studio后(上一步):File-->Sync with file system
- 文件--> 将项目与 gradle 文件同步
- 关闭您的项目
- 重启android studio 10.几分钟后打开你的项目。
推荐阅读
- facebook - 如何在同一个 Flutter 应用中使用 Facebook 登录和 Facebook 应用事件?
- php - laravel 多对多多键
- javascript - 我想使用我的 Button 组件将 id 应用于我的元素
- python-3.x - 串行写入字节列表
- python - 是否有根据数据类型为变量着色的 Jupyter 主题?
- c++ - 如何使用 IPC(Unix 域套接字)在 Node.js(首选 Express 模块)服务器和 C++ 应用程序之间进行本地通信
- html - 使用 Bootstrap 条带表而不添加 class="table table-striped"
- node.js - 猫鼬 | 按填充项目数组中的字段数组搜索
- scala - 是否可以使用模式注册表将 Avro 消息转换为 scala 案例类?
- javascript - 如何在 Ace 编辑器中启用搜索框?