我现在在一个项目上,正在尝试使用回收器视图和视图保持器从Firebase中获取数据,但是使用我的助手类,当应用启动时,我立即尝试通过助手类检索数据,它崩溃了,当我看起来通过我的logcat,它给了我这个错误E / RecyclerView:没有连接适配器;跳过布局...。我搜索了很多答案,我看到了一些答案,但它们与我的代码无关... 这是我的主要活动代码
package com.example.android.journalapp;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.example.android.journalapp.users.LogInActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final String DATE_FORMAT = "dd/MM/yyy";
private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT,
Locale.getDefault());
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private DatabaseReference mDatabase;
String mUserId;
private RecyclerView mRecyclerView;
private FirebaseHelper helper;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
mRecyclerView = (RecyclerView) findViewById(R.id.journal_recycler);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
if (mFirebaseUser == null) {
// Not logged in, launch the Log In activity
loadLogInView();
} else {
mDatabase = FirebaseDatabase.getInstance().getReference();
mUserId = mFirebaseUser.getUid();
helper = new FirebaseHelper(context, mDatabase, mRecyclerView,
mUserId);
helper.refreshData();
}
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent addJournalIntent = new Intent(MainActivity.this,
AddJournalActivity.class);
addJournalIntent.putExtra("USER_ID", mUserId);
startActivity(addJournalIntent);
}
});
}
private void loadLogInView() {
Intent intent = new Intent(this, LogInActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
@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_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_logout) {
mFirebaseAuth.signOut();
loadLogInView();
}
return super.onOptionsItemSelected(item);
}
}
这是我的Firebase助手类
package com.example.android.journalapp;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.example.android.journalapp.model.Journal;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
public class FirebaseHelper {
private String mUserId;
private Context context;
private DatabaseReference mDb;
RecyclerView mRecyclerView;
ArrayList<Journal> journals = new ArrayList<>();
public JournalAdapter mJournalAdapter;
private static final String DATE_FORMAT = "dd/MM/yyy";
private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
public FirebaseHelper(Context context, DatabaseReference mDb, RecyclerView mRecyclerView, String mUserId) {
this.context = context;
this.mDb = mDb;
this.mRecyclerView = mRecyclerView;
this.mUserId = mUserId;
}
public void saveData(String journal, String date) {
Journal journalEntry = new Journal(journal, date);
mDb.child("users").child(mUserId).child("journals").push().setValue(journalEntry) ;
// .child("journal")
}
public void recieveData(DataSnapshot ds) {
journals.clear();
for (DataSnapshot dataSnapshot : ds.getChildren()) {
Journal journal = dataSnapshot.getValue(Journal.class);
journals.add(journal);
}
mJournalAdapter = new JournalAdapter(context, journals);
mRecyclerView.setAdapter(mJournalAdapter);
// if (journals.size() > 0) {
//
// mJournalAdapter = new JournalAdapter(context, journals);
// mRecyclerView.setAdapter(mJournalAdapter);
// } else {
// Toast.makeText(context, "No data", Toast.LENGTH_SHORT).show();
// }
}
public void refreshData() {
mDb.child("users").child(mUserId).child("journals").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
recieveData(dataSnapshot);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
recieveData(dataSnapshot);
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
这是我的模型课
package com.example.android.journalapp.model;
import java.util.Date;
public class Journal {
private String journalContent;
private String date;
public Journal(String journalContent, String date) {
this.journalContent = journalContent;
this.date = date;
}
public String getJournalContent() {
return journalContent;
}
public void setJournalContent(String journalContent) {
this.journalContent = journalContent;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
/// 这是我的适配器类
package com.example.android.journalapp;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.journalapp.model.Journal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;
public class JournalAdapter extends RecyclerView.Adapter<JournalHolder> {
Context context;
private ArrayList<Journal> journals;
private static final String DATE_FORMAT = "dd/MM/yyy";
private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
public JournalAdapter(Context context, ArrayList<Journal> journals) {
this.context = context;
this.journals = journals;
}
@NonNull
@Override
public JournalHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.journal_layout, parent, false);
JournalHolder holder = new JournalHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull JournalHolder holder, int position) {
holder.mJournalTextView.setText(journals.get(position).getJournalContent());
String date = journals.get(position).getDate();
holder.mDateTextView.setText(date);
}
@Override
public int getItemCount() {
if (journals == null) {
return 0;
}
return journals.size();
}
}
// 这是我的观看者类
package com.example.android.journalapp;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
public class JournalHolder extends RecyclerView.ViewHolder {
TextView mJournalTextView;
TextView mDateTextView;
public JournalHolder(View itemView) {
super(itemView);
mJournalTextView = itemView.findViewById(R.id.journal_text_view);
mDateTextView = itemView.findViewById(R.id.journal_date_text_view);
}
}
我能够推送到数据库,但是检索数据给我错误E / RecyclerView:未连接适配器;跳过布局
答案 0 :(得分:0)
尝试此代码。 display_data.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvData"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/dlTvEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Found"
android:textSize="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
DisplayActivity.java ..
public class DisplayActivity extends AppCompatActivity {
private RecyclerView mRvData;
private DisplayAllData allDataAdapter;
private DatabaseReference mDatabase;
private TextView mTvEmpty;
private FirebaseDatabase mFirebaseInstance;
private List<User> mUserList = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_data);
initView();
}
private void initView() {
mFirebaseInstance = FirebaseDatabase.getInstance();
mDatabase = mFirebaseInstance.getReference("usersDb/UserTable");
mRvData = findViewById(R.id.rvData);
mTvEmpty = findViewById(R.id.dlTvEmpty);
mRvData.setLayoutManager(new LinearLayoutManager(this));
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUserList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
User user = dataSnapshot1.getValue(User.class);
mUserList.add(user);
}
allDataAdapter = new DisplayAllData(DisplayActivity.this, mUserList);
mRvData.setAdapter(allDataAdapter);
allDataAdapter.notifyDataSetChanged();
if (mUserList.isEmpty())
mTvEmpty.setVisibility(View.VISIBLE);
else
mTvEmpty.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
DisplayAdapter.java ..
public class DisplayAllData extends RecyclerView.Adapter<DisplayAllData.ItemViewHolder> {
private List<User> mUserLsit = new ArrayList<>();
private Context mContext;
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new ItemViewHolder(view);
}
public DisplayAllData(Context mContext, List<User> mUserLsit) {
this.mContext = mContext;
this.mUserLsit = mUserLsit;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
User user = mUserLsit.get(position);
holder.mTvName.setText(user.name);
holder.mTvEmail.setText(user.email);
holder.mTvPwd.setText(user.pwd);
}
@Override
public int getItemCount() {
return mUserLsit.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView mTvName, mTvEmail, mTvPwd;
public ItemViewHolder(View itemView) {
super(itemView);
mTvEmail = itemView.findViewById(R.id.rlTvEmail);
mTvName = itemView.findViewById(R.id.rlTvName);
mTvPwd = itemView.findViewById(R.id.rlTvPwd);
}
}
}
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/rlTvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20dp" />
<TextView
android:id="@+id/rlTvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="@+id/rlTvName" />
<TextView
android:id="@+id/rlTvPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="@+id/rlTvEmail" />
</android.support.constraint.ConstraintLayout>
我希望您将互联网许可添加到android清单文件中。
<uses-permission android:name="android.permission.INTERNET"/>
更多信息请参考此链接 https://www.simplifiedcoding.net/firebase-realtime-database-crud/