当我使用 recyclerview 和自定义适配器启动片段时出错
Error when i start fragment with recyclerview and custom adapter
我正在尝试在 Fragment
中显示来自 Firebase 的一些数据,为此我需要使用自定义 adapter.When 打开片段,我的应用程序正在停止。当我在 Constructor 应用程序中删除 this.inbox_interface = (inbox_data) mContext;
时,片段是空的。在 logcat 我得到错误
InboxActivity cannot be cast to InboxAdapter$inbox_data at
InboxAdapter.(InboxAdapter.java:51) at
ChatList.onCreateView(ChatList.java:68)
适配器代码
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
User contact_user;
public ArrayList<InboxObject> inbox_list;
private Fragment chatlist;
public Context mContext;
public inbox_data inbox_interface;
public FragmentManager f_manager;
public interface inbox_data{
void onInboxClick(String reciver, String photo, String fn);
}
public InboxAdapter(ArrayList<InboxObject> inbox_list, Context mContext) {
this.inbox_list = inbox_list;
this.f_manager = f_manager;
this.mContext = mContext;
this.inbox_interface = (inbox_data) mContext;
}
代码片段
public class ChatList extends Fragment implements ContactsAdapter.iData,InboxAdapter.inbox_data{
LinearLayoutManager messageLayoutManager,contactsLayoutManager;
InboxAdapter adapter_inbox;
ArrayList<User> all_users = new ArrayList<>();
User current;
RecyclerView inbox_rv;
ArrayList<InboxObject> allInboxObjects;
public InboxAdapter.inbox_data inbox_interface;
public static final String CONTACT_TAG = "contact_user";
public static final String CURRENT_USER = "current_user";
GoogleApiClient mGoogleApiClient;
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
StorageReference f_storage = FirebaseStorage.getInstance().getReference();
public ChatList() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
allInboxObjects = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = container.getContext();
View layout = LayoutInflater.from(context).inflate(R.layout.fragment_chat_list, container, false);
inbox_rv = (RecyclerView) layout.findViewById(R.id.inboxsRecyclerView);
// Inflate the layout for this fragment
messageLayoutManager= new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity());
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
return inflater.inflate(R.layout.fragment_chat_list, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onStart() {
super.onStart();
Log.d("demo","onStart");
f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inboxobjects").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("demo","onStart:inside inbox data change");
allInboxObjects.clear();
for (com.google.firebase.database.DataSnapshot snapshot: dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
}
else{
InboxObject io = snapshot.getValue(InboxObject.class);
allInboxObjects.add(io);
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity());
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
}
}
您需要移出您在适配器中声明的 interface
,并且必须在您的 activity 中实现该接口。
因此创建一个名为 inbox_data.java
的单独接口 class
public interface inbox_data{
void onInboxClick(String reciver, String photo, String fn);
}
然后像这样修改你的适配器。
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
User contact_user;
public ArrayList<InboxObject> inbox_list;
private Fragment chatlist;
public Context mContext;
public inbox_data inbox_interface;
public FragmentManager f_manager;
public InboxAdapter(ArrayList<InboxObject> inbox_list, Context mContext, inbox_data inbox_interface) {
this.inbox_list = inbox_list;
this.f_manager = f_manager;
this.mContext = mContext;
this.inbox_interface = inbox_interface; // Assign the interface instead
}
现在在您的片段中实现接口。
public class ChatList extends Fragment implements ContactsAdapter.iData, inbox_data{
LinearLayoutManager messageLayoutManager,contactsLayoutManager;
InboxAdapter adapter_inbox;
ArrayList<User> all_users = new ArrayList<>();
User current;
RecyclerView inbox_rv;
ArrayList<InboxObject> allInboxObjects;
public InboxAdapter.inbox_data inbox_interface;
public static final String CONTACT_TAG = "contact_user";
public static final String CURRENT_USER = "current_user";
GoogleApiClient mGoogleApiClient;
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
StorageReference f_storage = FirebaseStorage.getInstance().getReference();
public ChatList() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
allInboxObjects = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = container.getContext();
View layout = LayoutInflater.from(context).inflate(R.layout.fragment_chat_list, container, false);
inbox_rv = (RecyclerView) layout.findViewById(R.id.inboxsRecyclerView);
// Inflate the layout for this fragment
messageLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity(), this); // Pass the interface using this
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
return inflater.inflate(R.layout.fragment_chat_list, container, false);
}
@Override
public void onStart() {
super.onStart();
Log.d("demo","onStart");
f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inboxobjects").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("demo","onStart:inside inbox data change");
allInboxObjects.clear();
for (com.google.firebase.database.DataSnapshot snapshot: dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
}
else{
InboxObject io = snapshot.getValue(InboxObject.class);
allInboxObjects.add(io);
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity(), this); // Pass the interface to the adapter using this
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
}
// Override the function of the implemented interface.
@Override
public void void onInboxClick(String reciver, String photo, String fn) {
// Do something based on the item click in the RecyclerView
}
}
我正在尝试在 Fragment
中显示来自 Firebase 的一些数据,为此我需要使用自定义 adapter.When 打开片段,我的应用程序正在停止。当我在 Constructor 应用程序中删除 this.inbox_interface = (inbox_data) mContext;
时,片段是空的。在 logcat 我得到错误
InboxActivity cannot be cast to InboxAdapter$inbox_data at InboxAdapter.(InboxAdapter.java:51) at ChatList.onCreateView(ChatList.java:68)
适配器代码
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
User contact_user;
public ArrayList<InboxObject> inbox_list;
private Fragment chatlist;
public Context mContext;
public inbox_data inbox_interface;
public FragmentManager f_manager;
public interface inbox_data{
void onInboxClick(String reciver, String photo, String fn);
}
public InboxAdapter(ArrayList<InboxObject> inbox_list, Context mContext) {
this.inbox_list = inbox_list;
this.f_manager = f_manager;
this.mContext = mContext;
this.inbox_interface = (inbox_data) mContext;
}
代码片段
public class ChatList extends Fragment implements ContactsAdapter.iData,InboxAdapter.inbox_data{
LinearLayoutManager messageLayoutManager,contactsLayoutManager;
InboxAdapter adapter_inbox;
ArrayList<User> all_users = new ArrayList<>();
User current;
RecyclerView inbox_rv;
ArrayList<InboxObject> allInboxObjects;
public InboxAdapter.inbox_data inbox_interface;
public static final String CONTACT_TAG = "contact_user";
public static final String CURRENT_USER = "current_user";
GoogleApiClient mGoogleApiClient;
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
StorageReference f_storage = FirebaseStorage.getInstance().getReference();
public ChatList() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
allInboxObjects = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = container.getContext();
View layout = LayoutInflater.from(context).inflate(R.layout.fragment_chat_list, container, false);
inbox_rv = (RecyclerView) layout.findViewById(R.id.inboxsRecyclerView);
// Inflate the layout for this fragment
messageLayoutManager= new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity());
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
return inflater.inflate(R.layout.fragment_chat_list, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onStart() {
super.onStart();
Log.d("demo","onStart");
f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inboxobjects").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("demo","onStart:inside inbox data change");
allInboxObjects.clear();
for (com.google.firebase.database.DataSnapshot snapshot: dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
}
else{
InboxObject io = snapshot.getValue(InboxObject.class);
allInboxObjects.add(io);
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity());
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
}
}
您需要移出您在适配器中声明的 interface
,并且必须在您的 activity 中实现该接口。
因此创建一个名为 inbox_data.java
public interface inbox_data{
void onInboxClick(String reciver, String photo, String fn);
}
然后像这样修改你的适配器。
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
User contact_user;
public ArrayList<InboxObject> inbox_list;
private Fragment chatlist;
public Context mContext;
public inbox_data inbox_interface;
public FragmentManager f_manager;
public InboxAdapter(ArrayList<InboxObject> inbox_list, Context mContext, inbox_data inbox_interface) {
this.inbox_list = inbox_list;
this.f_manager = f_manager;
this.mContext = mContext;
this.inbox_interface = inbox_interface; // Assign the interface instead
}
现在在您的片段中实现接口。
public class ChatList extends Fragment implements ContactsAdapter.iData, inbox_data{
LinearLayoutManager messageLayoutManager,contactsLayoutManager;
InboxAdapter adapter_inbox;
ArrayList<User> all_users = new ArrayList<>();
User current;
RecyclerView inbox_rv;
ArrayList<InboxObject> allInboxObjects;
public InboxAdapter.inbox_data inbox_interface;
public static final String CONTACT_TAG = "contact_user";
public static final String CURRENT_USER = "current_user";
GoogleApiClient mGoogleApiClient;
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();
StorageReference f_storage = FirebaseStorage.getInstance().getReference();
public ChatList() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
allInboxObjects = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = container.getContext();
View layout = LayoutInflater.from(context).inflate(R.layout.fragment_chat_list, container, false);
inbox_rv = (RecyclerView) layout.findViewById(R.id.inboxsRecyclerView);
// Inflate the layout for this fragment
messageLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
ArrayList<InboxObject> allInboxObjects = new ArrayList<>();
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity(), this); // Pass the interface using this
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
return inflater.inflate(R.layout.fragment_chat_list, container, false);
}
@Override
public void onStart() {
super.onStart();
Log.d("demo","onStart");
f_database.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("inboxobjects").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("demo","onStart:inside inbox data change");
allInboxObjects.clear();
for (com.google.firebase.database.DataSnapshot snapshot: dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
}
else{
InboxObject io = snapshot.getValue(InboxObject.class);
allInboxObjects.add(io);
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
InboxAdapter adapter_inbox = new InboxAdapter(allInboxObjects, getActivity(), this); // Pass the interface to the adapter using this
inbox_rv.setLayoutManager(messageLayoutManager);
inbox_rv.setAdapter(adapter_inbox);
}
// Override the function of the implemented interface.
@Override
public void void onInboxClick(String reciver, String photo, String fn) {
// Do something based on the item click in the RecyclerView
}
}